Search code examples
javaandroidnullpointerexceptionbluetooth-lowenergycharacteristics

Null pointer exception on writeCharacteristic Android


I am trying to perform a ble write to a microprocessor on an android application and something weird is happening.

To start, the log for the error is below:

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)

After a debugging for a bit I noticed that the characteristic that I am trying to write to isn't being discovered and therefore explains why the null pointer exception occurs.

The problem is that the characteristic is definitely being advertised because I downloaded LightBlue on my iPhone and the characteristic appears. This brings me to believe that I have made a mistake in initializing characteristics but I after I downloaded Bluetooth LE scanner from the app store, it still acts the same way as my app.

Is there a difference in the way iOS and android getCharacteristics?

Note: I have had this issue before, the solution was re-downloading the app a few times and it eventually read the characteristic. But I did a data clear on my tablet and now it is doing the same thing as before and re-downloading isn't fixing it this time.

Any help would be appreciated and I can provide more code if necessary.


Solution

  • The problem was fixed by manually forgetting the previously paired device and repairing.