Android Question [SOLVED] - Android 10 - Background GPS/Ble scans stop getting data after 1 hr

Jmu5667

Well-Known Member
Licensed User
Longtime User
EDIT #1: This does not work on SAMSUNG phone running API 29+
EDIT #2: See post #12
EDIT #3: There maybe a solution to this, both myself and @OliverA have been working on. Will let you know tommorrow.


Hello All

I have a gps service and a ble service. They run every 5 minutes from a main service which has all the necessary lock and notifications set for the app to run in the back ground. I have the SDK level set to 29 in the manifest with the following permissions:

The app is not in the foreground and the screen is off. When the screen is turned on again data is received from both services.

Both the BLE and GPS do not give any results for their scanning i.e No Beacons found and No GPS NMEA strings. (see logs)

This is a summary as some of the manifest data has special keys provided by some phone manufacturers.
B4X:
' // https://developer.android.com/guide/topics/manifest/uses-feature-element#hw-features
AddManifestText(<uses-feature android:name="android.hardware.bluetooth"/>)
AddManifestText(<uses-feature android:name="android.hardware.bluetooth_le"/>)
AddManifestText(<uses-feature android:name="android.hardware.location.gps"/>)
AddManifestText(<uses-feature android:name="android.hardware.camera"/>)
AddManifestText(<uses-feature android:name="android.hardware.camera.autofocus"/>)
AddManifestText(<uses-feature android:name="android.hardware.camera.flash" />)


AddPermission(android.permission.ACCESS_COARSE_LOCATION)
AddPermission(android.permission.ACCESS_COARSE_UPDATES)
AddPermission(android.permission.ACCESS_BACKGROUND_LOCATION)
AddPermission(android.permission.ACCESS_FINE_LOCATION)

AddPermission(android.permission.ACCESS_NETWORK_STATE)
AddPermission(android.permission.ACCESS_WIFI_STATE)
AddPermission(android.permission.BLUETOOTH)
AddPermission(android.permission.BLUETOOTH_ADMIN)
AddPermission(android.permission.CAMERA)
AddPermission(android.permission.CALL_PHONE)
AddPermission(android.permission.CALL_PRIVILEGED)
AddPermission(android.permission.CHANGE_NETWORK_STATE)
AddPermission(android.permission.CHANGE_WIFI_STATE)
AddPermission(android.permission.FLASHLIGHT)
AddPermission(android.permission.GET_TASKS)
AddPermission(android.permission.INTERNET)

AddPermission(android.permission.MODIFY_PHONE_STATE)
AddPermission(android.permission.MODIFY_AUDIO_SETTINGS)
AddPermission(android.permission.READ_EXTERNAL_STORAGE)
AddPermission(android.permission.READ_PHONE_STATE)
AddPermission(android.permission.READ_SMS)
AddPermission(android.permission.READ_CALL_LOG)
AddPermission(android.permission.RECEIVE_BOOT_COMPLETED)
AddPermission(android.permission.RECEIVE_SMS)
AddPermission(android.permission.SEND_SMS)
AddPermission(android.permission.UPDATE_DEVICE_STATS)
AddPermission(android.permission.VIBRATE)
AddPermission(android.permission.WAKE_LOCK)
AddPermission(android.permission.WRITE_EXTERNAL_STORAGE)
AddPermission(android.permission.WRITE_SECURE_SETTINGS)
AddPermission(android.permission.WRITE_SETTINGS)
AddPermission(android.permission.WRITE_SMS)
AddPermission(android.permission.PROCESS_OUTGOING_CALLS)
AddPermission(android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS)


