现在时间是2015年11月24日02:58:24 (夜里),这个问题困扰我一个星期了,仍没能解决,所以希望大神 赐教啊,真的,调了这么久 这么晚,看在辛劳的份上 给个原因分析吧!
尝试了三种setCharacteristicNotification方法,一下列出两种,感觉设置绝不会再出问题,之前的操作 service characteristic 都很熟,但结果蓝牙数据改变时 仍没能触发onCharacteristicChanged
// public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic, boolean enabled) //{
// if (mBluetoothAdapter == null || mBluetoothGatt == null) {
// Log.w(TAG, "BluetoothAdapter not initialized");
// return;
// }
// mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);
// // BluetoothGattDescriptor descriptor = characteristic.getDescriptor(
// // UUID.fromString(SampleGattAttributes.CLIENT_CHARACTERISTIC_CONFIG));
// // descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
// // mBluetoothGatt.writeDescriptor(descriptor);
// // if (UUID_HEART_RATE_MEASUREMENT.equals(characteristic.getUuid()))
// {
// BluetoothGattDescriptor descriptor = characteristic .getDescriptor(UUID .fromString(SampleGattAttributes.CLIENT_CHARACTERISTIC_CONFIG));
// descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
// mBluetoothGatt.writeDescriptor(descriptor);
// }
// }
lic void setCharacteristicNotification ( BluetoothGattCharacteristic characteristic, boolean enabled) {
if (mBluetoothAdapter == null || mBluetoothGatt == null) {
return;
}
mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);
BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID.fromString(SampleGattAttributes.CLIENT_CHARACTERISTIC_CONFIG));
if (descriptor != null) {
descriptor.setValue(enabled ? BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE : new byte[] { 0x00, 0x00 });
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
mBluetoothGatt.writeDescriptor(descriptor);
}
}
回调函数中的
private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback()
{
@Override
public void onCharacteristicChanged(BluetoothGatt gatt,BluetoothGattCharacteristic characteristic)
{
Log.e("onCharacteristicChanged", "onCharacteristicChanged");
broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
if (characteristic.getValue() != null) {
Log.e("==data", characteristic.getStringValue(0));
//System.out.println(characteristic.getStringValue(0));
}
System.out.println("--------onCharacteristicChanged-----");
Log.e("==data", "--------onCharacteristicChanged-----");
}
}
为什么 为什么啊,蓝牙数据的改变为什么没能触发onCharacteristicChanged