I am working on BLE and have to scan the BLE devices with specific udid.
Currently, All BLE supported devices comes as a scan result.
To avoid and scan only specific devices, what I have done is as below :
fun startScanDevices(interval: Long = Constants.DEFAULT_SCAN_TIME_OUT,
connectedDevices: HashMap<String, BLEDeviceModel>? = null,
scanWithId: String? = null, context: Activity? = null) {
this.context = context
if (scanWithId != null)
this.scanWithId = scanWithId
if (isBluetoothEnabled) {
clearDeviceList(connectedDevices)
val BLP_SERVICE_UUID = UUID.fromString(UUID_OF_DEVICE)
val serviceUUIDs = arrayOf<UUID>(BLP_SERVICE_UUID)
var filters: MutableList<ScanFilter?>? = null
if (serviceUUIDs != null) {
filters = ArrayList()
for (serviceUUID in serviceUUIDs) {
val filter: ScanFilter = ScanFilter.Builder()
.setServiceUuid(ParcelUuid(serviceUUID))
.build()
filters!!.add(filter)
}
}
val scanSettings: ScanSettings = ScanSettings.Builder()
.setScanMode(ScanSettings.SCAN_MODE_LOW_POWER)
.setCallbackType(ScanSettings.CALLBACK_TYPE_ALL_MATCHES)
.setMatchMode(ScanSettings.MATCH_MODE_AGGRESSIVE)
.setNumOfMatches(ScanSettings.MATCH_NUM_ONE_ADVERTISEMENT)
.setReportDelay(1L)
.build()
mBluetoothAdapter?.bluetoothLeScanner?.startScan(filters,scanSettings,scanCallback)
mScanning = true
handler.postDelayed(stopScanningRunnable, interval)
}
}
Now, here in above method, you can verify that I am passing filters and scanSettings as a arguments of startScan method.
But with these two arguments I am getting below error :
2021-05-06 11:16:24.295 14669-14669/? E/AndroidRuntime: FATAL EXCEPTION: main Process: com.dev, PID: 14669 java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 at java.util.ArrayList.get(ArrayList.java:437) at com.ble.BLEManager$scanCallback$1.onBatchScanResults(BLEManager.kt:153) at android.bluetooth.le.BluetoothLeScanner$BleScanCallbackWrapper$2.run(BluetoothLeScanner.java:627) at android.os.Handler.handleCallback(Handler.java:938) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:246) at android.app.ActivityThread.main(ActivityThread.java:8506) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)
This is working fine with single arguments in startScan() as below - not getting this error :
mBluetoothAdapter?.bluetoothLeScanner?.startScan(scanCallback)
So, Error is in below method :
override fun onBatchScanResults(results: List<ScanResult>) {
results[0].device
super.onBatchScanResults(results)
}
EDIT
Removed this unnecessary Method named : onBatchScanResults since I am using onScanResult.
Now, Error is not coming but, not able to scan any devices with the filter and scanSettings.
What might be the issue? Please guide. Thanks.