我正在使用安卓Nexus 7通过蓝牙低功耗链接来连接设备。如果我不与设备进行通信,我可以连接设备并保持连接。
然而,如果我通过点击按钮启用一个特定特征的通知,那么在几秒钟的数据传输后,设备将会与平板电脑断开连接。
有人知道问题可能是什么吗?非常感谢!
这是我的代码:
public boolean setCharacteristicNotification(boolean enabled){
if (mBluetoothAdapter == null || mBluetoothGatt == null) {
Log.w(TAG, "BluetoothAdapter not initialized");
return false;
}
BluetoothGattService Service = mBluetoothGatt.getService(UUID_MY_SERVICE);
if (Service == null) {
Log.e(TAG, "service not found!");
return false;
}
BluetoothGattCharacteristic characteristic = Service.getCharacteristic(UUID_MY_CHARACTERISTIC);
final int charaProp = characteristic.getProperties();
if ((charaProp | BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0) {
mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);
BluetoothGattDescriptor descriptor = characteristic.getDescriptor(
UUID.fromString(SampleGattAttributes.CLIENT_CHARACTERISTIC_CONFIG));
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
mBluetoothGatt.writeDescriptor(descriptor);
return true;
}
return false;
}