BLE低功耗蓝牙详解,解决读写、通知失败问题

开发公司的智能锁,通过蓝牙控制,是一种低功耗蓝牙,由于之前没做过,一路磕磕碰碰总算完成了,在此写下总结,帮助需要的朋友少走一些弯路,主要解决蓝牙读写数据和设置通知失败的问题。

开发步骤:

  1. 检测蓝牙是否可用,绑定蓝牙服务
  2. 使用BluetoothAdapter.startLeScan来扫描低功耗蓝牙设备
  3. 在扫描到设备的回调函数中会得到BluetoothDevice对象,并使用BluetoothAdapter.stopLeScan停止扫描
  4. 使用BluetoothDevice.connectGatt来获取到BluetoothGatt对象
  5. 执行BluetoothGatt.discoverServices,这个方法是异步操作,在回调函数onServicesDiscovered中得到status, 通过判断status是否等于BluetoothGatt.GATT_SUCCESS来判断查找Service是否成功
  6. 如果成功了,则通过BluetoothGatt.getService来获取BluetoothGattService
  7. 接着通过BluetoothGattService.getCharacteristic获取BluetoothGattCharacteristic
  8. 然后通过BluetoothGattCharacteristic.getDescriptor获取BluetoothGattDescriptor

首先1-2步都是固定步骤,没什么好说的,按照Android提供的Demo写就可以了,第3步扫描到设备会回调,在回调中,一般根据设备名称找到想要连接的设备,然后连接,获取到BluetoothGatt对象,然后在BluetoothGatt.discoverServices中会找到很多Services,根据硬件工程师提供的UUID连接你需要的Services;

//Ble服务发现回调
mBleService.setOnServicesDiscoveredListener(newBleService.OnServicesDiscoveredListener() {
@Override
public voidonServicesDiscovered(BluetoothGatt gatt,intstatus) {
List services = gatt.getServices();
for(BluetoothGattService service : services) {
if(service.getUuid().toString().equals(UUID_KEY_DATA)) {
LogUtil.d("蓝牙服务找到了");
mReadCharacteristic= service.getCharacteristic(UUID_READ);
mWriteCharacteristic= service.getCharacteristic(UUID_WRITE);
break;
}
}
}
});

//设置通知
private BluetoothGatt mBluetoothGatt;
BluetoothGattCharacteristic characteristic;
boolean enabled;
...
mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);
...
BluetoothGattDescriptor descriptor = characteristic.getDescriptor(
UUID.fromString(SampleGattAttributes.CLIENT_CHARACTERISTIC_CONFIG));
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
mBluetoothGatt.writeDescriptor(descriptor);
//写入
if(mBluetoothGatt!=null) {
characteristic.setValue(value);
returnmBluetoothGatt.writeCharacteristic(characteristic);
}
//读取
mBluetoothGatt.readCharacteristic(characteristic);

读写和设置通知有几个重载的方法,这里就只列举其中一种,可以根据需求来写。
找到Services后再根据硬件工程师提供的UUID找到读写和通知的UUID,然后就能进行读写操作了。这里有一个地方需要注意!!!读写和设置通知都是单步操作,必须执行完一个才能执行第二个,否则会操作失败。,之前我在这个地方纠结了很久,网上搜索了很久找不到答案,官方demo里面也没有关于读写的操作,网上大部分写的都是仿照官方demo写的原理之类的。通知打开或关闭其实也是一次写入的操作,这个在官方demo中有。
后来我查看源码,终于找到了解决的办法。(还是要多看源码,少搜索,养成了习惯就不好了,依赖性强,不会独立解决问题)在源码BluetoothGatt类中,有读写和通知的具体代码,都包含了下面这段代码,所以判断都是单步操作。

synchronized(mDeviceBusy) {
if(mDeviceBusy)return false;
mDeviceBusy=true;
}

通过搜索mDeviceBusy=false找到了解决的答案,在每次执行写、读、通知操作后都会有一个是否成功的回调,在回调中设置了mDeviceBusy=false,所以在回调中可以继续下一步操作了;在BluetoothGattCallback 中可以重写以下回调,根据具体业务逻辑来重写,都是异步操作。

// 发现Services回调
@Override
public voidonServicesDiscovered(BluetoothGatt gatt,intstatus) {
if(mOnServicesDiscoveredListener!=null) {
mOnServicesDiscoveredListener.onServicesDiscovered(gatt, status);
}

if(status == BluetoothGatt.GATT_SUCCESS) {
broadcastUpdate(ACTION_GATT_SERVICES_DISCOVERED);
}else{
Log.w(TAG,"onServicesDiscovered received: "+ status);
}

}
// 读操作回调
@Override
public voidonCharacteristicRead(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic,intstatus) {
if(mOnDataAvailableListener!=null) {
mOnDataAvailableListener.onCharacteristicRead(gatt, characteristic, status);
}
}
//写操作回调
@Override
public voidonCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic,intstatus) {
super.onCharacteristicWrite(gatt, characteristic, status);
String address = gatt.getDevice().getAddress();
for(inti =0; i < characteristic.getValue().length; i++) {
Log.i(TAG,"address: "+ address +",Write: "+ characteristic.getValue()[i]);
}
if(mOnDataAvailableListener!=null) {
mOnDataAvailableListener.onCharacteristicWrite(gatt, characteristic,status);
}
}
//通知写入回调
@Override
public voidonDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor,
intstatus) {
if(mOnDataAvailableListener!=null) {
mOnDataAvailableListener.onDescriptorWrite(gatt, descriptor,status);
}
}
//通知收到数据的回调
@Override
public voidonCharacteristicChanged(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic) {
if(mOnDataAvailableListener!=null) {
mOnDataAvailableListener.onCharacteristicChanged(gatt, characteristic);
}
}

最后在BLE低功耗蓝牙中最重要的三个操作完成了,剩下就是一些收尾工作,最后说几点需要注意的地方,释放该释放的资源避免内存泄漏,这可以参考官方demo来写;扫描到需要设备后记得停止扫描;打开通知后,记得关闭通知,提高蓝牙的性能。
BLE官方demo
官方讲解

    原文作者:WuTa0
    原文地址: https://www.jianshu.com/p/f8130a0bfd94
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