Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/189.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 回调中未处理的异常_Android_Bluetooth - Fatal编程技术网

Android 回调中未处理的异常

Android 回调中未处理的异常,android,bluetooth,Android,Bluetooth,我正在开发一个Android应用程序,我需要连接到BLE医疗设备(这里是血压)并传输测得的血压。我正在升级代码。例如,我有蓝牙2.0代码,成功连接到BP设备,成功写入和读取设备中的值 但在BLE中,我能够成功地写入值(这里BP设备要求写入特定值以开始测量值),并且当我尝试启用通知时,我会收到空指针异常 writeCharacteristic方法的代码: public boolean writeCharacteristic(BluetoothGatt mBluetoothGatt) throws

我正在开发一个Android应用程序,我需要连接到BLE医疗设备(这里是血压)并传输测得的血压。我正在升级代码。例如,我有蓝牙2.0代码,成功连接到BP设备,成功写入和读取设备中的值

但在BLE中,我能够成功地写入值(这里BP设备要求写入特定值以开始测量值),并且当我尝试启用通知时,我会收到空指针异常

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;
    }
日志:-


我正在尝试为
public UUID CONFIG_DESCRIPTOR=UUID.fromString(“00001101-0000-1000-8000-00805F9B34FB”)启用通知。这个UUID是否有问题,或者我的代码是否有其他问题。请帮帮我。我得到一个空指针异常

谢谢大家

编辑:

从onServiceDiscovered()调用WriteCharacteristic

发现服务上的公共无效(蓝牙gatt、int状态){
//mHandler.sendMessage(Message.get(null,MSG_PROGRESS,“启用传感器…”);
/*
*发现服务后,我们将重置状态机并启动
*通过我们需要启用的传感器工作
*/
如果(状态==蓝牙GATT.GATT\U成功){
试一试{
servicesList=gatt.getServices();
for(BluetoothGattService服务:服务列表){
列表特征=service.getCharacteristics();
}
书面特征(关贸总协定);
}捕捉(中断异常e){
e、 printStackTrace();
}
}否则{
Log.w(标记“OnServicesDiscovery已接收:+状态);
}
}

使用此代码更改您的代码:

    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);
        }
    }
发现服务上的公共无效(蓝牙gatt、int状态){
//mHandler.sendMessage(Message.get(null,MSG_PROGRESS,“启用传感器…”);
/*
*发现服务后,我们将重置状态机并启动
*通过我们需要启用的传感器工作
*/
如果(状态==蓝牙GATT.GATT\U成功){
试一试{
servicesList=gatt.getServices();
for(BluetoothGattService服务:服务列表){
列表特征=service.getCharacteristics();
}
runOnUiThread(新的Runnable(){
@凌驾
公开募捐{
书面特征(关贸总协定);
}
});
}捕捉(中断异常e){
e、 printStackTrace();
}
}否则{
Log.w(标记“OnServicesDiscovery已接收:+状态);
}
}

从何处调用writeCharacteristic()方法,是否在线程内调用它更新了我的问题。不,我想我不是从单独的线程中调用它。我不知道怎么做。这是否会影响回调方法?您的代码中存在与线程相关的问题,您是从错误的线程调用代码,您需要首先识别调用线程并将相关信息发布到另一个线程(可能是UI线程)。更改为上述代码。有一个错误,使关贸总协定变量最终,我改变了和获取致命错误。日志:-
14400-14400/com.example.sagarch.bluetooth\u test\u 4 E/AndroidRuntime﹕ 致命异常:主进程:com.example.sagarch.bluetooth_test_4,PID:14400 java.lang.NullPointerException at com.example.sagarch.bluetooth_test_4.PollingTest$3.writeCharacteristic(PollingTest.java:416)at com.example.sagarch.bluetooth_test_4.PollingTest$3.run(PollingTest.java:351)在android.os.Handler.handleCallback(Handler.java:733)
请帮助我
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();
                }
                writeCharacteristic(gatt);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        } else {
            Log.w(TAG, "onServicesDiscovered received: " + status);
        }
    }
    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);
        }
    }