Android Question BLE connection lost

Similar threads

B4A Library BLE 2 - Bluetooth Low Energy
B4A Library BLE Peripheral
B4A Code Snippet Theme Colors
B4A Tutorial BLE - Heart Rate Monitor
B4A Tutorial Discovering iBeacon devices with BLE2 library

Alfonso Hermida

Member
Licensed User
I've noticed that BLE connection is lost when phone goes to sleep mode or the app is not the primary screen. Any suggestions on how to bypass this?
 

MarkusR

Well-Known Member
Licensed User
you made the "BLE connection" in a service?
 

Alfonso Hermida

Member
Licensed User
you made the "BLE connection" in a service?
This is what I used:

B4X:
Sub Process_Globals
    Public manager As BleManager2
    Public currentStateText As String = "UNKNOWN"
    Private serviceId, charId, ReadId As String
    Dim connected As Boolean
    Public rp As RuntimePermissions
    Dim num As Boolean = False
    Dim IDDevConn As String = ""
End Sub

Sub Service_Create
    manager.Initialize("manager")
    serviceId = "6e400001-b5a3-f393-e0a9-e50e24dcca9e"
    charId =    "6e400002-b5a3-f393-e0a9-e50e24dcca9e"
    ReadId =    "6e400003-b5a3-f393-e0a9-e50e24dcca9e"
    Log("Service_Create")
End Sub

Sub Service_Start (StartingIntent As Intent)

End Sub
 

emexes

Well-Known Member
Licensed User
I have found 3 devices of perhaps 15 where Bluetooth and Wifi are linked together, and when WiFi is power-saved, then it takes Bluetooth down with it. There are usually multiple settings for this, and you have to find them all in order to quell the beast. Keeping the phone connected to a charger can help, although obviously that might not be practical in the field. I just grabbed the nearest phone to me right now and found:

Screenshot_20190402-102558[1].png
Screenshot_20190402-102533[4].png
 

emexes

Well-Known Member
Licensed User
Another way of keeping WiFi active is to use B4ABridge. Erel said it had a similar problem with connections going down, and so B4A now pings B4ABridge (or perhaps it's the other way around) once a second to keep the network connection alive. If you have BLE apps that work on your development desk but fail elsewhere, this could be a reason why.
 

Alfonso Hermida

Member
Licensed User
I added the PhoneWakeState object to the app and it seems to keep the connection on.

B4X:
Sub Service_Create
    manager.Initialize("manager")
    serviceId = "6e400001-b5a3-f393-e0a9-e50e24dcca9e"
    charId =    "6e400002-b5a3-f393-e0a9-e50e24dcca9e"
    ReadId =    "6e400003-b5a3-f393-e0a9-e50e24dcca9e"
    Log("Service_Create")
    lock.PartialLock
End Sub

Sub Service_Destroy
    lock.ReleasePartialLock
End Sub
Now I need to figure out how to restore the values of some of the component values when the phone goes to sleep
 
Top