Application log data
B4X:
*** Service (svc_ble) Create ***
01/08/2021 10:12:07.395 - Atlas SOS - svc_ble(), Service_Create
** Service (svc_ble) Start **
01/08/2021 10:12:07.409 - Atlas SOS - svc_ble(), Service_Start
01/08/2021 10:12:07.414 - Atlas SOS - svc_ble(), StartScan, STATE = 12
01/08/2021 10:12:07.426 - Atlas SOS - svc_ble(), Manager_StateChanged, 12 POWERED ON
01/08/2021 10:12:17.428 - Atlas SOS - svc_ble() check_timeout, Scan Time Reached
01/08/2021 10:12:17.444 - Atlas SOS - svc_ble(), process_beacons, SCAN COMPLETE, Beacons Count = 3
01/08/2021 10:12:17.455 - Atlas SOS -  Beacon One - DESK FE:FF:1A:CE:57:B3 Kontakt 56440 1652 -86.0 -12
01/08/2021 10:12:17.478 - Atlas SOS -  Beacon Three - SERVERS E8:E9:11:48:09:76 Kontakt 16563 59525 -89.0 -12
01/08/2021 10:12:17.494 - Atlas SOS -  Beacon One - BOOKSHELF E1:BC:CB:CD:A7:9E Kontakt 61077 34408 -84.0 -12
01/08/2021 10:12:17.512 - Atlas SOS - svc_ble(), BEACONS = 564401652-86-1201656359525-89-1206107734408-84-120
01/08/2021 10:12:17.515 - Atlas SOS - svc_ble(), Created Beacon Packet  WIBD~12021/01/08 10:12:17866599043350697564401652-86-1201656359525-89-1206107734408-84-120
** Service (svc_ble) Destroy **
01/08/2021 10:12:17.519 - Atlas SOS - svc_ble(), Service_Destroy
01/08/2021 10:12:17.521 - Atlas SOS - svc_ble(), Service_Destroy, Next Run @ 10:17:17
01/08/2021 10:12:19.380 - Atlas SOS - svc_service(), tmrServiceHelper_tick, STARTING svc_gps SERVICE
*** Service (svc_gps) Create ***
01/08/2021 10:12:19.412 - Atlas SOS - svc_gps(), Service_Create
** Service (svc_gps) Start **
01/08/2021 10:12:19.419 - Atlas SOS - svc_gps(), Service_Start, GPS timeout set to 10:14:19
01/08/2021 10:12:19.423 - Atlas SOS - svc_gps(), Service_Start, GPS Location Intent android.settings.LOCATION_SOURCE_SETTINGS
01/08/2021 10:12:19.427 - Atlas SOS - svc_gps(), Service_Start, Location Providers Allowed gps,network
01/08/2021 10:12:20.236 - Atlas SOS - **** GPS STARTED ****
01/08/2021 10:12:20.242 - Atlas SOS - svc_gps(), aquire_position, GPS turned ON
01/08/2021 10:12:23.142 - Atlas SOS - svc_gps(),mGps_NMEA, RMC=$GPRMC,101220.00,A,5222.116603,N,00630.413579,W,0.3,297.2,080121,6.3,W,A,V*5F

01/08/2021 10:12:24.132 - Atlas SOS - svc_gps(),mGps_NMEA, RMC=$GPRMC,101221.00,A,5222.117767,N,00630.414598,W,0.0,,080121,6.3,W,A,V*77

01/08/2021 10:12:25.128 - Atlas SOS - svc_gps(),mGps_NMEA, RMC=$GPRMC,101222.00,A,5222.117829,N,00630.414643,W,0.0,,080121,6.3,W,A,V*74

01/08/2021 10:12:26.141 - Atlas SOS - svc_gps(),mGps_NMEA, RMC=$GPRMC,101223.00,A,5222.117995,N,00630.414695,W,0.0,,080121,6.3,W,A,V*78

01/08/2021 10:12:26.144 - Atlas SOS - svc_gps(), end of scheduled run
01/08/2021 10:12:26.150 - Atlas SOS - StopGps
** Service (svc_gps) Destroy **
01/08/2021 10:12:26.261 - Atlas SOS - StopGps
01/08/2021 10:12:26.264 - Atlas SOS - svc_gps(), Service_Destroy
01/08/2021 10:12:26.267 - Atlas SOS - mod_functions(), set_next_gps_update, Main.APPSET.RT_gps.next_update = 10:17:26
01/08/2021 10:12:26.268 - Atlas SOS - svc_gps(), Service_Destroy, Service Callback enabled, 10:17:26
.
. Everything is good so far
.
01/08/2021 11:03:44.147 - Atlas SOS - svc_gps(), Service_Start, GPS timeout set to 11:05:44
01/08/2021 11:03:44.150 - Atlas SOS - svc_gps(), Service_Start, GPS Location Intent android.settings.LOCATION_SOURCE_SETTINGS
01/08/2021 11:03:44.153 - Atlas SOS - svc_gps(), Service_Start, Location Providers Allowed gps,network
01/08/2021 11:03:44.222 - Atlas SOS - **** GPS STARTED ****
01/08/2021 11:03:44.229 - Atlas SOS - svc_gps(), aquire_position, GPS turned ON
01/08/2021 11:03:48.136 - Atlas SOS - svc_gps(),mGps_NMEA, RMC=$GPRMC,110345.00,A,5222.114914,N,00630.412939,W,0.0,,080121,6.3,W,A,V*7C

