0

I am trying to implement the most simple app to scan for a BLE device. I have given the necessary rights:

<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

However, onScanResult never gets called. Bluetooth is enabled on my phone and the BLE device is on, this is my code:

class MainActivity : AppCompatActivity() {
private val bleScanner = object :ScanCallback() {
    override fun onScanResult(callbackType: Int, result: ScanResult) {
        Log.d("DeviceListActivity", "onScanResult()")
        super.onScanResult(callbackType, result)
        val device = result.device
        Log.d("DeviceScanner", "Device found: ${device.address} - ${device.name ?: "Unknown"}")
        //Log.d("DeviceListActivity","onScanResult: ${result.device?.address} - ${result.device?.name}")
    }
}

private val bluetoothLeScanner: BluetoothLeScanner
    get()  {
        val bluetoothManager = applicationContext.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager
        val bluetoothAdapter: BluetoothAdapter = bluetoothManager.adapter
        return bluetoothAdapter.bluetoothLeScanner
    }

override fun onCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?) {
    Log.d("DeviceListActivity", "onCreate()")
    super.onCreate(savedInstanceState, persistentState)
    setContentView(R.layout.activity_device_list)
}

override fun onStart() {
    Log.d("DeviceListActivity", "onStart()")
    super.onStart()

    enableLocation()
    bluetoothLeScanner.startScan(bleScanner)
}

override fun onStop() {
    Log.d("DeviceListActivity", "onStop()")
    bluetoothLeScanner.stopScan(bleScanner)
    super.onStop()
}

private fun enableLocation(): Boolean {
    val service = applicationContext.getSystemService(Context.LOCATION_SERVICE) as LocationManager
    val enabled = service.isProviderEnabled(LocationManager.GPS_PROVIDER)
    return enabled
}
}

What am I missing?

hsc
  • 13
  • 3
  • Have you confirmed that your BLE device is an actual BLE device using a generic BLE scanner app such as [nRF Connect](https://www.nordicsemi.com/Products/Development-tools/nrf-connect-for-mobile)? – Michael Kotzjan Sep 07 '21 at 10:34
  • 1
    @MichaelKotzjan With "nRF Connect" i can find the device – hsc Sep 07 '21 at 10:52
  • 1
    I see that you have declared the location permission in your manifest and you also enable it on every start. But you never properly requested the permission which is needed for the location (see [this](https://stackoverflow.com/a/40142454/7473793) answer for more information. [This](https://developer.android.com/guide/topics/connectivity/bluetooth/permissions) page from the android developer guide also shows you the permission requirements for different android versions – Michael Kotzjan Sep 07 '21 at 11:41

1 Answers1

1

Yes it was the missing requested permission. I inserted following function into onResume()

    if (ContextCompat.checkSelfPermission(this,Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(
            this,
            arrayOf(Manifest.permission.ACCESS_FINE_LOCATION),
            0
        )
    } else {
        leDeviceScanner.start()
    }

It now workes like a charm. Thanks!

hsc
  • 13
  • 3