Android Question Uart Ble Adafruit

fishwolf

Well-Known Member
Licensed User
Longtime User
I have a Adafruit Feather M0 with a simple chat application, send and receive text with UART.
with test app of Adafruit work fine.

I'm connecting to device with b4a and ble with success
but i cannot read data.

i have a Adafruit that send "123" each a seconds and try to read service 6e400001 and charateristics 6e400003 and 6e400002

what is the correct way for read and write uart data?

thanks

Code: Copy
DataAvailable
Service=6e400001-b5a3-f393-e0a9-e50e24dcca9e
Characteristics=(MyMap) {6e400003-b5a3-f393-e0a9-e50e24dcca9e=[B@1f63311, 6e400002-b5a3-f393-e0a9-e50e24dcca9e=[B@9891c76}
Service UART
char: 6e400003-b5a3-f393-e0a9-e50e24dcca9e, value = 
char: 6e400003-b5a3-f393-e0a9-e50e24dcca9e, value = [B@1f63311
char: 6e400002-b5a3-f393-e0a9-e50e24dcca9e, value = 
char: 6e400002-b5a3-f393-e0a9-e50e24dcca9e, value = [B@9891c76
 
Upvote 0
have you tried to convert the bytearray in there to a String?

in this mode?
i see the same problem.....

Code: Copy
Sub DataAvailable (Service As String, Characteristics As Map)
   
    Log("----------------------------------------")
    Log("DataAvailable")
    Log("Service=" & Service)
    Log("Characteristics=" & Characteristics)
   
    If Service.StartsWith("6e400001") == True Then
        Log ("Service UART")   
    End If
 
    Dim bc As ByteConverter
    For Each key As String In Characteristics.Keys
        Dim b() As Byte = Characteristics.Get(key)
        Log (" ")
        Log("Key: " & key &", value: " & bc.HexFromBytes(b))
        Log("string: " & BytesToString(b, 0, b.Length, "utf8") )'useful if the data is actually a string
    Next
   
End Sub

Code: Copy
DataAvailable
Service=6e400001-b5a3-f393-e0a9-e50e24dcca9e
Characteristics=(MyMap) {6e400003-b5a3-f393-e0a9-e50e24dcca9e=[B@55adbcb, 6e400002-b5a3-f393-e0a9-e50e24dcca9e=[B@a99fa8}
Service UART

Key: 6e400003-b5a3-f393-e0a9-e50e24dcca9e, value: 
string: 

Key: 6e400002-b5a3-f393-e0a9-e50e24dcca9e, value: 
string:
 
Upvote 0
char: 6e400003-b5a3-f393-e0a9-e50e24dcca9e, value = [B@1f63311
here the value should contain something
Key: 6e400003-b5a3-f393-e0a9-e50e24dcca9e, value:
string:

Key: 6e400002-b5a3-f393-e0a9-e50e24dcca9e, value:
string:
here the value is empty.

What did you tried above where the value was not empty?
 
Upvote 0
it's the same value showed in different mode, but nothing show the real data

Code: Copy
Log("Key: " & key &", value: " & bc.HexFromBytes(b))
        Log("string: " & BytesToString(b, 0, b.Length, "utf8") )'useful if the data is actually a string

what is the right mode for read the data?
 
Last edited:
Upvote 0
i have a error on SetNotify

Code: Copy
Sub Manager_DeviceFound (Name As String, Id As String, AdvertisingData As Map, RSSI As Double)
    Log("Found: " & Name & ", " & Id & ", RSSI = " & RSSI & ", " & AdvertisingData)
    If Name = "Adafruit Bluefruit LE" Then
        Log ("Device Found")
        ConnectedName = Name
        manager.StopScan
        manager.Connect2(Id, False) 'disabling auto connect can make the connection quicker
       
        'Sleep(1000)
       
        manager.SetNotify("6e400001-b5a3-f393-e0a9-e50e24dcca9e", "6e400003-b5a3-f393-e0a9-e50e24dcca9e", True)
        manager.SetNotify("6e400001-b5a3-f393-e0a9-e50e24dcca9e", "6e400002-b5a3-f393-e0a9-e50e24dcca9e", True)

    End If    
End Sub

Code: Copy
Found: Adafruit Bluefruit LE, FE:9F:D2:DA:96:33, RSSI = -70, (MyMap) {1=[B@e592bb4, 10=[B@880a3dd, 6=[B@aa14b52, 9=[B@f0c3e23, 0=[B@5374f20}
Device Found
Error occurred on line: 72 (Starter)
java.lang.RuntimeException: No device connected
    at anywheresoftware.b4a.objects.BleManager2.getService(BleManager2.java:380)
    at anywheresoftware.b4a.objects.BleManager2.setNotify(BleManager2.java:302)
    at anywheresoftware.b4a.objects.BleManager2.SetNotify(BleManager2.java:299)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runVoidMethod(Shell.java:755)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:345)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:249)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:139)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:166)
    at anywheresoftware.b4a.shell.DebugResumableSub$RemoteResumableSub.resume(DebugResumableSub.java:19)
    at anywheresoftware.b4a.keywords.Common$13.run(Common.java:1680)
    at android.os.Handler.handleCallback(Handler.java:836)
    at android.os.Handler.dispatchMessage(Handler.java:103)
    at android.os.Looper.loop(Looper.java:208)
    at android.app.ActivityThread.main(ActivityThread.java:6267)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1063)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:924)
 
Upvote 0
You need to put the SetNotify in the Connected event. Also Nordic nRF the class characteristic 3 is the listener, not 2.

in my case, this works:
Code: Copy
Sub Manager_Connected (services As List)
    Log("Connected")
    connected = True
    ConnectedServices = services
    manager.SetNotify("6E400001-B5A3-F393-E0A9-E50E24DCCA9E".ToLowerCase, "6E400003-B5A3-F393-E0A9-E50E24DCCA9E".ToLowerCase, True)
    CallSub(Main, "StateChanged")
End Sub
 
Upvote 0
You need to put the SetNotify in the Connected event. Also Nordic nRF the class characteristic 3 is the listener, not 2.

in my case, this works:
Code: Copy
Sub Manager_Connected (services As List)
    Log("Connected")
    connected = True
    ConnectedServices = services
    manager.SetNotify("6E400001-B5A3-F393-E0A9-E50E24DCCA9E".ToLowerCase, "6E400003-B5A3-F393-E0A9-E50E24DCCA9E".ToLowerCase, True)
    CallSub(Main, "StateChanged")
End Sub

perfect, work fine

i now send 24 bytes, but i receive 2 events, the first event contain first 20 bytes, the second event contain last 4 bytes

i send as unique buffer from device
is it possible reveice all bytes in a unique event?

thanks
 
Upvote 0
You have to edit the firmware to change the ATT MTU size. Which means getting the source code, a SEGGER programmer, and the SEGGER Studio. As well as the SDK and the UART example so you can mod it.
 
Upvote 0
You have to edit the firmware to change the ATT MTU size. Which means getting the source code, a SEGGER programmer, and the SEGGER Studio. As well as the SDK and the UART example so you can mod it.
thanks, it's more simple manager buffer with start and end symbol :)
 
Upvote 0
Yes, but you will just be transmitting multiple packets. So as long as you dont need alot of throughput, that may work fine.

But if your like me, in my case, I need 100ms packet timings on throughput so I had to mod the firmware to extend the packet length to 127 bytes.
 
Upvote 0
Yes, but you will just be transmitting multiple packets. So as long as you dont need alot of throughput, that may work fine.

But if your like me, in my case, I need 100ms packet timings on throughput so I had to mod the firmware to extend the packet length to 127 bytes.

good, i see that you are very expert
i have a little curiosity
the adafruit device firmware wait the connection in "setup" function, after set the parameters
in "loop" function send only the data each 1 second.
all work fine, also when i reconnect with device without poweroff

i don't understand why is not necessary manage the disconnect event on device and return in wait the connection?
 
Upvote 0
Back
Top