所以我有一个外设应用程序在运行,我试图在一个特征上放一个简单的字符串。外围设备看起来像这样:
- (void)peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheral{
if (peripheral.state == CBPeripheralManagerStatePoweredOn) {
_notifyUUID = [CBUUID UUIDWithString:@"E098FDEF-B82C-43F1-8BFB-18757743BA10"];
_notificationCharacteristic = [[CBMutableCharacteristic alloc] initWithType:_notifyUUID
properties:CBCharacteristicPropertyNotify
value:nil
permissions:CBAttributePermissionsReadable];
_shareService = [[CBMutableService alloc] initWithType:_serviceUUID primary:YES];
[_shareService setCharacteristics:@[_notificationCharacteristic]];
[_pMgr addService:_shareService];
[self startAdvertising];
}
}
-(void) peripheralManager:(CBPeripheralManager *)peripheral didReceiveReadRequest:(CBATTRequest *)request {
request.value =
[@"Some Data" dataUsingEncoding:NSUTF16StringEncoding];
[_pMgr respondToRequest:request withResult:CBATTErrorSuccess];
}
然而,当我尝试查看特征上的数据并将其记录下来时,我看到的值为nil:
2015-09-24 12:57:36.875 BLEScanner[533:43084] Discovered characteristic <CBCharacteristic: 0x17546740, UUID = E098FDEF-B82C-43F1-8BFB-18757743BA10, properties = 0x10, value = (null), notifying = NO>
扫描仪来源:
-(void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error {
NSLog( @"didDiscoverService" );
for (CBService *service in peripheral.services) {
NSLog(@"Discovered service %@", service);
if( service == peripheral.services.lastObject ) {
[peripheral discoverCharacteristics:nil forService:service];
}
}
}
- (void)peripheral:(CBPeripheral *)peripheral
didDiscoverCharacteristicsForService:(CBService *)service
error:(NSError *)error {
for (CBCharacteristic *characteristic in service.characteristics) {
NSLog(@"Discovered characteristic %@", characteristic);
}
}
我不知道我错过了什么,因为我已经浏览了Apple网站上使用BLE的文档(https://developer.apple.com/library/ios/documentation/NetworkingInternetWeb/Conceptual/CoreBluetooth_concepts/PerformingCommonPeripheralRoleTasks/PerformingCommonPeripheralRoleTasks.html)