【发布时间】:2014-05-05 18:20:58
【问题描述】:
我正在使用 Android 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;
}
【问题讨论】:
标签: android bluetooth bluetooth-lowenergy android-4.4-kitkat android-4.3-jelly-bean