Android Question BLE SetNotify crashes program.

fjb

Member
Licensed User
Longtime User
I am trying to use the setnotify service on a connected arduino. I am able to make a connection, send and receive data but when I try to use setNotify I get the below error and the program crashes. I have the characteristic set up with notify enabled I am using the correct service and characteristic UUIDs. Any help is appreciated.


arduino:
BLECharacteristic firstChar(MY_UUID("0001"), BLERead | BLEWrite, 40 | BLENotify);

B4A Code:
Sub Manager_Connected (services As List)
    Log("Connected")
    connected = True
    'ConnectedServices = services
    Sleep(1000)
   169 -  manager.SetNotify(strBcSvcUUID,strBcChrUUID,True)
    StateChanged

End Sub

error:
connecting
Discovering services.
Discovering services.
Connected
Connected
Error occurred on line: 169 (B4XLoadingIndicator)
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.bluetooth.BluetoothGattDescriptor.setValue(byte[])' on a null object reference
    at anywheresoftware.b4a.objects.BleManager2.setNotify(BleManager2.java:343)
    at anywheresoftware.b4a.objects.BleManager2.SetNotify(BleManager2.java:332)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runVoidMethod(Shell.java:777)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:354)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:146)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:193)
    at anywheresoftware.b4a.shell.DebugResumableSub$RemoteResumableSub.resume(DebugResumableSub.java:22)
    at anywheresoftware.b4a.keywords.Common$13.run(Common.java:1716)
    at android.os.Handler.handleCallback(Handler.java:938)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loopOnce(Looper.java:226)
    at android.os.Looper.loop(Looper.java:313)
    at android.app.ActivityThread.main(ActivityThread.java:8751)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:571)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1135)
 

fjb

Member
Licensed User
Longtime User
Figured this one out but created more problems for myself.
You have to use BLEByteCharacteristic not BLECharacteristic with the arduino arduinoBle library for the notify to be available. I was counting on using BLECharacteristic because you can send up to 512 bytes at a time. So know it back to the drawing board.
 
Upvote 0

fjb

Member
Licensed User
Longtime User
I discovered with the help from the Arduino forum that I was not setting the BLECharacteristic parameters correctly.
I can indeed read and write large arrays of bytes with one characteristic.

B4X:
incorrect
BLECharacteristic fourthChar(MY_UUID("0004"), BLERead | BLEWrite, 40 | BLENotify);

correct
BLECharacteristic fourthChar(MY_UUID("0004"), BLERead | BLEWrite | BLENotify, 40);
 
Last edited:
Upvote 0

fjb

Member
Licensed User
Longtime User
Got it mostly working well. Now that the data is moving like it should I can see that I can send bytes and receive bytes no magical data conversion going on. It is great I can send 50 bytes from the arduino to the phone in one characteristic. I can also send 5 bytes that are set to notify in another. At first glance I thought I was going to have to setup 50 characteristics.
Now I just have to figure out how to reference a field (view) from a form (layout) that is loaded in a scrollview in another layout that has it's own module with the data first received from the arduino. I do have something working but I don't understand why.... I just know it is going to breakdown soon. Some things are much easier to accomplish in C#. You can go down a lot of rabbit holes searching for b4x stuff.
 
Upvote 0

emexes

Expert
Licensed User
Sounds like the BLE device is more than just a wireless light or motor switch. Global variables, here we come! šŸ™ƒ
 
Upvote 0
Top