Android Question BLE2 Library problem

arge06

Member
Licensed User
Hi everybody,

Firstly I wish you to have a very good year. I'm using a ESP32 BLE module, I tried module with some applications and it's working as expected, when I send "A" or 0x41 to module, the led turns on and when I press the button connected to the module, it sends "B" to the application. With this library and example I couldn't manage to send and receive data with my module. Which commands must I use for this ?

Many thanks in advance.
 

arge06

Member
Licensed User
I got the service data from ble app,

Service : "6E400001-B5A3-F393-E0A9-E50E24DCCA9E"

Tx : "6E400002-B5A3-F393-E0A9-E50E24DCCA9E"

Rx : "6E400003-B5A3-F393-E0A9-E50E24DCCA9E"


I just wanna know, how to send "A" char to my module from my phone.

When module receives "A", it turns on led and "B" then the led goes off.
 
Upvote 0

arge06

Member
Licensed User
Definition in module (Coded with Arduino IDE):

#define SERVICE_UUID "6E400001-B5A3-F393-E0A9-E50E24DCCA9E" // UART service UUID
#define CHARACTERISTIC_UUID_RX "6E400002-B5A3-F393-E0A9-E50E24DCCA9E"
#define CHARACTERISTIC_UUID_TX "6E400003-B5A3-F393-E0A9-E50E24DCCA9E"

I'm sending data with code:

Sub Send_Data

Dim hb(3)As Byte
hb(0)=65
hb(1)=43
hb(2)=10

manager.writeData("6E400001-B5A3-F393-E0A9-E50E24DCCA9E","6E400002-B5A3-F393-E0A9-E50E24DCCA9E",hb)

End Sub


Log says:

Found: ESP32 BLE DENEME, 3C:71:BF:7D:05:9A, RSSI = -60, (MyMap) {1=[B@8f80b14, 18=[B@a74ffbd, 9=[B@2eaa9b2,
Discovering services.
Discovering services.
Discovering services.
Connected
Connected
Connected
starter_send_data (java line: 276)
java.lang.RuntimeException: Service not found
at anywheresoftware.b4a.objects.BleManager2.getService(BleManager2.java:385)
at anywheresoftware.b4a.objects.BleManager2.WriteData(BleManager2.java:338)
at b4a.bbb.starter._send_data(starter.java:276)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:196)
at anywheresoftware.b4a.keywords.Common.CallSub4(Common.java:1083)
at anywheresoftware.b4a.keywords.Common.CallSubNew(Common.java:1030)
at b4a.bbb.main._write_click(main.java:630)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:196)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:180)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:176)
at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:80)
at android.view.View.performClick(View.java:6320)
at android.view.View$PerformClick.run(View.java:25087)
at android.os.Handler.handleCallback(Handler.java:869)
at android.os.Handler.dispatchMessage(Handler.java:101)
at android.os.Looper.loop(Looper.java:206)
at android.app.ActivityThread.main(ActivityThread.java:6749)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:845)

And app closes. :(
 
Upvote 0

emexes

Expert
Licensed User
Log the discovered device, service and characteristic uuids.

Also, you seem to be doing the WriteData to the RX uuid. That doesn't feel right.
 
Upvote 0

arge06

Member
Licensed User
Thank you for the answer, I tried with both "6E400002-B5A3-F393-E0A9-E50E24DCCA9E" and "6E400003-B5A3-F393-E0A9-E50E24DCCA9E" but program closes. Tryed in 3 different phones. BLE test programs can send data to my module with same UUID s.
 
Upvote 0

emexes

Expert
Licensed User
The error is:

Service not found

which suggests the WriteData is to a service that is not in the list returned by the Connected event.

Log that entire service list from the Connected event, and also log the Service and Characteristic parameters sent to WriteData.
 
Upvote 0

arge06

Member
Licensed User
The example program is giving the service values but with same values I can't send data. Any help would be appreciated, I'll be pleased.
 

Attachments

  • Screenshot_2020-01-04-20-52-34-119_b4a.bbb.png
    Screenshot_2020-01-04-20-52-34-119_b4a.bbb.png
    136.2 KB · Views: 64
Last edited:
Upvote 0

f0raster0

Well-Known Member
Licensed User
********* Write data to your BLE Module**********
Look this:
B4X:
manager.WriteData("service id", "char id", HMString.GetBytes("UTF8")) 'replace the ids
in this link to send data to BLE modules

In your code write, It should be something like that: (I use 1 and 2 to turn on/off the led.
B4X:
Public Sub WriteON

    Dim HMString As String

    HMString = "1" & CRLF ' 20 characters max

    manager.WriteData("6e400001-b5a3-f393-e0a9-e50e24dcca9e", "6e400002-b5a3-f393-e0a9-e50e24dcca9e", HMString.GetBytes("UTF8"))

End Sub

********* Read data in your App **********
Look this:
https://www.b4x.com/android/forum/threads/ble2-setnotify.74421/#post-409128
look this in that link:
B4X:
Dim s As String = "0000dfb0-0000-1000-8000-00805f9b34fb"
Dim c As String = "0000dfb1-0000-1000-8000-00805f9b34fb"
Log("Properties: " & manager.GetCharacteristicProperties(s, c))
Log("a")
SetNotify(s, c, false)
Log("b")
SetNotify(s, c, True)

To read data, notify should be true, in your code, it should be something like that:
B4X:
    manager.SetNotify("6e400001-b5a3-f393-e0a9-e50e24dcca9e","6e400003-b5a3-f393-e0a9-e50e24dcca9e",True)
    Log("Set Notify")

Edit:
*** Note ***
Make sure your App is really connected to your Module before sending any command.
 
Last edited:
Upvote 0

emexes

Expert
Licensed User
The example program is giving the service values but with same values I can't send data. Any help would be appreciated, I'll be pleased.
I've always used the UUID strings returned by eg .Connected event. I see in your screenshot that they have lowercase letters, but in your code samples they are uppercase.

Perhaps I've been obliviously dodging a bullet all this time, thanks to my boring coding style :cool:

Anyway, try:

1/ change the UUID's to lowercase
2/ even better, grab the UUID from the .Connected list of services, eg:
B4X:
'distilled from working code ;-)

Sub Process_Globals
    Dim PressureDevice As String = ""    'empty string = device not yet found
    Dim PressureService As String = ""    'empty string = device not yet connected
    Dim PressureCharacteristic As String = ""
End Sub

Sub Manager_Connected(Services As List)
    For Each S As String In Services
        If S.StartsWith("cc4a6a80") Then    'something that identifies the desired service from all the others
            PressureService = S
            manager.ReadData(PressureService)    'I understand you're writing, not reading, but same principle applies: use UUID string supplied by BLE2
        End If
    Next
End Sub
 
Last edited:
Upvote 0
Top