01/08/2021 11:03:49.145 - Atlas SOS - svc_gps(),mGps_NMEA, RMC=$GPRMC,110346.00,A,5222.114361,N,00630.413258,W,0.0,,080121,6.3,W,A,V*7A

01/08/2021 11:03:50.151 - Atlas SOS - svc_gps(),mGps_NMEA, RMC=$GPRMC,110347.00,A,5222.114454,N,00630.412964,W,0.0,,080121,6.3,W,A,V*7F

01/08/2021 11:03:51.148 - Atlas SOS - svc_gps(),mGps_NMEA, RMC=$GPRMC,110348.00,A,5222.113454,N,00630.412708,W,0.0,,080121,6.3,W,A,V*73

01/08/2021 11:03:51.150 - Atlas SOS - svc_gps(), end of scheduled run
01/08/2021 11:03:51.157 - Atlas SOS - StopGps
** Service (svc_gps) Destroy **
01/08/2021 11:03:51.246 - Atlas SOS - StopGps
01/08/2021 11:03:51.249 - Atlas SOS - svc_gps(), Service_Destroy
01/08/2021 11:03:51.251 - Atlas SOS - mod_functions(), set_next_gps_update, Main.APPSET.RT_gps.next_update = 11:08:51
01/08/2021 11:03:51.253 - Atlas SOS - svc_gps(), Service_Destroy, Service Callback enabled, 11:08:51
*** Service (svc_ble) Create ***
01/08/2021 11:03:52.139 - Atlas SOS - svc_ble(), Service_Create
** Service (svc_ble) Start **
01/08/2021 11:03:52.151 - Atlas SOS - svc_ble(), Service_Start
01/08/2021 11:03:52.156 - Atlas SOS - svc_ble(), StartScan, STATE = 12
01/08/2021 11:03:52.167 - Atlas SOS - svc_ble(), Manager_StateChanged, 12 POWERED ON
01/08/2021 11:04:02.168 - Atlas SOS - svc_ble() check_timeout, Scan Time Reached
01/08/2021 11:04:02.185 - Atlas SOS - svc_ble(), process_beacons, SCAN COMPLETE, Beacons Count = 3
01/08/2021 11:04:02.193 - Atlas SOS -  Beacon One - DESK FE:FF:1A:CE:57:B3 Kontakt 56440 1652 -91.0 -12
01/08/2021 11:04:02.213 - Atlas SOS -  Beacon Three - SERVERS E8:E9:11:48:09:76 Kontakt 16563 59525 -86.0 -12
01/08/2021 11:04:02.237 - Atlas SOS -  Beacon One - BOOKSHELF E1:BC:CB:CD:A7:9E Kontakt 61077 34408 -84.0 -12
01/08/2021 11:04:02.253 - Atlas SOS - svc_ble(), BEACONS = 564401652-91-1201656359525-86-1206107734408-84-120
01/08/2021 11:04:02.254 - Atlas SOS - svc_ble(), Created Beacon Packet  WIBD~12021/01/08 11:04:02866599043350697564401652-91-1201656359525-86-1206107734408-84-120
** Service (svc_ble) Destroy **
01/08/2021 11:04:02.258 - Atlas SOS - svc_ble(), Service_Destroy
01/08/2021 11:04:02.259 - Atlas SOS - svc_ble(), Service_Destroy, Next Run @ 11:09:02
01/08/2021 11:08:52.629 - Atlas SOS - svc_service(), tmrServiceHelper_tick, STARTING svc_gps SERVICE
'
' Now it begins to fail
'
*** Service (svc_gps) Create ***
01/08/2021 11:08:52.653 - Atlas SOS - svc_gps(), Service_Create
** Service (svc_gps) Start **
01/08/2021 11:08:52.657 - Atlas SOS - svc_gps(), Service_Start, GPS timeout set to 11:10:52
01/08/2021 11:08:52.660 - Atlas SOS - svc_gps(), Service_Start, GPS Location Intent android.settings.LOCATION_SOURCE_SETTINGS
01/08/2021 11:08:52.662 - Atlas SOS - svc_gps(), Service_Start, Location Providers Allowed gps,network
01/08/2021 11:08:52.869 - Atlas SOS - **** GPS STARTED ****
01/08/2021 11:08:52.876 - Atlas SOS - svc_gps(), aquire_position, GPS turned ON
*** Service (svc_ble) Create ***
01/08/2021 11:09:02.686 - Atlas SOS - svc_ble(), Service_Create
** Service (svc_ble) Start **
01/08/2021 11:09:02.701 - Atlas SOS - svc_ble(), Service_Start
01/08/2021 11:09:02.706 - Atlas SOS - svc_ble(), StartScan, STATE = 12
01/08/2021 11:09:02.719 - Atlas SOS - svc_ble(), Manager_StateChanged, 12 POWERED ON
01/08/2021 11:09:12.717 - Atlas SOS - svc_ble() check_timeout, Scan Time Reached
01/08/2021 11:09:12.731 - Atlas SOS - svc_ble(), process_beacons, SCAN COMPLETE, Beacons Count = 0
01/08/2021 11:09:12.736 - Atlas SOS - svc_ble(), No Beacons found
01/08/2021 11:09:12.740 - Atlas SOS - svc_ble(), Created Beacon Packet, packet is too old
01/08/2021 11:09:12.743 - Atlas SOS - svc_ble(), Created Beacon Packet  WIBD~12021/01/08 11:09:12866599043350697NOBEACONFIX
** Service (svc_ble) Destroy **
01/08/2021 11:09:12.750 - Atlas SOS - svc_ble(), Service_Destroy
01/08/2021 11:09:12.753 - Atlas SOS - svc_ble(), Service_Destroy, Next Run @ 11:14:12
01/08/2021 11:10:53.107 - Atlas SOS - svc_gps(), perform_timeout, unable To get GPS Fix
01/08/2021 11:10:53.126 - Atlas SOS - StopGps
01/08/2021 11:10:53.134 - Atlas SOS - svc_gps(), perform_timeout, GPS turned OFF
** Service (svc_gps) Destroy **
01/08/2021 11:10:53.141 - Atlas SOS - StopGps
01/08/2021 11:10:53.144 - Atlas SOS - svc_gps(), Service_Destroy
01/08/2021 11:10:53.147 - Atlas SOS - mod_functions(), set_next_gps_update, Main.APPSET.RT_gps.next_update = 11:15:53
01/08/2021 11:10:53.150 - Atlas SOS - svc_gps(), Service_Destroy, Service Callback enabled, 11:15:53
*** Service (svc_ble) Create ***
01/08/2021 11:14:13.308 - Atlas SOS - svc_ble(), Service_Create
** Service (svc_ble) Start **
01/08/2021 11:14:13.319 - Atlas SOS - svc_ble(), Service_Start
01/08/2021 11:14:13.323 - Atlas SOS - svc_ble(), StartScan, STATE = 12
01/08/2021 11:14:13.334 - Atlas SOS - svc_ble(), Manager_StateChanged, 12 POWERED ON
01/08/2021 11:14:23.351 - Atlas SOS - svc_ble() check_timeout, Scan Time Reached
01/08/2021 11:14:23.367 - Atlas SOS - svc_ble(), process_beacons, SCAN COMPLETE, Beacons Count = 0
01/08/2021 11:14:23.374 - Atlas SOS - svc_ble(), No Beacons found
01/08/2021 11:14:23.379 - Atlas SOS - svc_ble(), Created Beacon Packet, packet is too old
01/08/2021 11:14:23.382 - Atlas SOS - svc_ble(), Created Beacon Packet  WIBD~12021/01/08 11:14:23866599043350697NOBEACONFIX
** Service (svc_ble) Destroy **
01/08/2021 11:14:23.388 - Atlas SOS - svc_ble(), Service_Destroy
01/08/2021 11:14:23.391 - Atlas SOS - svc_ble(), Service_Destroy, Next Run @ 11:19:23
01/08/2021 11:15:53.494 - Atlas SOS - svc_service(), tmrServiceHelper_tick, STARTING svc_gps SERVICE
*** Service (svc_gps) Create ***
01/08/2021 11:15:53.524 - Atlas SOS - svc_gps(), Service_Create
** Service (svc_gps) Start **
01/08/2021 11:15:53.529 - Atlas SOS - svc_gps(), Service_Start, GPS timeout set to 11:17:53
01/08/2021 11:15:53.531 - Atlas SOS - svc_gps(), Service_Start, GPS Location Intent android.settings.LOCATION_SOURCE_SETTINGS
01/08/2021 11:15:53.534 - Atlas SOS - svc_gps(), Service_Start, Location Providers Allowed gps,network
01/08/2021 11:15:53.752 - Atlas SOS - **** GPS STARTED ****
01/08/2021 11:15:53.758 - Atlas SOS - svc_gps(), aquire_position, GPS turned ON
'
'
'

