bluetooth le gatt客户端和服务器

时间:2018-05-17 13:50:52

标签: android bluetooth-lowenergy

我一直试图让一个android的gatt客户端与android的gatt服务器交谈。

我一直在关注此博客讨论的例子:

https://www.bignerdranch.com/blog/bluetooth-low-energy-part-1/

博客的源代码在这里:

https://github.com/bignerdranch/android-bluetooth-testbed

示例应用可以充当gatt客户端或服务器。当我启动服务器时,它正确地通告外围设备,客户端扫描并找到服务器。当我按下" connect"时,会调用GattServerCallback的onCharacteristicReadRequest,并尝试在GattServerCallback.java的第52行向客户端发送响应:

https://github.com/bignerdranch/android-bluetooth-testbed/blob/master/BluetoothLowEnergy/app/src/main/java/com/bignerdranch/android/bluetoothtestbed/server/GattServerCallback.java

此调用因空指针错误而失败:

17 14:44:43.358 17888-17925/com.bignerdranch.android.bluetoothlowenergy W/BluetoothGattServer: Unhandled exception in callback
    java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.Integer.intValue()' on a null object reference
        at android.os.Parcel.readException(Parcel.java:1699)
        at android.os.Parcel.readException(Parcel.java:1646)
        at android.bluetooth.IBluetoothGatt$Stub$Proxy.sendResponse(IBluetoothGatt.java:1424)
        at android.bluetooth.BluetoothGattServer.sendResponse(BluetoothGattServer.java:599)
        at com.bignerdranch.android.bluetoothtestbed.server.ServerActivity.sendResponse(ServerActivity.java:315)
        at com.bignerdranch.android.bluetoothtestbed.server.GattServerCallback.onDescriptorWriteRequest(GattServerCallback.java:115)
        at android.bluetooth.BluetoothGattServer$1.onDescriptorWriteRequest(BluetoothGattServer.java:261)
        at android.bluetooth.IBluetoothGattServerCallback$Stub.onTransact(IBluetoothGattServerCallback.java:263)
        at android.os.Binder.execTransact(Binder.java:573)

在异常之后,客户端断开连接并调用onConnectionStateChange方法,并在第34到36行删除客户端设备:

else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
        mServerActionListener.removeDevice(device);
}

客户端和服务器之间无法进一步通信。在客户端上有一个文本框,允许您向服务器发送消息,但没有消息通过服务器,可能是因为断开连接。

我已经汇总了一个示例,该示例在ble-echo的github上生成相同的异常。

有谁可以解释为什么这个例子不起作用?

0 个答案:

没有答案