iOS 下 altBeacons 的主要和次要
Major and Minor on altBeacons under iOS
有没有办法找出 altBeacon 的主要和次要值?
我正在使用这个库 (https://github.com/CharruaLab/AltBeacon) 来检测附近的 BLE 设备(无论它是否有信标)。
检测到信标,我知道它们的 uuid。
我可以使用 iOS CoreBluetooth 框架提取主要值和次要值吗?
更新:
代码看起来不错。看起来找出主要或次要的唯一方法是请求信标的数据,因为它在下面接受的答案中完成。但是在这种情况下,信标本身不一定会响应您。
仅适用于 iBeacon:
重写下面的 CLLocationManager
委托方法,此方法将在您能够检测到信标时调用。
-(void)locationManager:(CLLocationManager*)manager
didRangeBeacons:(NSArray*)beacons
inRegion:(CLBeaconRegion*)region {
// Beacon found!
CLBeacon *foundBeacon = [beacons firstObject];
// You can retrieve the beacon data from its properties
NSString *uuid = foundBeacon.proximityUUID.UUIDString;
NSString *major = [NSString stringWithFormat:@"%@", foundBeacon.major];
NSString *minor = [NSString stringWithFormat:@"%@", foundBeacon.minor];
}
要使用核心定位框架检测信标,您需要在 viewDidLoad
方法中添加以下代码。
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// Initialize location manager and set ourselves as the delegate
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
// Create a NSUUID with the same UUID as the broadcasting beacon
NSUUID *uuid = [[NSUUID alloc] initWithUUIDString:@"XXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX"];
// Setup a new region with that UUID and same identifier as the broadcasting beacon
self.myBeaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:uuid
identifier:@"com.app.identifier"];
// Tell location manager to start monitoring for the beacon region
[self.locationManager startMonitoringForRegion:self.myBeaconRegion];
// Check if beacon monitoring is available for this device
if (![CLLocationManager isMonitoringAvailableForClass:[CLBeaconRegion class]]) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Monitoring not available" message:nil delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles: nil];
[alert show];
}
}
对于蓝牙设备:
使用两个委托 CBCentralManagerDelegate
和 CBPeripheralDelegate
扩展视图控制器
在 viewDidLoad
中创建 CBCentralManager
对象。
_centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
并覆盖以下方法(scan
方法除外):
- (void)centralManagerDidUpdateState:(CBCentralManager *)central {
if (central.state != CBCentralManagerStatePoweredOn) {
return;
}
// The state must be CBCentralManagerStatePoweredOn...
// ... so start scanning
[self scan];
}
- (void) scan {
// UUID for peripheral services
[self.centralManager scanForPeripheralsWithServices:[NSArray arrayWithObject:[CBUUID UUIDWithString:@"XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXX"]] options:nil];
NSLog(@"Scanning started");
}
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI {
NSLog(@"Discovered %@ at %@", peripheral.name, RSSI);
if (self.discoveredPeripheral != peripheral) {
// Save a local copy of the peripheral, so CoreBluetooth doesn't get rid of it
self.discoveredPeripheral = peripheral;
// And connect
NSLog(@"Connecting to peripheral %@", peripheral.UUID);
[self.centralManager connectPeripheral:peripheral options:nil];
}
}
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral {
NSLog(@"Peripheral Connected");
// Stop scanning
[self.centralManager stopScan];
NSLog(@"Scanning stopped");
// Make sure we get the discovery callbacks
peripheral.delegate = self;
[peripheral discoverServices:nil];
}
有没有办法找出 altBeacon 的主要和次要值? 我正在使用这个库 (https://github.com/CharruaLab/AltBeacon) 来检测附近的 BLE 设备(无论它是否有信标)。 检测到信标,我知道它们的 uuid。 我可以使用 iOS CoreBluetooth 框架提取主要值和次要值吗?
更新: 代码看起来不错。看起来找出主要或次要的唯一方法是请求信标的数据,因为它在下面接受的答案中完成。但是在这种情况下,信标本身不一定会响应您。
仅适用于 iBeacon:
重写下面的 CLLocationManager
委托方法,此方法将在您能够检测到信标时调用。
-(void)locationManager:(CLLocationManager*)manager
didRangeBeacons:(NSArray*)beacons
inRegion:(CLBeaconRegion*)region {
// Beacon found!
CLBeacon *foundBeacon = [beacons firstObject];
// You can retrieve the beacon data from its properties
NSString *uuid = foundBeacon.proximityUUID.UUIDString;
NSString *major = [NSString stringWithFormat:@"%@", foundBeacon.major];
NSString *minor = [NSString stringWithFormat:@"%@", foundBeacon.minor];
}
要使用核心定位框架检测信标,您需要在 viewDidLoad
方法中添加以下代码。
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// Initialize location manager and set ourselves as the delegate
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
// Create a NSUUID with the same UUID as the broadcasting beacon
NSUUID *uuid = [[NSUUID alloc] initWithUUIDString:@"XXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX"];
// Setup a new region with that UUID and same identifier as the broadcasting beacon
self.myBeaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:uuid
identifier:@"com.app.identifier"];
// Tell location manager to start monitoring for the beacon region
[self.locationManager startMonitoringForRegion:self.myBeaconRegion];
// Check if beacon monitoring is available for this device
if (![CLLocationManager isMonitoringAvailableForClass:[CLBeaconRegion class]]) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Monitoring not available" message:nil delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles: nil];
[alert show];
}
}
对于蓝牙设备:
使用两个委托 CBCentralManagerDelegate
和 CBPeripheralDelegate
在 viewDidLoad
中创建 CBCentralManager
对象。
_centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
并覆盖以下方法(scan
方法除外):
- (void)centralManagerDidUpdateState:(CBCentralManager *)central {
if (central.state != CBCentralManagerStatePoweredOn) {
return;
}
// The state must be CBCentralManagerStatePoweredOn...
// ... so start scanning
[self scan];
}
- (void) scan {
// UUID for peripheral services
[self.centralManager scanForPeripheralsWithServices:[NSArray arrayWithObject:[CBUUID UUIDWithString:@"XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXX"]] options:nil];
NSLog(@"Scanning started");
}
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI {
NSLog(@"Discovered %@ at %@", peripheral.name, RSSI);
if (self.discoveredPeripheral != peripheral) {
// Save a local copy of the peripheral, so CoreBluetooth doesn't get rid of it
self.discoveredPeripheral = peripheral;
// And connect
NSLog(@"Connecting to peripheral %@", peripheral.UUID);
[self.centralManager connectPeripheral:peripheral options:nil];
}
}
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral {
NSLog(@"Peripheral Connected");
// Stop scanning
[self.centralManager stopScan];
NSLog(@"Scanning stopped");
// Make sure we get the discovery callbacks
peripheral.delegate = self;
[peripheral discoverServices:nil];
}