Android Question Fixed GPS icon while using service that reads bluetooth in the background

It happens that I have a service that searches for bluetooht in the background, but on the phone the GPS icon is activated in the phone's status bar, as shown in the attached image.


WhatsApp Image 2021-04-01 at 12.07.32.jpeg


Attached code that I use.

Servicio:
#Region  Service Attributes
    #StartAtBoot: True
   
#End Region

Sub Process_Globals
    Private admin As BluetoothAdmin
End Sub

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

Sub Service_Start (StartingIntent As Intent)
    StartServiceAt("", DateTime.Now + 60 * DateTime.TicksPerSecond, True)
    If ModuloFunciones.s.IsInitialized Then
        LogColor("BD Iniciada Servico", 0xFF8A2BE2)
    Else
        LogColor("BD iniciada por el servicio", 0xFF8A2BE2)
        ModuloFunciones.IniciaSQLite
    End If
   
    Connect
    'admin.CancelDiscovery
    Service.StopAutomaticForeground 'Call this when the background task completes (if there is one)
End Sub

Sub Service_Destroy

End Sub

Public Sub Connect
    Log("Buscar BlueTooth...")
    If admin.IsEnabled Then
        admin.StartDiscovery
    Else
        LogColor("BlueTooth Apagado", Colors.Red)
        admin.Enable
    End If
End Sub

Private Sub admin_DeviceFound (Name As String, MacAddress As String)
    'Log($"Nombre Dispositivo: ${Name} / ${MacAddress}"$)
    ModuloFunciones.s.ExecNonQuery("DELETE FROM tags WHERE sync = 1;")
    If Name.StartsWith("AVLTAG_") Then
        admin.CancelDiscovery
        LogColor($"Encontro Dispositivo: ${Name} / ${MacAddress}"$, Colors.Blue)
        ModuloFunciones.s.ExecNonQuery2("INSERT INTO tags VALUES(null, ?, ?, ?, ?, ?, ?)", _
                                            Array As String(Name, MacAddress, ModuloFunciones.ObtineFecha("yyyy-MM-dd"), 0, 0, 0))
        CompruebaConexion
    End If
End Sub

Sub CompruebaConexion
    Dim j As HttpJob
    j.Initialize("", Me)
    'j.Download("https://www.google.cl")
    j.GetRequest.Timeout = 2000
    j.Download("http://ip/testconexion/testconexion.html")
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        Log(j.GetString)
        'Obtiene_Geocercas
        Envia_LecturasTag
    Else
        LogColor("Sin Conexión", Colors.Red)
        'CallSub(Main, "ConsultaPuntos")
    End If
    j.Release
End Sub

Sub Envia_LecturasTag
    Dim j As HttpJob
    Dim Map As Map
    j.Initialize("", Me) 'name is empty as it is no longer needed
   
    ModuloFunciones.s.BeginTransaction
    ModuloFunciones.cbt = ModuloFunciones.s.ExecQuery("SELECT * FROM tags WHERE sync = 0;")
    If ModuloFunciones.cbt.RowCount <> 0 Then
        For i = 0 To ModuloFunciones.cbt.RowCount-1
            ModuloFunciones.cbt.Position = i
                                   
            j.download2(ModuloFunciones.Url_php_mysql(7), _
                    Array As String ("REQUEST_METHOD", "POST_LTAGS", _
                                    "USER_ID", ModuloFunciones.ObtieneID, _
                                    "nombre_tag", ModuloFunciones.cbt.GetString("nombre_tag"), _
                                    "mac_tag", ModuloFunciones.cbt.GetString("mac_tag"), _
                                    "fecha_lectura", ModuloFunciones.cbt.GetString("fecha_lectura") _
                                    ))
                                   

            Wait For (j) JobDone(j As HttpJob)
            If j.Success Then
                'The result is a json string. We parse it and log the fields.
                Dim jp As JSONParser
                jp.Initialize(j.GetString)
                Map = jp.NextObject
                Log(Map.Get("status"))
                Log(Map.Get("message"))
                Dim status = Map.Get("status") As Int
                If status = 1 Then
                    ModuloFunciones.s.ExecNonQuery2("UPDATE tags SET sync = ? WHERE id = '" & ModuloFunciones.cbt.GetInt("id") & "'", Array As Object(1))
                    'LogColor(Map.Get("message"), 0xFF008000)
                End If
               
                If status = 2 Then
                    Log(Map.Get("message"))
                End If
               
                If status = 3 Then
                    Log(Map.Get("message"))
                End If
            End If
        Next
    End If
    'ModuloFunciones.cbt.Close
    ModuloFunciones.s.TransactionSuccessful
    ModuloFunciones.s.EndTransaction
    j.Release
    'Service.StopAutomaticForeground
End Sub
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
This will not work as you expect: StartServiceAt("", DateTime.Now + 60 * DateTime.TicksPerSecond, True)
Most devices will not let you start the service such frequently. You should make it a foreground service.

Scanning for Bluetooth devices is considered a location related task so it shows this icon.
 
Upvote 0
This will not work as you expect: StartServiceAt("", DateTime.Now + 60 * DateTime.TicksPerSecond, True)
Most devices will not let you start the service such frequently. You should make it a foreground service.

Scanning for Bluetooth devices is considered a location related task so it shows this icon.


Hi Erel, excuse the ignorance, how do I convert the service to foreground service.

Thanks
 
Upvote 0

Peter Simpson

Expert
Licensed User
Longtime User
Look at this example...
 
Upvote 0
Top