0

iBeacon 协议将信号强度或测量功率作为数据包的最后一个字节。有没有办法获得这个价值?

4

2 回答 2

1

不幸的是,iOS 没有提供读取这个值的方法。CoreLocation 不提供对该字段的访问,CoreBluetooth 阻止访问 iBeacon 广告的原始字节。具有讽刺意味的是,您可以在 MacOS、Android、Windows 和 Linux 设备上读取此字节,而在 iOS 上则不行。

您可以阅读 CLBeacon rssi 属性,它为您提供检测到的信号强度。但您可能知道,这与信标数据包内传输的测量功率字节不同,信标数据包会告诉您 1 米处的预期信号强度。

令人沮丧的是,iOS 不允许访问该字段。

于 2018-10-13T03:18:56.647 回答
0

根据苹果的官方文件,RSSI 被认为是信号强度。

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

Declaration
@property(readonly, nonatomic) NSInteger rssi;

在 Objective-c 代码中,您需要添加两个标头

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

在 .m 中,您应该添加他们需要的委托:

CBPeripheralManagerDelegate,
CLLocationManagerDelegate

那么你应该创建三个对象

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

locationManager 的实例如下:

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

信标是这样的实例:

_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 像这样被安装:

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

在viewdidload中,调整定位服务是否ok,然后进行下一步:

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

    }
}

当找到 iBeacon 设备时,可以调用这个委托方法:

//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);
}

}

这个beacon.rssi是信号强度,希望对您有所帮助。

于 2018-10-15T08:05:30.850 回答