1

I want to put custom data, like User ID, into advdata of iBeacon packet. Is it possible? If so, how can I get the data. CLBeaconRegion seems not to have the advdata property.

Please take a look the great sample of iBeacon below. https://github.com/suwa-yuki/BeaconSample/blob/master/BeaconSender/BeaconSender/BSViewController.m

As far as I can see, I may put custom measuredPower into the advertisement data. Can I use this property as a custom variable? I mean I want to put UserId into the measuredPower property. Does it affect RSSI or other things?

Thanks,

zono
  • 8,366
  • 21
  • 75
  • 113

1 Answers1

8

You can put any bytes into an iBeacon advertisement that you wish, but iOS severely limits which of these bytes you can read. If you need to use iOS to read these data, you only have four bytes of bluetooth advdata you can change that will be readable by iOS. If you only care about reading the bytes with other operating systems (OSX Mavericks, Android, Linux) then this is not a problem.

To explain the limits of iOS, it helps to take a look at the advdata of a typical iBeacon advertisement, which looks like this:

4C 00 # Company identifier code (0x004C == Apple)
02 15 # iBeacon advertisement indicator
e2 c5 6d b5 df fb 48 d2 b0 60 d0 f5 a7 10 96 e0 # ProximityUUID
00 00 # Major 
00 00 # Minor 
c5 # The 2's complement of the MeasuredPower

iOS does not allow you to access the raw bytes of the advdata shown above using its CoreBluetooth APIs. (See here for more details.) If you use the iBeacon ranging functions of CoreLocation, you can read the ProximityUUID (16 bytes), Major (2 bytes) and Minor (2 bytes) out of the advdata. The MeasuredPower that is set in the code example you mentioned is NOT directly readable in iOS. It simply is used as an input to the algorithm used to estimate distance from the RSSI. (See here for details.) Changing the MeasuredPower in the advertisement will affect this distance estimate, but you can not reliably determine the actual MeasuredPower value. So you cannot use it as a custom variable.

As described above, an iBeacon advertisement also includes a 16-byte ProximityUUID. But the iOS CoreLocation APIs don't let you see any iBeacon advertisements unless you specify the matching ProximityUUID up front. So it really isn't any use as a custom variable.

What can you use as a custom variable? The two values you can use for custom variables are the Major field and the Minor fields. Each of these is two bytes and if you tell iOS CoreLocation to range all iBeacons that have a given ProximityUUID, you will get ranging updates telling you the Major and Minor values for each, and you can encode any data in these fields you want. To access these, use simply read the values like this:

-(void)locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray *)beacons inRegion:(CLBeaconRegion *)region {
    for (CLBeacon *iBeacon in iBeacons) {
        // write code here to do something with iBeacon.major and iBeacon.minor
        // which each will have values from 0-65535
    }        
}

All of the above is an iOS-only restriction. Other platforms let you see the full bytes of the advdata, so you can use any of the bytes you want as custom variables. If you still want them to be recognized as iBeacons, however, you should leave the second two bytes (the iBeacon advertisement indicator) alone.

For custom variables, the best practice is to use lookup table to tie the standard iBeacon identifiers (ProximityUUID, Major, Minor) to data fields. This lookup table can be embedded in your app, or it can use a web service like Radius Networks' ProximityKit to do do this automatically.

Full disclosure: I am Chief Engineer for Radius Networks.

Community
  • 1
  • 1
davidgyoung
  • 63,876
  • 14
  • 121
  • 204
  • I didn't think of that I can use the major or minor as a custom variable. The 2bit is enough value to specific identity in my case and the number of beacons is lower than 65536. Therefore, I can use the minor value as a custom variable. Thanks so much! – zono Mar 22 '14 at 16:23
  • As far as I understand it, the minor is suppsed to be THE custom variable. – Hermann Klecker Mar 22 '14 at 18:04
  • @davidgyound Hello, I would like to ask if this information still holds. I can see that the answer is over a year and half old. Were there any changes or on iOS you can still only use 4 bytes for variable information? – Dávid Kaya Dec 15 '15 at 14:34
  • I think this is still true. You may have some more specifics you want to ask (I saw you added more info in a deleted answer.) I did have some further thoughts on your other points if you want to ask a new question and link to it from here. – davidgyoung Dec 15 '15 at 20:35