I want to scan BLE advertisers passively within my Android App.
But I couldn't found how to do this.
According Bluetooth 4.0 Core spec, There exists passive scan mode.
Vol 6 : Core System Package [Low Energy Controller volume],
Part D:4.1 PASSIVE SCANNING
https://www.bluetooth.org/DocMan/handlers/DownloadDoc.ashx?doc_id=282159
"A device can use passive scanning to find advertising devices in the area."And also, android has parameter that determines scan type. (Active/Passive)
http://androidxref.com/4.3_r2.1/xref/external/bluetooth/bluedroid/stack/btm/btm_ble_gap.c#555
"scan_type: active scan or passive scan"Meanwhile, iOS can scan ble advertisers passively. (sadly, background mode only)
http://lists.apple.com/archives/bluetooth-dev/2012/May/msg00041.html
"When the app is in the background, iOS performs passive scanning."
But I cannot found whether it is possible to use passive scan mode or not.
Question : Is it possible to use "PASSIVE SCAN" on Android? If it is possible, How to use this feature?
The difference between
active
andpassive
scan is thatactive
scans requests aSCAN_RESPONSE
paket from the advertiser. This is done by sending aSCAN_REQUEST
paket after an advertisements has been detected. The information (payload) of both will be in thescanRecord
parameter of the device found callback.From the core spec:
So, for any usecase it will not be necessary to differ between those two scanning types.
But if you want to listen for advertisements in the background then you need to do this yourself by creating a
Service
- there is no built in functionality (as of Android 4.4).For background scanning take this example. But the scan will end atthe point your app is killed by the system(or stopped by the user).
Starting a PendingIntent through AlarmManager (anywhere in your app, that has to be run at least once to start the service...)
BleScanService
The constants are just Strings to distribute the intent action and extra names. There is another preferences store that stores the how long the scanning phase should be...You can easily replace it ba your needs.
Then you have to registera broadcast reciever with an intent filter that matches the above action name (
BleServiceConstants.ACTION_DEVICE_DISCOVERED
)Find btif_gatt_client.c in AOSP repo, Edit it,
replace
with
then build AOSP, flash the image to phone, then passive scan works.