-1

我正尝试在Android应用程序中执行ble写入微处理器,并发生了一些奇怪的事情。编写空指针异常特征Android

要启动,在日志中误差小于:

Unhandled exception in callback 
                        java.lang.NullPointerException 
                         at android.bluetooth.BluetoothGatt.writeCharacteristic(BluetoothGatt.java:874) 
                         at com.example.tannerhenry.standalone.ConfigurationActivity.setWriteSensor(ConfigurationActivity.java:248) 
                         at com.example.tannerhenry.standalone.ConfigurationActivity.access$100(ConfigurationActivity.java:31) 
                         at com.example.tannerhenry.standalone.ConfigurationActivity$4.onServicesDiscovered(ConfigurationActivity.java:289) 
                         at android.bluetooth.BluetoothGatt$1.onSearchComplete(BluetoothGatt.java:295) 
                         at android.bluetooth.IBluetoothGattCallback$Stub.onTransact(IBluetoothGattCallback.java:215) 
                         at android.os.Binder.execTransact(Binder.java:404) 
                         at dalvik.system.NativeStart.run(Native Method) 

了一下,我注意到,我试图写入特性不被发现了经过调试,因此解释了为什么发生空指针异常。

问题是该特性肯定是广告,因为我在我的iPhone上下载了LightBlue并且出现了该特性。这让我相信我在初始化特性时犯了一个错误,但是在我从应用商店下载了蓝牙LE扫描仪后,它仍然和我的应用程序一样。

iOS和android getCharacteristics的方式有差异吗?

注:我以前有过这个问题,解决方案是重新下载应用程序几次,它最终读取的特点。但是我在平板电脑上做了一个清晰的数据,现在它和以前一样做了,重新下载并没有解决这个问题。

任何帮助将不胜感激,我可以提供更多的代码,如果有必要。

+0

您不应创建自己的BluetoothGattCharacteristic对象,而是先通过执行gatt.discoverServices()获取它们,然后在onServicesDiscovered回调中使用gatt.getService(uuid)或gatt.getServices()来获取服务。然后,您可以使用service.getCharacteristic(uuid)或service.getCharacteristics()来获取您的BluetoothGattCharacteristic对象。 – Emil

+0

是的,我在我的onConnectionStateChange回调的连接状态中调用gatt.discoverServices,所以这不是问题。你知道我可以投入服务的特性有多少? @Emil – thenry

+0

除了gatt db的句柄不能超过0xffff之外,没有限制。 – Emil

回答

0

该问题已通过手动遗忘先前配对的设备并修复而得到解决。