0

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!

60pfennig
  • 43
  • 6
  • 1
    You can checkout this library Implementation or directly can use it An Android Bluetooth Low Energy (BLE) Library with RxJava2 interface http://polidea.github.io/RxAndroidBle/ – aslamhossin Mar 03 '20 at 18:16
  • Thanks mate, I tried it and it pointed me in the right direction of my error. You dont just need to have the location permissions, but also have to enable location services! – 60pfennig Mar 03 '20 at 19:11

1 Answers1

0

The solution was simple, but time consuming. You dont just need to have the location permissions, but also have to enable location services! On the Pixel 2 devices, they were randomly turned on, so it worked there, but not on the Pixel 1.

60pfennig
  • 43
  • 6