Android-Scanner-Compat-Library icon indicating copy to clipboard operation
Android-Scanner-Compat-Library copied to clipboard

When scanning by pendingIntent with optional extra, extra of broadcastreceiver intent is overwritten, so EXTRA_LIST_SCAN_RESULT data is lost.

Open jeddchoi opened this issue 2 years ago • 1 comments

// in BroadcastReceiver(MyReceiver)
override fun onReceive(context: Context, intent: Intent) {
        Log.e(TAG, "onReceive: ${intent.extras?.getInt("my.extra.data")}")
        Log.e(TAG, "onReceive: ${intent.action}")
        Log.e(TAG, "onReceive: ${intent.hasExtra(BluetoothLeScanner.EXTRA_LIST_SCAN_RESULT)}")
        Log.e(TAG, "onReceive: ${intent.getParcelableArrayListExtra<ScanResult>(BluetoothLeScanner.EXTRA_LIST_SCAN_RESULT)?.size}")
}
  1. start scan without extra
        requestCode++

        val intent = Intent(context, MyReceiver::class.java) // explicit intent

        intent.action = ACTION_FOUND_DEVICE

        val pendingIntent = PendingIntent.getBroadcast(
            context,
            requestCode,
            intent,
            PendingIntent.FLAG_UPDATE_CURRENT
        )

        BluetoothLeScannerCompat.getScanner().startScan(
            getScanFilter(),
            getScanSettings(),
            context,
            pendingIntent,
            requestCode
        )

Result(Logs in MyReceiver) :

onReceive: 0
onReceive: ACTION_FOUND_DEVICE
onReceive: true
onReceive: 1

As you can see, extra of EXTRA_LIST_SCAN_RESULT exist and extra of "my.extra.data" is 0(which is default value of Int).

  1. start scan with extra data
        requestCode++

        val intent = Intent(context, MyReceiver::class.java) // explicit intent

        intent.action = ACTION_FOUND_DEVICE
        intent.putExtra("my.extra.data", 100) // INPUT OPTIONAL EXTRA

        val pendingIntent = PendingIntent.getBroadcast(
            context,
            requestCode,
            intent,
            PendingIntent.FLAG_UPDATE_CURRENT
        )

        BluetoothLeScannerCompat.getScanner().startScan(
            getScanFilter(),
            getScanSettings(),
            context,
            pendingIntent,
            requestCode
        )

Result(Logs in MyReceiver) :

onReceive: 100
onReceive: ACTION_FOUND_DEVICE
onReceive: false
onReceive: null

Extra of EXTRA_LIST_SCAN_RESULT is null and extra of "my.extra.data" is 100(which is value I put on starting scan).

How can I put custom extra? In addition, when I tried with immutable request code, result was same.

jeddchoi avatar Mar 24 '22 01:03 jeddchoi

I think #66 is related with this

jeddchoi avatar Mar 24 '22 01:03 jeddchoi