我很困惑。
我使用 CLBeaconRegion 创建了一个 Beacon,并使用 CBPeripheralManager 对其进行广告:
- (void)startTransmitting:(NSUUID*)uuid major:(NSInteger)major minor:(NSInteger)minor identifier:(NSString*)identifier {
self.beaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:uuid
major:major
minor:minor
identifier:identifier];
self.beaconPeripheralData = [self.beaconRegion peripheralDataWithMeasuredPower:nil];
self.peripheralManager = [[CBPeripheralManager alloc] initWithDelegate:self
queue:nil
options:nil];
}
-(void)peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheral {
if (peripheral.state == CBPeripheralManagerStatePoweredOn) {
NSLog(@"Powered On");
[self.peripheralManager startAdvertising:self.beaconPeripheralData];
} else if (peripheral.state == CBPeripheralManagerStatePoweredOff) {
NSLog(@"Powered Off");
[self.peripheralManager stopAdvertising];
}
}
而且我能够通过 CBCentralManager 接收 iBeacon:
self.centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
[self.centralManager scanForPeripheralsWithServices:nil options:nil];
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI {
[self.peripherals addObject:peripheral];
NSLog(@"New Peripheral found and added... %@", peripheral);
}
这基本上有效。但是传输的和接收到的 UUID 是不同的——在我看来应该是相同的。
--> 我做错了什么/理解错了什么??
最佳答案
问题是您使用 CBCentralManager
读取的 UUID 与 iBeacon 的 ProximityUUID 无关。
尽管在两个领域都使用术语 UUID,但它们是完全不同的。您可以使用 CBCentralManager
看到的字段只是 iOS 生成的特定于 session 的标识符。如果您以后使用相同的 API 发现相同的设备,它将是不同的值。
不幸的是,无法使用 CoreBluetooth
API 读取 iBeacon 标识符。有关原因的更多信息,请参阅 this blog post .
关于ios - iBeacons 发送和接收 UUID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23364601/