Hello
We are working with these devices, https://store.kontakt.io/next-generation/43-asset-tag-s18-3.html.
We have a service that runs and is scanning:
	
	
	
	
	
	
	
		
			
			
			
			
			
		
	
	
	
		
	
	
		
	
We have noticed that on Android 9 when the screen is turned off no more Scan_Result events are fired, and when the screen is turn back on the scan results resume.
We were using the standard Ble method that Erel provided, https://www.b4x.com/android/forum/threads/ble-2-bluetooth-low-energy.59937/#content
We switched to the above code example to see if it made a difference, the result was the same. It functioned fine up to Android 8.
Regards
John.
			
			We are working with these devices, https://store.kontakt.io/next-generation/43-asset-tag-s18-3.html.
We have a service that runs and is scanning:
			
				B4X:
			
		
		
		#Region  Service Attributes
   #StartAtBoot: False
  
#End Region
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
   Private manager As BleManager2
   Private currentStateText As String = "UNKNOWN"
   Private currentState As Int
   Private scanning As Boolean
   Private bc As ByteConverter
      
   Private const TIME_TO_LIVE_SECONDS As Int = 30
  
   Private Timer1 As Timer
   Private rp As RuntimePermissions
   Private ManagerJO As JavaObject
   Private Adapter As JavaObject
   Private Scanner As JavaObject
   Private ScanCallback As JavaObject
End Sub
Sub Service_Create
   manager.Initialize("manager")
   ManagerJO = manager
   Adapter = ManagerJO.GetField("blueAdapter")
   Scanner = Adapter.RunMethod("getBluetoothLeScanner", Null)
   ScanCallback.InitializeNewInstance(Application.PackageName & ".svc_ble_sos_le$MyScanCallback", Null)
  
   Timer1.Initialize("hook",5000)
End Sub
Sub Service_Start (StartingIntent As Intent)
   Timer1.Enabled = True
  
   StartScan_le
  
  
End Sub
Sub Service_Destroy
  
   StopScanWithLeScanner
  
   Timer1.Enabled = False
   
End Sub
Sub hook_tick
  
   mod_functions.writelog("hook_tick() Still Running")
  
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
End Sub
Sub Manager_DeviceFound (Name As String, Id As String, AdvertisingData As Map, RSSI As Double)
  
   
  
End Sub
' // based on this answer: http://stackoverflow.com/questions/20416218/understanding-ibeacon-distancing
Private Sub CalculateDistance(tx As Int, rssi As Double) As Double
   If rssi = 0 Then Return -1
   Dim ratio As Double = rssi / tx
   If ratio < 1 Then
       Return Power(ratio, 10)
   Else
       Return 0.89976 * Power(ratio, 7.7095) + 0.111
   End If
End Sub
#region "LE Scanner"
Public Sub StartScan_le
   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
       ScanWithLeScanner
   End If
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 name As String
   Dim o As Object = device.RunMethod("getName", Null)
   If o = Null Then name = "" Else name = o
   Dim ScanRecord As JavaObject = ScanResult.RunMethod("getScanRecord", Null)
   'Log(ScanRecord) 'you can extend the code to access the data.
   'https://developer.android.com/reference/android/bluetooth/le/ScanRecord
   Dim rssi As Double = ScanResult.RunMethod("getRssi", Null)
  
   log("svc_ble_sos_le() Scan_Result > name : " & name & ", rssi : " & rssi & ", " & address)
  
' // not called yet as screen off issue
'   Manager_DeviceFound(name, address, CreateMap(), rssi)
  
End Sub
Private Sub ScanWithLeScanner
   Dim ScanSettingsStatic As JavaObject
   ScanSettingsStatic.InitializeStatic("android.bluetooth.le.ScanSettings")
   Dim ScanSettingsBuilder As JavaObject
   ScanSettingsBuilder.InitializeNewInstance("android.bluetooth.le.ScanSettings.Builder", Null)
   'https://developer.android.com/reference/android/bluetooth/le/ScanSettings.Builder
   ScanSettingsBuilder.RunMethod("setScanMode", Array(ScanSettingsStatic.GetField("SCAN_MODE_LOW_POWER")))
  
   Scanner.RunMethod("startScan", Array(Null, ScanSettingsBuilder.RunMethod("build", Null), ScanCallback))
End Sub
Private Sub StopScanWithLeScanner
   Scanner.RunMethod("stopScan", Array(ScanCallback))
End Sub
#End Region
#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) {
       BA.Log("Error: " + errorCode);
    }
}
#End If
	We have noticed that on Android 9 when the screen is turned off no more Scan_Result events are fired, and when the screen is turn back on the scan results resume.
We were using the standard Ble method that Erel provided, https://www.b4x.com/android/forum/threads/ble-2-bluetooth-low-energy.59937/#content
We switched to the above code example to see if it made a difference, the result was the same. It functioned fine up to Android 8.
Regards
John.