W/BluetoothGatt: 回调中未处理的异常
W/BluetoothGatt: Unhandled exception in callback
我会解释我的问题,我会收取可用 BLE 的列表,当找到具体的 BLE 时,我会做下一个
gatt = device.connectGatt(MainActivity.this, true, gattCallback);
那里一切都很好(或者我认为如此)。
但是当我运行下面的代码时:gatt.getService(BLOOD_PRESURE_SERVICE_UUID).getCharacteristic(BLOOD_PRESURE_MEASUREMENT_CHAR_UUID);
我收到这个错误:
W/BluetoothGatt: Unhandled exception in callback
java.lang.NullPointerException: Attempt to invoke virtual method 'android.bluetooth.BluetoothGattCharacteristic
android.bluetooth.BluetoothGattService.getCharacteristic(java.util.UUID)'
on a null object reference
不知道为什么,gatt对象自带内容,所以不是null,也不应该是
BluetoothGattCallback gattCallback = new BluetoothGattCallback() {
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
super.onConnectionStateChange(gatt, status, newState);
if (newState == BluetoothProfile.STATE_CONNECTED){
gatt.discoverServices();
}
}
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
super.onServicesDiscovered(gatt, status);
BluetoothGattCharacteristic characteristic =
gatt.getService(BLOOD_PRESURE_SERVICE_UUID)
.getCharacteristic(BLOOD_PRESURE_MEASUREMENT_CHAR_UUID);
gatt.setCharacteristicNotification(characteristic, true);
BluetoothGattDescriptor descriptor =
characteristic.getDescriptor(CLIENT_CHARACTERISTIC_CONFIG_UUID);
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
gatt.writeDescriptor(descriptor);
}
};
我在另一个条目中读到,有人碰巧 CallBack 为空,这就是错误跳转的原因。该用户对他们提供的解决方案发表评论,Unhandled exception in callback while trying to connect with BluetoothGatt
但如果这是我的情况,我不知道如何解决。
请帮忙。谢谢
您尝试调用 getCharacteristic
的对象为空。这意味着 gatt.getService(BLOOD_PRESURE_SERVICE_UUID)
返回了 null。这意味着您连接的设备没有 blood pressure gatt 服务。 (而且你拼错了 pressure...)
我会解释我的问题,我会收取可用 BLE 的列表,当找到具体的 BLE 时,我会做下一个
gatt = device.connectGatt(MainActivity.this, true, gattCallback);
那里一切都很好(或者我认为如此)。
但是当我运行下面的代码时:gatt.getService(BLOOD_PRESURE_SERVICE_UUID).getCharacteristic(BLOOD_PRESURE_MEASUREMENT_CHAR_UUID);
我收到这个错误:
W/BluetoothGatt: Unhandled exception in callback java.lang.NullPointerException: Attempt to invoke virtual method 'android.bluetooth.BluetoothGattCharacteristic android.bluetooth.BluetoothGattService.getCharacteristic(java.util.UUID)' on a null object reference
不知道为什么,gatt对象自带内容,所以不是null,也不应该是
BluetoothGattCallback gattCallback = new BluetoothGattCallback() {
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
super.onConnectionStateChange(gatt, status, newState);
if (newState == BluetoothProfile.STATE_CONNECTED){
gatt.discoverServices();
}
}
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
super.onServicesDiscovered(gatt, status);
BluetoothGattCharacteristic characteristic =
gatt.getService(BLOOD_PRESURE_SERVICE_UUID)
.getCharacteristic(BLOOD_PRESURE_MEASUREMENT_CHAR_UUID);
gatt.setCharacteristicNotification(characteristic, true);
BluetoothGattDescriptor descriptor =
characteristic.getDescriptor(CLIENT_CHARACTERISTIC_CONFIG_UUID);
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
gatt.writeDescriptor(descriptor);
}
};
我在另一个条目中读到,有人碰巧 CallBack 为空,这就是错误跳转的原因。该用户对他们提供的解决方案发表评论,Unhandled exception in callback while trying to connect with BluetoothGatt 但如果这是我的情况,我不知道如何解决。
请帮忙。谢谢
您尝试调用 getCharacteristic
的对象为空。这意味着 gatt.getService(BLOOD_PRESURE_SERVICE_UUID)
返回了 null。这意味着您连接的设备没有 blood pressure gatt 服务。 (而且你拼错了 pressure...)