don / cordova-plugin-ble-central

Bluetooth Low Energy (BLE) Central plugin for Apache Cordova (aka PhoneGap)
Apache License 2.0
941 stars 601 forks source link

BLE scan android when screen is locked #987

Closed younesspotmaster closed 8 months ago

younesspotmaster commented 8 months ago

Since android 8.1.0, android doesn't allow scanning without filters when the phone is locked. A work around for this is adding an empty filter. More info in this stackoverflow: https://stackoverflow.com/questions/48077690/ble-scan-is-not-working-when-screen-is-off-on-android-8-1-0

Is it possible to add an option for adding an empty filter? Or change the standard empty services array [] to use an empty filter instead of no filter.

peitschie commented 8 months ago

Hi @younesspotmaster

I agree the plugin currently doesn't support this use case. I'd definitely accept a patch though to add this.

The best approach here would be to add a forceScanFilter option to the https://github.com/don/cordova-plugin-ble-central#startscanwithoptions parameter list. This flag could then be used at https://github.com/don/cordova-plugin-ble-central/blob/master/src/android/BLECentralPlugin.java#L1251 to ensure a filter is created if none already exist.