Oreo, BLE scanner startScan with PendingIntent not working on Samsung device

前端 未结 3 1679
感动是毒
感动是毒 2021-02-11 09:41

I am trying to scan for beacons using startScan(filters, settings, callbackIntent). I have an implementation that works fine for Sony Xperia XZ, and Nexus 5X. The only other dev

3条回答
  •  刺人心
    刺人心 (楼主)
    2021-02-11 10:02

    You have registered the broadcast receiver in the AndroidManifest.xml with an Intent Filter name

    First of all, you may provide there any String. Above suggest that you are using this constant, but in fact you are just using the String as is (BluetoothDevice.ACTION_FOUND). I would recommend changing it to some com.testapp.samsungoscan.ACTION_FOUND. The Extras are not needed and they suggest they are in fact extras, but they are just another action names with name "extra".

    I recommend changing to:

    
        
             
        
    
    

    Secondly, you have to create the PendingIntent with this action name:

    private fun getPendingIntent(): PendingIntent {
        return PendingIntent.getBroadcast(
                this, REQ_CODE,
                Intent(this.applicationContext, BleReceiver::class.java).setAction("com.testapp.samsungoscan.ACTION_FOUND"),
                PendingIntent.FLAG_UPDATE_CURRENT)
    }
    

    Creating an intent with providing Component name is required since Oreo to be able to get any broadcasts. I'm not sure, however, will it work after your app has been killed by the system.

    Lastly, I recommend requesting FINE_LOCATION, as this: https://android-review.googlesource.com/c/platform/packages/apps/Bluetooth/+/848935 change may in the future require fine location permission to scan for Bluetooth devices.

提交回复
热议问题