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

B4X:
DataAvailable
Service=6e400001-b5a3-f393-e0a9-e50e24dcca9e
Characteristics=(MyMap) {6e400003-b5a3-f393-e0a9-e50e24dcca9e=[[email protected], 6e400002-b5a3-f393-e0a9-e50e24dcca9e=[[email protected]}
Service UART
char: 6e400003-b5a3-f393-e0a9-e50e24dcca9e, value = 
char: 6e400003-b5a3-f393-e0a9-e50e24dcca9e, value = [[email protected]
char: 6e400002-b5a3-f393-e0a9-e50e24dcca9e, value = 
char: 6e400002-b5a3-f393-e0a9-e50e24dcca9e, value = [[email protected]
 

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

fishwolf

Well-Known Member
Licensed User
Longtime User
have you tried to convert the bytearray in there to a String?

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

B4X:
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

B4X:
DataAvailable
Service=6e400001-b5a3-f393-e0a9-e50e24dcca9e
Characteristics=(MyMap) {6e400003-b5a3-f393-e0a9-e50e24dcca9e=[[email protected], 6e400002-b5a3-f393-e0a9-e50e24dcca9e=[[email protected]}
Service UART

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

Key: 6e400002-b5a3-f393-e0a9-e50e24dcca9e, value: 
string:
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

fishwolf

Well-Known Member
Licensed User
Longtime User
it's the same value showed in different mode, but nothing show the real data

B4X:
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

fishwolf

Well-Known Member
Licensed User
Longtime User
This means that the arrays of bytes are empty.

You can add this to check it:
B4X:
Log("Length: " & b.Length)
Length is zero

adafruit send "123" string each 1 second, the original app work fine
i don't understand :(
 
Upvote 0

f0raster0

Well-Known Member
Licensed User
Longtime User
Try setting notify true:
SetNotify(6e400001-b5a3-f393-e0a9-e50e24dcca9e, 6e400003-b5a3-f393-e0a9-e50e24dcca9e, true)
SetNotify(6e400001-b5a3-f393-e0a9-e50e24dcca9e, 6e400002-b5a3-f393-e0a9-e50e24dcca9e, true)
 
Upvote 0

fishwolf

Well-Known Member
Licensed User
Longtime User
i have a error on SetNotify

B4X:
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

B4X:
Found: Adafruit Bluefruit LE, FE:9F:D2:DA:96:33, RSSI = -70, (MyMap) {1=[[email protected], 10=[[email protected], 6=[[email protected], 9=[[email protected], 0=[[email protected]}
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

f0raster0

Well-Known Member
Licensed User
Longtime User
better to test, create a new button
then only after you see your device connected , click the new button


B4X:
Sub Button1_Click
        manager.SetNotify("6e400001-b5a3-f393-e0a9-e50e24dcca9e", "6e400002-b5a3-f393-e0a9-e50e24dcca9e", True)
End Sub
 
Last edited:
Upvote 0

techknight

Well-Known Member
Licensed User
Longtime User
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:
B4X:
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

fishwolf

Well-Known Member
Licensed User
Longtime User
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:
B4X:
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

techknight

Well-Known Member
Licensed User
Longtime User
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

fishwolf

Well-Known Member
Licensed User
Longtime User
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

techknight

Well-Known Member
Licensed User
Longtime User
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

fishwolf

Well-Known Member
Licensed User
Longtime User
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
Top