The above log data is from an OnePlus 7T but we have had the same results other Android 10 Devices, namely Samsung XCover.

The ble service has the following code for background scanning:

B4X:
Sub Process_Globals()

    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

    Dim SCAN_FAILED_ALREADY_STARTED As Int = 1
    Dim SCAN_FAILED_APPLICATION_REGISTRATION_FAILED As Int = 2
    Dim SCAN_FAILED_FEATURE_UNSUPPORTED As Int = 4
    Dim SCAN_FAILED_INTERNAL_ERROR As Int = 3
    Dim SCAN_TIMEOUT As Int = 10

    Dim tmrClock As Timer
    Dim mfinished, scanComplete, restartNow As Boolean
    ' // class's
    Dim ws_sck As cls_sockets
    Dim ca As cls_assets
    Dim comI As mComms
    Dim sf As StringFunctions
    Dim dieNow As Boolean

    Private manager             As BleManager2
    Private Scanner             As JavaObject
    Private ScanCallback         As JavaObject
    Private ManagerJO             As JavaObject
    Private Adapter             As JavaObject

    Dim iBeacons As Map
    Dim bc As ByteConverter

End Sub


Sub Service_Create

   Main.APPSET.RT_iBeacon.next_update = 0

    ' // main data is not available
    If Not(Main.APPSET.IsInitialized) Then
        dieNow = True
        Return
    End If

    mfinished = False
    scanComplete = False

    mod_functions.writelog("svc_ble(), Service_Create")
    Try
        tmrClock.Initialize("tmrClock",1000)
        comI.Initialize
        ca.Initialize
        ws_sck.Initialize(Me,"svc_ble","socket")

        manager.Initialize("manager")
        ManagerJO = manager
        Adapter = ManagerJO.GetField("blueAdapter")
        Scanner = Adapter.RunMethod("getBluetoothLeScanner", Null)
        ScanCallback.InitializeNewInstance(Application.PackageName & ".svc_ble$MyScanCallback", Null)
    Catch
        mod_functions.writelog("svc_ble(), Service_Create, error - " & LastException.Message )
        StopService(Me)
    End Try
  

