微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

BLE 程序中的空对象引用

如何解决BLE 程序中的空对象引用

我是 BLE android 的新手……我指的是图书馆 SimpleBle。现在的问题是它显示空对象引用。

  java.lang.NullPointerException: Attempt to invoke virtual method 'android.bluetooth.BluetoothGattCharacteristic android.bluetooth.BluetoothGattService.getCharacteristic(java.util.UUID)' on a null object reference
    at com.example.trysimpleagain.utils.BluetoothLEHelper.write(BluetoothLEHelper.java:133)
    at com.example.trysimpleagain.MainActivity.lambda$listenerButtons$5$MainActivity(MainActivity.java:192)
    at com.example.trysimpleagain.-$$Lambda$MainActivity$lrXzgvPUvAdUlXR4jkWo791A__4.onClick(UnkNown Source:2)

这是我的 MainActivity 中显示代码

 btnWrite.setonClickListener(v -> {
        if(ble.isConnected()) {
            byte[] aBytes = "Start".getBytes();;
            ble.write("40000201-1fb5-459e-8fcc-c5c9c000014b","beb50000-36e1-4688-b7f5-ea07361b0000",aBytes);
        }
    });

这是来自 BluetoothHelper 类的代码

 public void write(String service,String characteristic,byte[] aBytes){

    BluetoothGattCharacteristic mBluetoothGattCharacteristic;

    mBluetoothGattCharacteristic = mBluetoothGatt.getService(UUID.fromString(service)).getCharacteristic(UUID.fromString(characteristic));
    mBluetoothGattCharacteristic.setValue(aBytes);

    mBluetoothGatt.writeCharacteristic(mBluetoothGattCharacteristic);
}

提前致谢。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。