Android-Scanner-Compat-Library
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.
// 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}")
}
- 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).
- 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.
I think #66 is related with this