End Sub
Private Sub ScanWithLeScanner

    Dim ScanSettingsStatic As JavaObject
    ScanSettingsStatic.InitializeStatic("android.bluetooth.le.ScanSettings")

    ' // https://punchthrough.com/android-ble-guide/
    ' // https://developer.android.com/reference/android/bluetooth/le/ScanSettings.Builder
    ' // https://developer.android.com/reference/android/bluetooth/le/ScanSettings#SCAN_MODE_LOW_POWER

    Dim ScanSettingsBuilder As JavaObject
    ScanSettingsBuilder.InitializeNewInstance("android.bluetooth.le.ScanSettings.Builder", Null)

    ' // 2021.01.07 -
    ScanSettingsBuilder.RunMethod("setScanMode", Array(ScanSettingsStatic.GetField("SCAN_MODE_LOW_POWER")))

    Dim ScanFilterStatic As JavaObject
    ScanFilterStatic.InitializeStatic("android.bluetooth.le.ScanFilter")

    Dim ScanFilterBuilder As JavaObject
    ScanFilterBuilder.InitializeNewInstance("android.bluetooth.le.ScanFilter.Builder", Null)
    ScanFilterBuilder.RunMethod("setManufacturerData", Array(76,Null)) 'Apple ID iBeacon

    Dim Filters As List = Array(ScanFilterBuilder.RunMethod("build", Null))
    Scanner.RunMethod("startScan", Array(Filters, ScanSettingsBuilder.RunMethod("build", Null), ScanCallback))
    comI.scanning = True

