软件编程
位置:首页>> 软件编程>> Android编程>> Android 蓝牙2.0的使用方法详解

Android 蓝牙2.0的使用方法详解

作者:请叫我小东子  发布时间:2022-10-19 17:21:21 

标签: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, Helvetica, 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中的使用,这两个还是有很多不同的,大家请期待。

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com