Android Question BLE Manager_DeviceFound doesn't work

Carlos Cerda

Member
Licensed User
Hi guys,

I'm not sure where is the problem....
I have a communication between my Arduino Uno and my Samsung S7 using a HM-10 BLE.
I have modified the Erel's sample. A Timer is sending data from Arduino to cellphone and the cellphone replies with another value.
Everything is ok to this point. Since everything is working in a service (StartForeground), every time the cell phone sleeps, communication is maintained, the same when the APP is swipe. So far everything is normal.

When I turn off the bluetooth sensor and then turn it on, the service is always runnig (I can see a Log() in Timer) but the communication between cellphone and Arduino does not restart.
The instruction "manager.Scan" starts when the bluetooth service is on again (manager.STATE_POWERED_ON) but the event Manager_DeviceFound never fires.

May be this is not correct but, I'm trying to understand wha's happening.
I have put a button in the main activity and in its Click event, I execute StartService (Me) and the event Manager_DeviceFound works, restarting the communication.

Anybody knows what's happening?, why Manager_DeviceFound doesn't work automatically?
 

Carlos Cerda

Member
Licensed User
Thi is my service code:

B4X:
#Region  Service Attributes
    #StartAtBoot: True
    #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
    Private t As Timer
    Private tReconnect As Timer
    Dim PhoneEvent As PhoneEvents
    Public battery As Int
    Dim n As Notification
End Sub

Sub Service_Create
    manager.Initialize("manager")
    serviceId = UUID("ffe0")   
    charId = UUID("ffe1")
    t.Initialize("t", 2000)
    tReconnect.Initialize("tReconnect", 3000)
    t.Enabled = True
    PhoneEvent.Initialize("PhoneEvent")
    
    n.Initialize
    n.AutoCancel = False
    n.OnGoingEvent = False
    n.SetInfo("ATModule", "The APP is running", Main)
    n.Sound = True
    n.Icon = "icon"
    n.AutoCancel = False
End Sub

Sub ManualStart
    Log("ManualStart")
    StartService(Me)
End Sub

Sub Service_Start (StartingIntent As Intent)
    Log("Service_Start")
    manager.Scan2(Array (serviceId), False)
    Service.StartForeground(1, n)
End Sub

Sub t_Tick
    If currentStateText = "POWERED ON" Then
        If manager.State=12 Then
            Log("BT on")
        End If
    End If
End Sub

Sub tReconnect_Tick
    tReconnect.Enabled = False
    StartService(Me)
    t.Enabled = True
End Sub

Sub PhoneEvent_BatteryChanged(Level As Int, Scale As Int, Plugged As Boolean, Intent As Intent)
    battery=Level
End Sub

Sub Manager_StateChanged (State As Int)
    Select State
        Case manager.STATE_POWERED_OFF
            connected = False
            currentStateText = "POWERED OFF"
            manager.StopScan
        Case manager.STATE_POWERED_ON
            currentStateText = "POWERED ON"
        Case manager.STATE_UNSUPPORTED
            currentStateText = "UNSUPPORTED"
    End Select
    CallSub(Main, "BleStateChanged")
    If State = manager.STATE_POWERED_ON Then
        manager.Scan2(Array (serviceId), False)
    End If
End Sub

Sub Manager_DeviceFound (Name As String, Id As String, AdvertisingData As Map, RSSI As Double)
    Log($"Device found: ${Name}"$)
    manager.Connect2(Id, False)
End Sub

Sub manager_Connected (Services As List)
    Log("connected: " & Services)
    connected = True
    Log(serviceId)
    manager.SetNotify(serviceId, charId, True)
End Sub

Sub Manager_Disconnected
    connected = 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)
    'Log(msg)
    Try
        SendMessage(Array As Byte(0, battery))
    Catch
        Log(LastException)
    End Try
    
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
    'StartService(Me)
    Return True
End Sub

Sub Service_Destroy

End Sub
 
Upvote 0

Carlos Cerda

Member
Licensed User
Erel,
I just bought the latest version of B4A to use "Sleep"
Unfortunately, it didn't work.
I even tried with Sleep(3000) and Sleep(5000).... I think the problem is not "time".

The most extrange is when I press the boton "ManualStart " in the Main activity, the communication runs inmediately.
 
Upvote 0

Carlos Cerda

Member
Licensed User
Erel,

I have tried everything you tell me but it does not work. Using "Log", I can see that the service works but "Manager_DeviceFound", not.
I'm attaching the project. It was obtained from yur BLE SeekBar example.
Please, check it.
 

Attachments

  • BLE_SeekBar.zip
    13.5 KB · Views: 347
Upvote 0

Carlos Cerda

Member
Licensed User
Thanks Erel.
I'm not sure what the difference is in using the code in a place other than "Starter" and would be happy to read information in this regard.
I have changed all the code to MyService and the problem continues.
Manager_DeviceFound does not detect any bluetooth device after turn on BT sensor in my phone.

Excuse my question but, have you tried to do the same test that I am indicating?
1.- Establish a communication between a telephone and a BLE (such as HM-10)
2.- Minimize the APP by pressing the Home button.
3.- Turn off the bluetooth sensor and wait a few seconds
4.- Turn on the bluetooth sensor.
5.- Verify that the communication is restored.
 
Upvote 0

Carlos Cerda

Member
Licensed User
Sure!
I am a self-taught person, but I like this a lot. I want your help to learn what's happening and solve it.
Thank you in advance.
 

Attachments

  • B4A.zip
    11.1 KB · Views: 349
Upvote 0
Top