I'am working on a RSSI scanner for Android. I want to archieve this via constantly scanning for BLE devices. So if the RSSI of a device should be figured out it needs to advertise via BLE. The measuring device start scanning in really short intervalls and as it finds the advertiser the RSSI can be read in the scan result. That results in folowing code on the advertiser side:
fun beginAdvertise() {
val advertiser = BluetoothAdapter.getDefaultAdapter().bluetoothLeAdvertiser
val settings = AdvertiseSettings.Builder()
.setAdvertiseMode(AdvertiseSettings.ADVERTISE_MODE_LOW_LATENCY)
.setTxPowerLevel(AdvertiseSettings.ADVERTISE_TX_POWER_HIGH)
.setConnectable(false)
.build()
val data = AdvertiseData.Builder()
.setIncludeDeviceName(true)
.build()
val advertisingCallback = object : AdvertiseCallback() {
override fun onStartFailure(errorCode: Int) {
Log.e("BLE", "Advertising onStartFailure: " + errorCode);
super.onStartFailure(errorCode)
}
}
advertiser.startAdvertising(settings, data, advertisingCallback);
}
The function for the scanning device gets called every 20ms (I tried much longer delays) and looks like this:
private val onBluetoothLeDeviceFound = object : ScanCallback() {
override fun onScanResult(callbackType: Int, result: ScanResult?) {
result?.let {...}}
fun startScan() {
val scanner = mBluetoothAdapter?.bluetoothLeScanner
val scanSetting =
ScanSettings.Builder().setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY)
.setMatchMode(ScanSettings.MATCH_MODE_AGGRESSIVE).build()
scanner?.startScan(
null,
scanSetting,
onBluetoothLeDeviceFound
)
}
This code is working perfectly on my Pixel 2 devices. But if I try it on Pixel 1 (one on Android 9, the other on Android 10) devices the onBluetoothLeDeviceFound callback does not get called. I have these permissions:
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCTION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/>
I really don'T know what I should try out next. Thank you for your help!