最近在做蓝牙开发,发现订阅蓝牙通知后,第二次连接会出现连接不上的情况,
BluetoothGattCharacteristic notifycharacteristic = service.getCharacteristic(GetDataUtils.characterReadUUID);//GetDataUtils.characterReadUUID为我司的订阅通知的UUID,为了方便我起名为read,其实应该起名为notify
mBluetoothGatt.setCharacteristicNotification(notifycharacteristic, true);
BluetoothGattDescriptor descriptor descriptor = notifycharacteristic.getDescriptor(UUID.fromString("00002902-0000-1000-8000-00805f9b34fb"));
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
mBluetoothGatt.writeDescriptor(descriptor);
以上是正常订阅通知的方式。但是会出现第二次连接不上的情况。这是为什么呢
我钻研了将近了一个月,其实需要在断开时把接受通知关闭,网上和安卓蓝牙官方DEMO都没有把关闭蓝牙通知写出来
descriptor.setValue(BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE);
把setValue的ENABLE改为DISABLE,
也就是
descriptor.setValue(BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE);
mBluetoothGatt.writeDescriptor(descriptor);
然后再writeDescriptor告诉底层我要关闭接受通知,第二次连接的时候就能完美连接上了