MsSQL   发布时间:2022-05-16  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Android 蓝牙2.0的使用方法详解大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

本文为大家分享了Android操作蓝牙2.0的使用方法,供大家参,具体内容如下

1.Android操作蓝牙2.0的使用流程
(1)找到设备uuid
(2)获取蓝牙适配器,使得蓝牙处于可发现模式,获取下位机的socket,并且与上位机建立建立连接,获取获取输入流和输出流,两个流都不为空时,表示连接成功。否则是连接失败。
(3).与下位机的socket开始通信。
(4).通信结束后,断开连接(关闭流,关闭socket)

2接下来接直接上代码:
2.1找到设备uuid(一般厂商都会给开发者提供)

复制代码 代码如下:
UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); 

2.2与蓝牙设备建立连接

BluetoothAdapter myBluetoothAdapter = null;//蓝牙适配器 
BluetoothServerSocket mBThServer = null;//上位机<span style="font-family: Arial,Helvetica,sans-serif;">ServerSocket</span> 
BluetoothSocket mBTHSocket = null;//下位机的socket 
InputStream mminstream = null;//输入流 
OutputStream mmOutStream = null;//输出流 
<span style="font-family: Arial,sans-serif; BACkground-color: rgb(255,255,255);">    </span>
myBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();//获取适配器 
Set<BluetoothDevice> pairedDevices = myBluetoothAdapter 
    .getBondedDevices();//获取适配器下的所有蓝牙设备 
if (pairedDevices.size() > 0) { 
  for (Iterator<BluetoothDevice> iterator = pairedDevices 
      .iterator(); iterator.hasNext();) { 
    BluetoothDevice device = (BluetoothDevicE) iterator 
        .next(); 
    if (DEVICE_NAME1.equals(device.getName()) 
        || DEVICE_NAME2.equals(device.getName()) 
        || DEVICE_NAME3.equals(device.getName()) 
        || DEVICE_NAME4.equals(device.getName())) { 
      try { 
        myBluetoothAdapter.enable();//将适配器设置可用 
        Intent discoverableIntent = new Intent( 
            BluetoothAdapter.ACTION_requEST_disCOVERABLE);// 使得蓝牙处于可发现模式,持续时间150s 
        discoverableIntent 
            .putExtra( 
                BluetoothAdapter.EXTRA_disCOVERABLE_DURATION,150); 
        mBTHSocket = device 
            .createRfcommSocketToserviceRecord(MY_UUID);//获取下位机的socket 
 
        int sdk = Integer.parseInt(Build.VERSION.SDK); 
        if (sdk >= 10) { 
          mBTHSocket = device 
              .createInsecureRfcommSocketToserviceRecord(MY_UUID); 
        } else { 
          mBTHSocket = device 
              .createRfcommSocketToserviceRecord(MY_UUID); 
        } 
 
        mBThServer = myBluetoothAdapter 
            .listenUsingRfcommWithserviceRecord( 
                "myServerSocket",MY_UUID);监听可用的设备 
        mBTHSocket.connect(); // 建立连接 
        mminstream = mBTHSocket.geTinputStream();// 获取输入流 
        mmOutStream = mBTHSocket.getoutputStream();// 获取输出流 
 
      } catch (IOException E) { 
        ett.setText("设备连接异常!"); 
      } 
      if ((mminstream != null) && (mminstream != null))// 二者不为空时,表示连接成功,否则连接失败 
      { 
        ett.setText("设备连接成功!"); 
      } else { 
        ett.setText("设备连接失败!"); 
      } 
      break; 
    } 
  } 
} 

2.3开始发送数据,并且读取数据(字节数组)                    

 if ((mminstream == null) || (mminstream == null)) { 
  Readflage = -2;// 连接异常 
  return; 
} 
mmOutStream.write(cmd_find);//写入查找指令 
Thread.sleep(200); 
int datalen = mminstream.read(recData);//读取数据 

 注意:cmd_find和recData都是字节数组byte[].
以上代码就一次发送指令和读取数据的步骤。很简单吧

2.4断开连接

if ((mminstream == null) || (mminstream == null)) { 
  return; 
} 
//关闭流和socket 
mmOutStream.close(); 
mminstream.close(); 
mBTHSocket.close(); 
mBThServer.close(); 

最后总结一下,基本就3大步,第一建立连接,第二发送数据读取数据,第三步断开连接。今天就这些了,以后会写关于蓝牙4.0 ble 在Android中的使用,这两个还是有很多不同的,大家请期待。

大佬总结

以上是大佬教程为你收集整理的Android 蓝牙2.0的使用方法详解全部内容,希望文章能够帮你解决Android 蓝牙2.0的使用方法详解所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。