user856232
user856232

Reputation: 1113

How do I get the Signal Strength value from an iBeacon packet on iOS using Objective-C

The iBeacon protocol includes the Signal Strength or Measured Power as the last byte of the packet. Is there any way to get that value?

Upvotes: 0

Views: 698

Answers (2)

Junior Jiang
Junior Jiang

Reputation: 12723

According to Apple's official documents,the RSSI is regarded as Signal Strength.

Instance Property
rssi
The received signal strength of the beacon, measured in decibels.

Declaration
@property(readonly, nonatomic) NSInteger rssi;

In Objective-c code,you need to add the two header

#import <CoreLocation/CoreLocation.h>
#import <CoreBluetooth/CoreBluetooth.h>

and in .m you should add their delegate needed:

CBPeripheralManagerDelegate,
CLLocationManagerDelegate

then you should create three object

@property(nonatomic, strong)CLBeaconRegion *beacon; //iBeacon device be scaned
@property(nonatomic, strong)CLLocationManager *locationManager;//location manager
@property (strong, nonatomic) CBPeripheralManager *peripheralManager;//periphera manager

locationManager be instanced like this:

 _locationManager = [[CLLocationManager alloc] init];
_locationManager.delegate = self;
[_locationManager requestWhenInUseAuthorization];//set location be allow when use

beacon be instanced like this:

_beacon = [[CLBeaconRegion alloc] initWithProximityUUID:[[NSUUID alloc] initWithUUIDString:@"FDA50693-A4E2-4FB1-AFCF-C6EB07647825"] identifier:@"media"];
//FDA50693-A4E2-4FB1-AFCF-C6EB07647825 this modified be your need scaned device's UUID

peripheralManager be instacned like this:

self.peripheralManager = [[CBPeripheralManager alloc] initWithDelegate:self queue:nil options:nil];

in the viewdidload,adjust the location service whether is ok ,and then to do next thing:

BOOL enable = [CLLocationManager locationServicesEnabled];
if (enable) {
    if ([CLLocationManager isMonitoringAvailableForClass:[CLBeaconRegion class]])
    {
        [self.locationManager requestAlwaysAuthorization];
        [self.locationManager startMonitoringForRegion:_beacon];
        [self.locationManager startRangingBeaconsInRegion:_beacon];

    }
}

when find iBeacon device ,can invoke this delegate method:

//find IBeacon device then scan
- (void)locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray*)beacons i    nRegion:(CLBeaconRegion *)region{
//if not we need found deice then stop scan
if (![[region.proximityUUID UUIDString]           
isEqualToString:@"12334566-7173-4889-9579-954995439125"]) {
[_locationManager stopMonitoringForRegion:region];
[_locationManager stopRangingBeaconsInRegion:region];
}
//print all IBeacon information
for (CLBeacon *beacon in beacons) {
   NSLog(@"rssi is : %ld", beacon.rssi);// this is signal strength
   NSLog(@"beacon.proximity %ld", beacon.proximity);
}

}

this beacon.rssi is the signal strength ,i hope this can help you.

Upvotes: 0

davidgyoung
davidgyoung

Reputation: 64961

Unfortunately, iOS provides no way to read this value. CoreLocation does not provide access to this field, and CoreBluetooth blocks access to the raw bytes of iBeacon advertisements. Ironically, you can read this byte on MacOS, Android, Windows and Linux devices -- just not on iOS.

You can read the CLBeacon rssi property, which gives you the detected signal strength. But as you probably know this is not the same as the measured power byte transmitted inside the beacon packet that tells you the expected signal strength at 1 meter.

It is very frustrating that iOS does not allow access to this field.

Upvotes: 1

Related Questions