Scan Android Marshmallow ScanSetting

I would like to create my scan settings in Android M in order to receive separate callbacks for three different events in one ScanCallback object:

  • When the remote device first matched the filter
  • When the Android phone received advertisements after the first detection (in this case, I check whether the advertising data has changed or not)
  • When the remote device is out of range

There are three options in the API:

 CALLBACK_TYPE_ALL_MATCHES CALLBACK_TYPE_FIRST_MATCH CALLBACK_TYPE_MATCH_LOST 

Unfortunately, there is no good documentation on whether these three options can be used together or not. According to my experiments, I should choose only one of them. Does the API CALLBACK_TYPE_ALL_MATCHES us to separate the simultaneous scan to get a callback for CALLBACK_TYPE_ALL_MATCHES and CALLBACK_TYPE_MATCH_LOST at the same time? Does anyone have a good experience using these options?

EDIT

After checking the API source code, I found that CALLBACK_TYPE_FIRST_MATCH | CALLBACK_TYPE_MATCH_LOST CALLBACK_TYPE_FIRST_MATCH | CALLBACK_TYPE_MATCH_LOST also a valid combination. However, using CALLBACK_TYPE_FIRST_MATCH makes it significantly slower (~ 30 seconds) for detection compared to CALLBACK_TYPE_ALL_MATCHES . In addition, CALLBACK_TYPE_MATCH_LOST is called, although the device is actively advertising. I do not think this API is reliable.

Here are my results:

12: 02: 50.179 31627-31655 D / BluetoothLeScanner: onClientRegistered () - status = 0 clientIf = 6
12: 03: 24.877 31627-31627 D / ScanCallbackMarshmallow: ScanResult {mDevice = E8: A8: C9: 82: 17: 61, ...}
12: 03: 24.878 31627-31627 D / ScanCallbackMarshmallow: CALLBACK_TYPE_FIRST_MATCH
12: 03: 29.905 31627-31627 D / ScanCallbackMarshmallow: ScanResult {mDevice = C0: B6: 43: 1E: 45: 28, ...}
12: 03: 29.906 31627-31627 D / ScanCallbackMarshmallow: CALLBACK_TYPE_FIRST_MATCH
12: 03: 39.721 31627-31627 D / ScanCallbackMarshmallow: ScanResult {mDevice = D8: 35: 26: 9D: 55: B4, ...}
12: 03: 39.722 31627-31627 D / ScanCallbackMarshmallow: CALLBACK_TYPE_FIRST_MATCH

12: 04: 21.889 718-730 D / BluetoothLeScanner: onClientRegistered () - status = 0 clientIf = 6 12: 04: 22.073 718-718 D / ScanCallbackMarshmallow: ScanResult {mDevice = D8: 35: 26: 9D: 55: B4, ...} 12: 04: 22.073 718-718 D / ScanCallbackMarshmallow: CALLBACK_TYPE_ALL_MATCHES 12: 04: 22.316 718-718 D / ScanCallbackMarshmallow: ScanResult {mDevice = E8: A8: C9: 82: 17: 61, ...} 12 : 04: 22.316 718-718 D / ScanCallbackMarshmallow: CALLBACK_TYPE_ALL_MATCHES 12: 04: 24.740 718-718 D / ScanCallbackMarshmallow: ScanResult {mDevice = E8: A8: C9: 82: 17: 61, ...} 12: 04: 24.740 718 -718 D / ScanCallbackMarshmallow: CALLBACK_TYPE_ALL_MATCHES

EDIT 2 :

Setting the scan mode as ScanSettings.SCAN_MODE_LOW_LATENCY makes detection with CALLBACK_TYPE_FIRST_MATCH as fast as using CALLBACK_TYPE_ALL_MATCHES . However, if I set the callback type as ScanSettings.CALLBACK_TYPE_FIRST_MATCH | ScanSettings.CALLBACK_TYPE_MATCH_LOST ScanSettings.CALLBACK_TYPE_FIRST_MATCH | ScanSettings.CALLBACK_TYPE_MATCH_LOST , I will not be able to check whether the advertising data has changed or not, in case the advertising data is dynamic. I think this is a problem with the API. It should be noted that advertising data is not predictable, so I can not filter advertising data based on its content.

+5
source share

Source: https://habr.com/ru/post/1237555/


All Articles