End Sub


Private Sub Scan_Failed (Result As Int)

    Dim res As String

    Select Case Result
  
        Case 1
            res = "SCAN_FAILED_ALREADY_STARTED"
        Case 2
            res = "SCAN_FAILED_APPLICATION_REGISTRATION_FAILED"
        Case 3
            res = "SCAN_FAILED_INTERNAL_ERROR"
        Case 4
            res = "SCAN_FAILED_FEATURE_UNSUPPORTED"
    End Select

    mod_functions.writeLog($"svc_ble(), Scan_Failed, ${Result} ${res} "$)

    Dim btm As BluetoothAdmin
    btm.Initialize("")
    btm.Disable

    Main.APPSET.RT_iBeacon.timeout = 0
    scanComplete = True


End Sub

Private Sub Scan_Result (Result As Object)

    Dim ScanResult As JavaObject = Result
    Dim device As JavaObject = ScanResult.RunMethod("getDevice", Null)
    Dim address As String = device.RunMethod("getAddress", Null)
    ManagerJO.GetFieldJO("devices").RunMethod("put", Array(address, device))

    Dim ScanRecord As JavaObject = ScanResult.RunMethod("getScanRecord", Null)
    Dim data() As Byte = ScanRecord.RunMethod("getManufacturerSpecificData", Array(76)) '76 = apple id
  
    If data <> Null Then
  
        Try
            Private bc As ByteConverter
            Dim raf As RandomAccessFile
            Dim ib As mBeacon
            raf.Initialize3(data, False)
            ' // load beacons props
            ib.Initialize
            ib.name = ScanRecord.RunMethod("getDeviceName",Null)
            ib.uniqueid = address
            ib.major =  Bit.And(0xFFFF,raf.ReadShort(18))
            ib.minor =  Bit.And(0xFFFF,raf.ReadShort(20))
            ib.rssi = ScanResult.RunMethod("getRssi", Null)
            ib.TX = ScanRecord.RunMethod("getTxPowerLevel",Null)
            ' // https://www.b4x.com/android/forum/threads/iot-beaconparser-discover-ibeacons-and-eddystone-beacons.61127/post-450458
            ib.distance = CalculateDistance(ib.tx, ib.rssi)
            ib.time = DateTime.Now
            iBeacons.Put(ib.uniqueid, ib)
        Catch
            'mod_functions.writelog($"Scan_Result, Error ${LastException.Message}"$)
            'mod_functions.writelog(ScanRecord.RunMethod("toString", Null))
        End Try  
        'Log($" ${ib.uniqueid} ${ib.major} ${ib.minor} ${ib.rssi} ${ib.TX} "$)
  
    End If

End Sub


#if Java
import android.bluetooth.le.*;
public static class MyScanCallback extends ScanCallback {
public void onScanResult(int callbackType, ScanResult result) {
         processBA.raiseEvent(this, "scan_result", result);
    }



    /**
     * Callback when scan could not be started.
     *
     * @param errorCode Error code (one of SCAN_FAILED_*) for scan failure.
     */
    public void onScanFailed(int errorCode) { 
       processBA.raiseEvent(this, "scan_failed", errorCode);
    }
}
#End If


I am not sure what is going on, so any help would be appreciated. This is an SOS app and is currently used by thousands' of users and this problem will prevent it from doing is job, protecting people.

Regards

John.
 
Last edited:
Top