Android Question B4A can't cpnnect with BLE module with UART

Alfonso Hermida

Member
Licensed User
I have a module made by Taiyo Yuden based on the NRF51822. I comes programmed with a UART application so the only thing needed is to connect the TxRx to an Android board and run a simple program. I can connect to it with the Taiyo Yuden's terminal Android app, with nRF Connect, with nRF UART and send text back and forth. With B4A I can see the module and get all the info, services etc. When I try to connect to it it doesn't. I'm using the BLEwrite demo application that has 2 scroll bars. I've used the same application with other BLE modules without a problem.

How would you start debugging something like this?

Thanks!
 

Alfonso Hermida

Member
Licensed User
That's the library and example I've been using. I can see the device and the info but when I use the BLEwrite demo (the one with the scroll bars) it doesn't work.
 
Upvote 0

Alfonso Hermida

Member
Licensed User
Here's the log

B4X:
Logger connected to: c8d17689
--------- beginning of main
** Service (service1) Start **
** Service (service1) Destroy **
*** Service (service1) Create ***
** Service (service1) Start **
** Service (service1) Destroy **
*** Service (service1) Create ***
** Service (service1) Start **
** Activity (main) Pause, UserClosed = false **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = true **
--------- beginning of crash
** Activity (main) Create, isFirst = false **
** Activity (main) Resume **
** Service (service1) Destroy **
*** Service (service1) Create ***
** Service (service1) Start **
** Service (service1) Destroy **
** Activity (main) Pause, UserClosed = true **
--------- beginning of system
Copying updated assets files (3)
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
Class not found: b4a.example.customlistview, trying: b4a.example3.customlistview
** Activity (main) Resume **
Found: TYSA-B 3.0.0, CB:A7:87:2F:B8:2D, RSSI = -74, (MyMap) {9=[B@55cb1b5, 1=[B@d9b004a, 10=[B@851dbbb, 3=[B@96fb8d8, 0=[B@df3b431}
Discovering services.
Connected!
Log in DataAvailable
Service: 00001801-0000-1000-8000-00805f9b34fb
Log in CreateServiceItem
service: 00001801-0000-1000-8000-00805f9b34fb
Log in DataAvailable
Service: 00001800-0000-1000-8000-00805f9b34fb
Log in CreateServiceItem
service: 00001800-0000-1000-8000-00805f9b34fb
Log in DataAvailable
Characteristic Key: 00002a00-0000-1000-8000-00805f9b34fb Characteristic Item: [B@4de7225
Log in CharacteristicItem
Id: 00002a00-0000-1000-8000-00805f9b34fb
Log in DataAvailable
Characteristic Key: 00002a01-0000-1000-8000-00805f9b34fb Characteristic Item: [B@71c0ffa
Log in CharacteristicItem
Id: 00002a01-0000-1000-8000-00805f9b34fb
Log in DataAvailable
Characteristic Key: 00002a04-0000-1000-8000-00805f9b34fb Characteristic Item: [B@92d29ab
Log in CharacteristicItem
Id: 00002a04-0000-1000-8000-00805f9b34fb
Log in DataAvailable
Service: 442f1570-8a00-9a28-cbe1-e1d4212d53eb
Log in CreateServiceItem
service: 442f1570-8a00-9a28-cbe1-e1d4212d53eb
Log in DataAvailable
Characteristic Key: 442f1571-8a00-9a28-cbe1-e1d4212d53eb Characteristic Item: [B@2f0808
Log in CharacteristicItem
Id: 442f1571-8a00-9a28-cbe1-e1d4212d53eb
Log in DataAvailable
Characteristic Key: 442f1572-8a00-9a28-cbe1-e1d4212d53eb Characteristic Item: [B@6a90ba1
Log in CharacteristicItem
Id: 442f1572-8a00-9a28-cbe1-e1d4212d53eb
Log in DataAvailable
Service: 0000180f-0000-1000-8000-00805f9b34fb
Log in CreateServiceItem
service: 0000180f-0000-1000-8000-00805f9b34fb
Log in DataAvailable
Characteristic Key: 00002a19-0000-1000-8000-00805f9b34fb Characteristic Item: [B@70b31c6
Log in CharacteristicItem
Id: 00002a19-0000-1000-8000-00805f9b34fb
 
Upvote 0

Alfonso Hermida

Member
Licensed User
When I use

B4X:
Sub Service_Create
    manager.Initialize("manager")
    serviceId = "442F1570-8A00-9A28-CBE1-E1D4212D53EB"     
    charId =     "442F1572-8A00-9A28-CBE1-E1D4212D53EB"     // another option was charId =     "442F1571-8A00-9A28-CBE1-E1D4212D53EB"
    Log("Service_Create")
End Sub

The log reads

B4X:
*** Service (starter) Create ***
Service_Create
** Service (starter) Start **
scanning!
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
scanning!
** Activity (main) Pause, UserClosed = true **

And stays scanning, never connecting.
 
Upvote 0

Alfonso Hermida

Member
Licensed User
the program keeps scanning and never finds the BLE device.

Here's the code I used from one of the demos

B4X:
#Region  Service Attributes
    #StartAtBoot: False
    #ExcludeFromLibrary: True
#End Region

Sub Process_Globals
    Public manager As BleManager2
    Public currentStateText As String = "UNKNOWN"
    Private serviceId, charId As String
    Private connected As Boolean
    Public rp As RuntimePermissions
End Sub

Sub Service_Create
    manager.Initialize("manager")
    serviceId = "442F1570-8A00-9A28-CBE1-E1D4212D53EB"     
    charId =    "442F1572-8A00-9A28-CBE1-E1D4212D53EB"     
    Log("Service_Create")
End Sub

Sub Service_Start (StartingIntent As Intent)

End Sub


Sub Manager_StateChanged (State As Int)
    Select State
        Case manager.STATE_POWERED_OFF
            currentStateText = "POWERED OFF"
        Case manager.STATE_POWERED_ON
            currentStateText = "POWERED ON"
        Case manager.STATE_UNSUPPORTED
            currentStateText = "UNSUPPORTED"
    End Select
    CallSub(Main, "BleStateChanged")
    StartScan
End Sub

Public Sub StartScan
    If rp.Check(rp.PERMISSION_ACCESS_COARSE_LOCATION) And manager.State = manager.STATE_POWERED_ON Then
        Log("scanning!")
        manager.Scan2(Array (serviceId), False)
        'manager.Scan2(Null, False)
    Else
        Log("No permission or not powered on")
    End If
    
End Sub


Sub Manager_DeviceFound (Name As String, Id As String, AdvertisingData As Map, RSSI As Double)
    Log("Dev found = " & Name)
    Log($"Device found: ${Name}"$)
    manager.StopScan                 'disabling auto connect can make the connection quicker
    manager.Connect2(Id, False)
    
End Sub


Sub manager_Connected (Services As List)
    Log("connected: " & Services)
    connected = True
End Sub

Sub Manager_Disconnected
    connected = False
    manager.Scan2(Array (serviceId), False)
End Sub


Sub Manager_DataAvailable (sid As String, Characteristics As Map)
    Dim b() As Byte = Characteristics.Get(charId)
    Dim msg As String = BytesToString(b, 0, b.Length, "utf8")
    CallSub2(Main, "DeviceMessage", msg)
End Sub

Public Sub SendMessage(msg() As Byte)
    If Not(connected) Then Return
    manager.WriteData(serviceId, charId, msg)
End Sub

Private Sub UUID(id As String) As String
#if B4A
    Return "0000" & id.ToLowerCase & "-0000-1000-8000-00805f9b34fb"
#else if B4I
    Return id.ToUpperCase
#End If
End Sub



'Return true to allow the OS default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
    Return True
End Sub

Sub Service_Destroy

End Sub
 
Last edited:
Upvote 0

Alfonso Hermida

Member
Licensed User
The code that I posted on my previous message works with many other devices (HM-11, Bluefruit, DA14580 etc) but does not even recognize the device from Taiyo Yuden. In order to gather the info of the Taiyo Yuden BLE device I used the following code which is a demo that I found in this website:

B4X:
#Region  Service Attributes
    #StartAtBoot: False
    #ExcludeFromLibrary: True
#End Region

Sub Process_Globals
    Public manager As BleManager2
    Public currentStateText As String = "UNKNOWN"
    Public currentState As Int
    Public connected As Boolean = False
    Public ConnectedName As String
    Private ConnectedServices As List
    Public Bmsg() As Byte
    Public rp As RuntimePermissions
End Sub

Sub Service_Create
    manager.Initialize("manager")
    Bmsg = "This is a test".GetBytes("UTF8")
End Sub

Sub Service_Start (StartingIntent As Intent)

End Sub

Public Sub ReadData
    For Each s As String In ConnectedServices
        manager.ReadData(s)
        'Log("manager.ReadData(s) = " & s)
    Next
End Sub

Public Sub Disconnect
    manager.Disconnect
    Manager_Disconnected
End Sub

Sub Manager_StateChanged (State As Int)
    Select State
        Case manager.STATE_POWERED_OFF
            currentStateText = "POWERED OFF"
        Case manager.STATE_POWERED_ON
            currentStateText = "POWERED ON"
        Case manager.STATE_UNSUPPORTED
            currentStateText = "UNSUPPORTED"
    End Select
    currentState = State
    CallSub(Main, "StateChanged")
End Sub

Sub Manager_DeviceFound (Name As String, Id As String, AdvertisingData As Map, RSSI As Double)
    If Name <> "TYSA-B 3.0.0" Then Return
    Log("Found: " & Name & ", " & Id & ", RSSI = " & RSSI & ", " & AdvertisingData)
    ConnectedName = Name
    manager.StopScan
    manager.Connect2(Id, False) 'disabling auto connect can make the connection quicker
End Sub

Public Sub StartScan
    If manager.State <> manager.STATE_POWERED_ON Then
        Log("Not powered on.")
    Else If rp.Check(rp.PERMISSION_ACCESS_COARSE_LOCATION) = False Then
        Log("No location permission.")
    Else
        manager.Scan2(Null, False)
    End If
End Sub

Sub Manager_DataAvailable (serviceId As String, Characteristics As Map)
    CallSub3(Main, "DataAvailable", serviceId, Characteristics)
End Sub

Sub Manager_Disconnected
    Log("Disconnected")
    connected = False
    CallSub(Main, "StateChanged")
End Sub

Sub Manager_Connected (services As List)
    Log("Connected!")
    connected = True
    ConnectedServices = services
    CallSub(Main, "StateChanged")
End Sub

'Return true to allow the OS default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
    Return True
End Sub

Sub Service_Destroy

End Sub

In the code "TYSA-B 3.0.0" is the name of the Taiyo Yuden device I'm searching for. If the scan finds another it'll skip it and keep scanning until it finds the one I want. This is the code I used to generate the Log that was posted at the beginning of this topic.
 
Upvote 0

Alfonso Hermida

Member
Licensed User
Some updates....I did the following modifications to the code

B4X:
Public Sub StartScan
    
    If manager.State <> manager.STATE_POWERED_ON Then
        Log("Not powered")
    Else If rp.Check(rp.PERMISSION_ACCESS_COARSE_LOCATION) = False Then
        Log("No location permission")
    Else   
        'manager.Scan2(Array (serviceId), False)  <---- commented and replaced with the line below
        manager.Scan(Null)
    End If   
End Sub


Sub Manager_DeviceFound (Name As String, Id As String, AdvertisingData As Map, RSSI As Double)
    'Log("Dev found = " & Name)
    If Name <> "TYSA-B 3.0.0" Then Return
    Log($"Device found: ${Name}"$)
    manager.StopScan                 'disabling auto connect can make the connection quicker
    Log("Id = " & Id)
    manager.Connect2(Id, False)
    
End Sub

As soon as I touched the slider to generate a stream of characters to send to the BLE device I get a Java dump...Here's the full log

B4X:
*** Service (starter) Create ***
Service_Create
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
Device found: TYSA-B 3.0.0
Id = CB:A7:87:2F:B8:2D
Device found: TYSA-B 3.0.0
Id = CB:A7:87:2F:B8:2D
Discovering services.
Discovering services.
connected: (ArrayList) [00001800-0000-1000-8000-00805f9b34fb, 00001801-0000-1000-8000-00805f9b34fb, 442f1570-8a00-9a28-cbe1-e1d4212d53eb, 0000180f-0000-1000-8000-00805f9b34fb]
connected: (ArrayList) [00001800-0000-1000-8000-00805f9b34fb, 00001801-0000-1000-8000-00805f9b34fb, 442f1570-8a00-9a28-cbe1-e1d4212d53eb, 0000180f-0000-1000-8000-00805f9b34fb]
** Activity (main) Resume **
Inside SendMessage connected =true
SendMessage =
Error occurred on line: 84 (Starter)
java.lang.RuntimeException: Service not found
    at anywheresoftware.b4a.objects.BleManager2.getService(BleManager2.java:375)
    at anywheresoftware.b4a.objects.BleManager2.WriteData(BleManager2.java:328)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runVoidMethod(Shell.java:780)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:363)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:260)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
    at anywheresoftware.b4a.debug.Debug.delegate(Debug.java:259)
    at b4a.example3.starter._sendmessage(starter.java:168)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.debug.Debug.CallSub4(Debug.java:315)
    at anywheresoftware.b4a.debug.Debug.CallSubNew2(Debug.java:282)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.keywords.Common.CallSubDebug2(Common.java:1031)
    at b4a.example3.main._seekbar_valuechanged(main.java:519)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:735)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:360)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:260)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
    at anywheresoftware.b4a.BA$1.run(BA.java:330)
    at android.os.Handler.handleCallback(Handler.java:789)
    at android.os.Handler.dispatchMessage(Handler.java:98)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6944)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
** Activity (main) Pause, UserClosed = true **

Line 84 is the statement "manager.WriteData(serviceId, charId, msg)" within the SendMessage() subroutine.

At least now it connects with the BLE device but still not working 100%
 
Upvote 0

Alfonso Hermida

Member
Licensed User
I went through the code again. Here are the changes I did to get it to connect. I still don't see the characters arriving at the BLE which is connected to the Arduino but the progress is good.

1. serviceId and charId where writted in lower case (upper case didn't work well)

B4X:
Sub Service_Create
    manager.Initialize("manager")
    serviceId = "442f1570-8a00-9a28-cbe1-e1d4212d53eb"
    charId =    "442f1572-8a00-9a28-cbe1-e1d4212d53eb"
    Log("Service_Create")
End Sub

2. As soon as I see the name of the BLE device I want I stop scanning for others
B4X:
Sub Manager_DeviceFound (Name As String, Id As String, AdvertisingData As Map, RSSI As Double)
    If Name <> "TYSA-B 3.0.0" Then Return
    Log($"Device found: ${Name}"$)
    manager.StopScan                 
    Log("Id = " & Id)
    manager.Connect2(Id, False)   
End Sub

3. When connected show the services available
B4X:
Sub manager_Connected (Services As List)
    connected = True
    For i = 0 To Services.Size - 1
        Log($"Services.Get ${i} ${Services.Get(i)}"$)       
    Next
End Sub

4. The app has 2 seek bars that generate characters to send to the BLE device. Show the characters being sent
B4X:
Public Sub SendMessage(msg() As Byte)
    Log("Inside SendMessage, connected =" & connected)
    If Not(connected) Then Return
    Log("msg =" & msg(0) & "," & msg(1))
    manager.WriteData(serviceId, charId, msg)
End Sub

Here's log. It shows that it connected to the BLE but when I check the serial monitor on the Arduino IDE, no characters show up

B4X:
*** Service (starter) Create ***
Service_Create
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
Device found: TYSA-B 3.0.0
Id = CB:A7:87:2F:B8:2D
Device found: TYSA-B 3.0.0
Id = CB:A7:87:2F:B8:2D
Discovering services.
Discovering services.
Services.Get 0 00001800-0000-1000-8000-00805f9b34fb
Services.Get 1 00001801-0000-1000-8000-00805f9b34fb
Services.Get 2 442f1570-8a00-9a28-cbe1-e1d4212d53eb
Services.Get 3 0000180f-0000-1000-8000-00805f9b34fb
Services.Get 0 00001800-0000-1000-8000-00805f9b34fb
Services.Get 1 00001801-0000-1000-8000-00805f9b34fb
Services.Get 2 442f1570-8a00-9a28-cbe1-e1d4212d53eb
Services.Get 3 0000180f-0000-1000-8000-00805f9b34fb
Inside SendMessage, connected =true
msg =0,67
Inside SendMessage, connected =true
msg =1,90
 
Upvote 0
Top