我正在开发一个Android应用程序,我需要连接到BLE医疗设备(此处为血压)并传输测量的BP。我正在升级代码。是的,我有蓝牙2.0代码,它成功连接到BP设备,成功写入和读取设备中的值。
但是在BLE中,我成功地能够写入值(这里BP设备需要一个特定的值来编写以开始测量值),当我尝试启用Notifications时,我得到Null指针Exception。
writeCharacteristic方法的代码:
public boolean writeCharacteristic(BluetoothGatt mBluetoothGatt) throws InterruptedException {
if (mBluetoothAdapter == null ){
Log.w(TAG, "BluetoothAdapter not initialized");
return false;
}
if (mBluetoothGatt == null ){
Log.w(TAG, "BluetoothGatt not initialized");
return false;
}
//BluetoothGattService Service = mBluetoothGatt.getService(CONFIG_DESCRIPTOR);
BluetoothGattService Service = mBluetoothGatt.getService(MY_UUID);
if (Service == null){
Log.e(TAG, "Service not found!");
return false;
}
BluetoothGattCharacteristic characteristic = Service.getCharacteristic(MY_UUID_CHARACTERISTIC);
if (characteristic == null){
Log.e(TAG, "Characteristic not found!");
return false;
}
//int counter = 3;
boolean status = false;
byte[] value = {(byte)0x0A};
characteristic.setValue(value);
//Enable local notifications
mBluetoothGatt.setCharacteristicNotification(characteristic, true);
//Enabled remote notifications
BluetoothGattDescriptor desc = characteristic.getDescriptor(CONFIG_DESCRIPTOR);
desc.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
mBluetoothGatt.writeDescriptor(desc);
status = mBluetoothGatt.writeCharacteristic(characteristic);
Log.e(TAG, "Service :"+status);
return status;
}
日志: -
11-01 23:39:32.289 1220-1255/com.example.sagarch.bluetooth_test_4 D/BluetoothGatt﹕ setCharacteristicNotification() - uuid: 0000fff2-0000-1000-8000-00805f9b34fb enable: true
11-01 23:39:32.289 1220-1220/com.example.sagarch.bluetooth_test_4 E/ViewRootImpl﹕ sendUserActionEvent() mView == null
11-01 23:39:32.289 1220-1255/com.example.sagarch.bluetooth_test_4 W/BluetoothGatt﹕ Unhandled exception in callback
java.lang.NullPointerException
at com.example.sagarch.bluetooth_test_4.PollingTest$3.writeCharacteristic(PollingTest.java:451)
at com.example.sagarch.bluetooth_test_4.PollingTest$3.onServicesDiscovered(PollingTest.java:359)
at android.bluetooth.BluetoothGatt$1.onSearchComplete(BluetoothGatt.java:299)
at android.bluetooth.IBluetoothGattCallback$Stub.onTransact(IBluetoothGattCallback.java:215)
at android.os.Binder.execTransact(Binder.java:404)
at dalvik.system.NativeStart.run(Native Method)
11-01 23:39:32.369 1220-1220/com.example.sagarch.bluetooth_test_4 D/ProgressBar﹕ updateDrawableBounds: left = 0
11-01 23:39:32.369 1220-1220/com.example.sagarch.bluetooth_test_4 D/ProgressBar﹕ updateDrawableBounds: top = 0
11-01 23:39:32.369 1220-1220/com.example.sagarch.bluetooth_test_4 D/ProgressBar﹕ updateDrawableBounds: right = 48
11-01 23:39:32.369 1220-1220/com.example.sagarch.bluetooth_test_4 D/ProgressBar﹕ updateDrawableBounds: bottom = 48
11-01 23:39:43.239 1220-1255/com.example.sagarch.bluetooth_test_4 D/BluetoothGatt﹕ onClientConnectionState() - status=0 clientIf=5 device=8C:DE:52:2C:65:B2
11-01 23:39:43.239 1220-1255/com.example.sagarch.bluetooth_test_4 D/Tag﹕ Connection State Change: 0 -> Disconnected
11-01 23:39:43.239 1220-1255/com.example.sagarch.bluetooth_test_4 D/Tag﹕ Connection State Change: 0 -> Disconnected
11-01 23:39:43.239 1220-1255/com.example.sagarch.bluetooth_test_4 W/BluetoothGatt﹕ Unhandled exception in callback
android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:7099)
at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:1076)
at android.view.View.requestLayout(View.java:17449)
at android.view.View.setFlags(View.java:9749)
at android.view.View.setVisibility(View.java:6298)
at android.app.Dialog.hide(Dialog.java:302)
at com.example.sagarch.bluetooth_test_4.PollingTest$3.onConnectionStateChange(PollingTest.java:322)
at android.bluetooth.BluetoothGatt$1.onClientConnectionState(BluetoothGatt.java:172)
at android.bluetooth.IBluetoothGattCallback$Stub.onTransact(IBluetoothGattCallback.java:71)
at android.os.Binder.execTransact(Binder.java:404)
at dalvik.system.NativeStart.run(Native Method)
11-01 23:41:32.579 1220-1220/com.example.sagarch.bluetooth_test_4 D/BluetoothAdapter﹕ stopLeScan()
11-01 23:41:32.589 1220-1220/com.example.sagarch.bluetooth_test_4 D/BluetoothGatt﹕ close()
11-01 23:41:32.589 1220-1220/com.example.sagarch.bluetooth_test_4 D/BluetoothGatt﹕ unregisterApp() - mClientIf=5
我正在尝试为public UUID CONFIG_DESCRIPTOR = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
启用通知。这个UUID有问题还是我的代码还有其他问题。请帮我。我得到NullPointer异常
谢谢你们。
编辑:
从onServiceDiscovered()
调用writeCharacterisiticpublic void onServicesDiscovered(BluetoothGatt gatt, int status) {
//mHandler.sendMessage(Message.obtain(null, MSG_PROGRESS, "Enabling Sensors..."));
/*
* With services discovered, we are going to reset our state machine and start
* working through the sensors we need to enable
*/
if (status == BluetoothGatt.GATT_SUCCESS) {
try {
servicesList = gatt.getServices();
for (BluetoothGattService service : servicesList) {
List<BluetoothGattCharacteristic> characteristics = service.getCharacteristics();
}
writeCharacteristic(gatt);
} catch (InterruptedException e) {
e.printStackTrace();
}
} else {
Log.w(TAG, "onServicesDiscovered received: " + status);
}
}
答案 0 :(得分:0)
使用以下代码更改您的代码:
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
//mHandler.sendMessage(Message.obtain(null, MSG_PROGRESS, "Enabling Sensors..."));
/*
* With services discovered, we are going to reset our state machine and start
* working through the sensors we need to enable
*/
if (status == BluetoothGatt.GATT_SUCCESS) {
try {
servicesList = gatt.getServices();
for (BluetoothGattService service : servicesList) {
List<BluetoothGattCharacteristic> characteristics = service.getCharacteristics();
}
runOnUiThread(new Runnable() {
@Override
public void run() {
writeCharacteristic(gatt);
}
});
} catch (InterruptedException e) {
e.printStackTrace();
}
} else {
Log.w(TAG, "onServicesDiscovered received: " + status);
}
}