Android Question BLE Receiving/Send problem

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0

chris_sung

Member
Licensed User


Hi Erel
I tried to modify the two functions in the "BLE_Example".
Have read the MA3_623(module) message

I still don't understand it very much. I need your guidance. Please.


Log:
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
Found: , F0:18:98:80:55:ED, RSSI = -90, (MyMap) {1=[B@91fc32c, -1=[B@5b1f5, 0=[B@7239b8a}
Found: , 77:AE:43:C4:C3:6F, RSSI = -89, (MyMap) {1=[B@a4975fb, -1=[B@e5be618, 0=[B@bcc7871}
Found: , 70:66:63:15:94:99, RSSI = -51, (MyMap) {-1=[B@b27e656, 0=[B@4af76d7}
Found: , 61:54:F6:40:4E:3F, RSSI = -95, (MyMap) {1=[B@6354bc4, -1=[B@f5f2aad, 0=[B@95411e2}
Found: , 40:00:B7:ED:63:78, RSSI = -77, (MyMap) {-1=[B@d0aa973, 0=[B@f1d6030}
Found: Mi Band 3, FA:60:BB:CB:52:CD, RSSI = -91, (MyMap) {1=[B@7f0c4a9, -1=[B@1256a2e, 9=[B@dc69cf, 2=[B@b304f5c, 0=[B@8e80265}
Found: MA3_623, 37:38:36:31:30:32, RSSI = -42, (MyMap) {1=[B@aa1fb3a, -1=[B@a5fd3eb, 9=[B@210548, 0=[B@b5b5fe1}
Connect MA3_623
Discovering services.
Connected
0000180f-0000-1000-8000-00805f9b34fb
0000fff0-0000-1000-8000-00805f9b34fb
anywheresoftware.b4a.objects.collections.Map$1@6b947b6
0000fff2-0000-1000-8000-00805f9b34fb:
0000fff1-0000-1000-8000-00805f9b34fb:
anywheresoftware.b4a.objects.collections.Map$1@f71dd53
00002a19-0000-1000-8000-00805f9b34fb: 63

B4X:
Public Sub ReadData
    For Each s As String In ConnectedServices
        manager.ReadData(s)
        Log(s)
        
    Next
    
    
End Sub
=======
Sub Manager_DataAvailable (ServiceId As String, Characteristics As Map)
    Dim bc As ByteConverter
    Log(Characteristics.Keys)
    
    For Each key As String In Characteristics.Keys
        Dim b() As Byte = Characteristics.Get(key)
        Log((key) & ": " & (bc.HexFromBytes(b)))
    Next



    
    CallSub3(Main, "DataAvailable", ServiceId, Characteristics)
End Sub
 
Upvote 0

f0raster0

Well-Known Member
Licensed User
Longtime User

0000180f-0000-1000-8000-00805f9b34fb
it is battery service

2a19 = battery status
63 = percentage (if i'm right)
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
0000fff1-0000-1000-8000-00805f9b34fb:

For the FFF1 you need to register/setup an Notifier.

Something like
B4X:
Manager.SetCharacteristicNotification("0000fff1-0000-1000-8000-00805f9b34fb",True)
Also can also setup an Notifier for the Batterylevel. You´ll get notified if the level changes.

0000fff2-0000-1000-8000-00805f9b34fb
The FFF2 can be used to write to the Device (whatever you need to write i don´t know ;-))

PD: Sorry, i can´t help much more as i have not much experience with BLE... I just read a bit in the Documentation...
 
Last edited:
Upvote 0

f0raster0

Well-Known Member
Licensed User
Longtime User
yes

But I want to receive the Button value...
What do you need to read? What sensors?
have a look to this links:
https://www.b4x.com/android/forum/threads/ible-data.63754/#content
https://www.b4x.com/android/forum/threads/ible-healtthermometer-solved.64232/#post-407611
https://www.b4x.com/android/forum/threads/ble-heart-rate-monitor.64103/#content

the battery is 0000180f-0000-1000-8000-00805f9b34fb
The characteristics are described here: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicsHome.aspx

try to activate setNotify true like that:
manager.SetNotify(0000180f-0000-1000-8000-00805f9b34fb, 00002a19-0000-1000-8000-00805f9b34fb, True)

EDIT: maybe is this try
manager.SetNotify(0000fff0-0000-1000-8000-00805f9b34fb, 0000fff1-0000-1000-8000-00805f9b34fb, True)
 
Last edited:
Upvote 0

chris_sung

Member
Licensed User

Add manager.SetNotify, on line 63.
At the moment, press the button, "fff1" has not seen the value display yet.

Log:
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
Found: , 10:d7:AF:dD:49:C9, RSSI = -99, (MyMap) {-1=[B@5b55610, 0=[B@eceb509}
Found: , 77:76:C9:0D:46:B2, RSSI = -72, (MyMap) {-1=[B@722030e, 0=[B@13dc12f}
Found: , 7C:1C:22:3F:27:33, RSSI = -79, (MyMap) {1=[B@181c33c, -1=[B@a2498c5, 0=[B@57f421a}
Found: , 19:E7:7D:B6:dD:33, RSSI = -87, (MyMap) {-1=[B@76f414b, 0=[B@b45d728}
Found: MA3_623, 37:38:36:31:30:32, RSSI = -52, (MyMap) {1=[B@53afc41, -1=[B@e3f65e6, 9=[B@7eea727, 0=[B@6b53dd4}
Found: , 6A:d5:4C:7A:d0:76, RSSI = -55, (MyMap) {-1=[B@9fa1b7d, 0=[B@38bfa72}
Discovering services.
Connected
Connect MA3_623
Setting descriptor. Success = true
writing descriptor: true
0000180f-0000-1000-8000-00805f9b34fb
0000fff0-0000-1000-8000-00805f9b34fb
0000fff2-0000-1000-8000-00805f9b34fb:
0000fff1-0000-1000-8000-00805f9b34fb:
00002a19-0000-1000-8000-00805f9b34fb: 63


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 rp As RuntimePermissions
End Sub



Sub Service_Create
    manager.Initialize("manager")
End Sub

Sub Service_Start (StartingIntent As Intent)

End Sub

Public Sub ReadData
   
    For Each s As String In ConnectedServices
        manager.ReadData(s)
        Log(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)
    Log("Found: " & Name & ", " & Id & ", RSSI = " & RSSI & ", " & AdvertisingData) 'ignore
    ConnectedName = Name
   
    If ConnectedName="MA3_623" Then
        manager.StopScan
        manager.Connect2(Id, False) 'disabling auto connect can make the connection quicker
       
       
        Sleep(2000)
        Log("Connect MA3_623")
        manager.SetNotify("0000fff0-0000-1000-8000-00805f9b34fb", "0000fff1-0000-1000-8000-00805f9b34fb", True)
       
    End If
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)
    Dim bc As ByteConverter
   
   CallSub3(Main, "DataAvailable", ServiceId, Characteristics)
   
    For Each key As String In Characteristics.Keys
        Dim b() As Byte = Characteristics.Get(key)
        Log((key) & ": " & (bc.HexFromBytes(b)))
    Next

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
 
Upvote 0

f0raster0

Well-Known Member
Licensed User
Longtime User
I have different modules,
using this code I can read the data..

try to change to that:
B4X:
Sub Manager_Connected (services As List)
    Log("Connected")
    connected = True
    ConnectedServices = services
    manager.SetNotify("0000fff0-0000-1000-8000-00805f9b34fb", "0000fff1-0000-1000-8000-00805f9b34fb", True)
    Log("Set Notify") 
    CallSub(Main, "StateChanged")
End Sub

Sub Manager_DataAvailable (ServiceId As String, Characteristics As Map)
    Dim bc As ByteConverter
    For Each key As String In Characteristics.Keys
        Dim b() As Byte = Characteristics.Get(key)
        Log("Key: " & key &", value: " & bc.HexFromBytes(b))
        'Log(BytesToString(b, 0, b.Length, "utf8") 'useful if the data is actually a string
        If key = "" Then
        End If
    Next  
End Sub
 
Last edited:
Upvote 0

chris_sung

Member
Licensed User

Hi f0raster0

Now receiving data success!!
Thank you very much.

Erel、DonManfred
Thank you very much for your assistance.
 
Upvote 0

f0raster0

Well-Known Member
Licensed User
Longtime User
i need help regard connecting my NXP kw 38 controller with b4a apk. have any examples related this?
Try the example:


Create a new thread for your question, and post the errors that you are getting
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…