1

I am trying to scan for Beacons in Android. I am able to detect beacons, but the problem here is that i want only beacons with particular UUID to be scanned. So I used the following method:

startLeScan(UUID[], BluetoothAdapter.LeScanCallback) 

instead of

startLeScan(BluetoothAdapter.LeScanCallback) 

to scan for particular beacons only. But it is not scanning any beacons. Can anyone tell how to implement this functionality.

HeadOnn
  • 1,480
  • 14
  • 21

1 Answers1

1

Do not confuse a Bluetooth Service UUID with an iBeacon Proximity UUID. They may look alike, but have completely different values and purposes.

The startLeScan method you mention takes an optional service UUID to filter the scan for Bluetooth devices offering a particular service. This will not work to scan for iBeacons, which do not advertise specific services, and need extra software to decode their fields.

To scan for iBeacons with a particular Proximity UUID and decode their values, try the open source Android iBeacon Library which is designed to do exactly what you describe. Setting up a filter for a single Proximity UUID is as simple as:

iBeaconManager.startMonitoringBeaconsInRegion(new Region("myRegion", "2F234454-CF6D-4A0F-ADF2-F4911BA9FFA6", null, null));

A full example can be seen here.

If you want to roll your own, then you need to decode every bluetooth advertisement returned to the BluetoothAdapter.LeScanCallback, discard non-iBeacon advertisements, then compare the Proximity UUID of each iBeacon advertisement against the one you are looking for, and ignore any that do not match. The open source library mentioned above has a fromScanData method that decodes an advertisement into iBeacon fields here.

davidgyoung
  • 63,876
  • 14
  • 121
  • 204
  • Thanks for your help. So you mean that device will scan all proximity UUIDs and I have to compare the uuid from scan result with my custom uuid to get results for particular beacon? – HeadOnn Jun 11 '14 at 12:52
  • Yes, see my edited answer, along with a link to code that will convert the advertisement data to iBeacon fields. – davidgyoung Jun 11 '14 at 13:00
  • 1
    Thanks. Also can you tell me how to increase the scan time to 1 second like ios? In Android it is scanning in milli seconds. – HeadOnn Jun 11 '14 at 13:22
  • I'm not sure exactly what you mean by "1 second like iOS". You should understand that if you use Android's `startLeScan` method, it will continue scanning *forever* until you stop it. The equivalent method on iOS works the same way. The iOS CoreLocation ranging functions do provide updates every 1s, but these are a higher API functionally equivalent to what the Android iBeacon Library does with its own ranging updates approximately every second. Regardless, this is a bit off the original topic so you should definitely post this as a new question. – davidgyoung Jun 11 '14 at 18:02
  • Ok. I've posted the new question. Please have a look at the following link: http://stackoverflow.com/questions/24180849/how-to-increase-the-scan-period-for-ble-devices-in-android – HeadOnn Jun 12 '14 at 09:26
  • @HeadOnn Are you able to get any solution on making android device scan for BLE for every sec instead of millis ?? Please let me know – KK_07k11A0585 Jul 26 '17 at 07:19
  • As @davidyoung has mentioned here https://stackoverflow.com/questions/24180849/how-to-increase-the-scan-period-for-ble-devices-in-android, I was able to stop the timer, and restart every 1 second, so that I am not checking every say < 100 ms instead of 1 second. – HeadOnn Jul 26 '17 at 12:56