2

I am a fresh man in android. I want to set BLE scan interval and windows in my android device in order to do a experiment about neighbour discovery. I search the answer on google and know that there are some definition in android's source code about BLE scan interval and windows as below

/**
 * Scan params corresponding to regular scan setting
 */
private static final int SCAN_MODE_LOW_POWER_WINDOW_MS = 500;
private static final int SCAN_MODE_LOW_POWER_INTERVAL_MS = 5000;
private static final int SCAN_MODE_BALANCED_WINDOW_MS = 2000;
private static final int SCAN_MODE_BALANCED_INTERVAL_MS = 5000;
private static final int SCAN_MODE_LOW_LATENCY_WINDOW_MS = 5000;
private static final int SCAN_MODE_LOW_LATENCY_INTERVAL_MS = 5000;

/**
 * Scan params corresponding to batch scan setting
 */
private static final int SCAN_MODE_BATCH_LOW_POWER_WINDOW_MS = 1500;
private static final int SCAN_MODE_BATCH_LOW_POWER_INTERVAL_MS = 150000;
private static final int SCAN_MODE_BATCH_BALANCED_WINDOW_MS = 1500;
private static final int SCAN_MODE_BATCH_BALANCED_INTERVAL_MS = 15000;
private static final int SCAN_MODE_BATCH_LOW_LATENCY_WINDOW_MS = 1500;
private static final int SCAN_MODE_BATCH_LOW_LATENCY_INTERVAL_MS = 5000;

If I want to change the values, does it mean that I should compile the android system? Is there a better way?

nikefd
  • 33
  • 1
  • 5
  • You can use the deprecated [startLeScan](https://developer.android.com/reference/android/bluetooth/BluetoothAdapter.html#startLeScan(android.bluetooth.BluetoothAdapter.LeScanCallback)) and [stopLeScan](https://developer.android.com/reference/android/bluetooth/BluetoothAdapter.html#stopLeScan(android.bluetooth.BluetoothAdapter.LeScanCallback)) and control everything yourself. – Markus Kauppinen Nov 21 '16 at 13:05
  • @MarkusKauppinen Got it. Thank you very much! I think if it is possible to control the ble Advertise interval? I just find three mode [here](https://developer.android.com/reference/android/bluetooth/le/AdvertiseSettings.Builder.html#setAdvertiseMode(int)). Or is it a way to advertise once so I can control the advertise interval. Thank you again! – nikefd Nov 22 '16 at 04:46
  • Those modes are not related to scanning, but you probably meant the `ScanSettings`. They are also related only to the new way of scanning and with the old one you can decide how often to scan and for how long to scan. Just start it with `startLeScan` and stop it with `stopLeScan`. There's a quite good example of this "old way of scanning" at [https://kittensandcode.blogspot.fi/2014/08/ibeacons-and-android-parsing-uuid-major.html](https://kittensandcode.blogspot.fi/2014/08/ibeacons-and-android-parsing-uuid-major.html). That code handles iBeacons, but of course any BLE devices are detected. – Markus Kauppinen Nov 22 '16 at 08:21
  • I understand what you said and I know how to set scan interval now. Thank you very much. And I have a similar question about how to set BLE transmitter interval. I wonder if there have a method to transmitter once so that I can control the transmitter interval. But I can't find it. There just three mode that I can choose to set. The advertise interval is 1000, 250, 100ms written in [line 234](http://androidxref.com/5.0.0_r2/xref/packages/apps/Bluetooth/src/com/android/bluetooth/gatt/AdvertiseManager.java). It is really kind of you! – nikefd Nov 22 '16 at 11:24
  • @MarkusKauppinen I am sorry I forget to @ you in the last comment. I don't know if you can get the notification if I don't @ you. – nikefd Nov 22 '16 at 11:33
  • I think that you are stuck with the pre-defined advertising intervals. – Markus Kauppinen Nov 22 '16 at 11:57
  • 5 seconds (5000 millis) interval isn't really what I call **"low latency"** – Louis CAD Apr 25 '17 at 09:09

1 Answers1

2

You are free to control the equivalent if scan window and interval yourself in higher-level code by starting and stopping scanning. This is exactly what the open source Android Beacon Library does with its equivalent configurable parameters:

 beaconManager.setForegroundScanPeriod(...);
 beaconManager.setForegroundBetweenScanPeriod(...);
davidgyoung
  • 63,876
  • 14
  • 121
  • 204
  • Thank you very much. You suggestions is great! I have another question about advertise interval. I think if it is possible to control the ble Advertise interval? I just find `setAdvertiseMode` in [Android Beacon Library](http://altbeacon.github.io/android-beacon-library/javadoc/org/altbeacon/beacon/BeaconTransmitter.html) but can't find some method to set advertise interval like `setForegroundScanPeriod`. Thank you again! – nikefd Nov 22 '16 at 04:51
  • You want to control the transmission rate? E.g. 1 Hz vs 10 Hz? – davidgyoung Nov 23 '16 at 04:26
  • Yes, is it possible to do it? @davidgyoung – nikefd Nov 23 '16 at 05:15
  • Yes, it is possible. See here: https://altbeacon.github.io/android-beacon-library/javadoc/org/altbeacon/beacon/BeaconTransmitter.html#setAdvertiseMode(int) – davidgyoung Nov 25 '16 at 06:39
  • I am really sorry that I don't explain my question clear, I want to control the advertise interval like `setForegroundBetweenScanPeriod`, not just choose from three mode. For example, I want to set the advertise interval to 20ms. @davidgyoung – nikefd Nov 25 '16 at 08:39
  • Unfortunately the Android OS does not give you the granularity to set the advertising rate at specific intervals, only the preset options mentioned in the link above. – davidgyoung Nov 28 '16 at 01:27