我有两个应用程序,一个作为外围设备(在运行iOS 7的iPad第四代)和一个作为中心(运行iOS7的iPhone 5c).
我希望在外围设备中具有可写特性,中央将少量字节写入(大约4个字节但可能因执行而异).
我正在配置外设如下:
-(void)startPeripheral { CBMutableService *service = [[CBMutableService alloc]initWithType:[CBUUID UUIDWithString:kMyServiceUUID] primary:YES]; CBMutableCharacteristic *ch = [[CBMutableCharacteristic alloc]initWithType:[CBUUID UUIDWithString:kMyWriteUUID] properties:CBCharacteristicPropertyWrite value:nil permissions:CBAttributePermissionsWriteable]; service.characteristics = @[ch]; NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:@[[CBUUID UUIDWithString:kMyServiceUUID]],CBAdvertisementDataServiceUUIDsKey,@"My Peripheral",CBAdvertisementDataLocalNameKey,nil]; [self.peripheralManager addService:service]; [self.peripheralManager startAdvertising:dict]; NSLog(@"Service = %@",service); }
这一切似乎都起作用,外围设备宣传服务,我的中心可以扫描,查找,发现服务和特性,并找到它感兴趣的特性.当我尝试写入特性时出现问题.
在中央我有以下方法:
-(void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error { NSLog(@"Service/Characteristics = %@",service.characteristics); for(CBCharacteristic *ch in service.characteristics) { NSLog(@"Characteristic properties = %x",ch.properties); NSLog(@"Characterisctic = %@",ch.UUID); if([ch.UUID isEqual:[CBUUID UUIDWithString:kMyWriteUUID]]) { // We've found our write characteristic NSLog(@"Writing data to characteristic %@",ch.UUID); NSData *data = [NSData dataWithBytes:(Byte[]){1,2,3,4} length:4]; [peripheral writeValue:data forCharacteristic:ch type:CBCharacteristicWriteWithResponse]; } } }
我得到一个回调didWriteValueForCharacteristic,它指示错误:
错误域= CBATTErrorDomain代码= 14“不太可能出错.”
哪个不是很有帮助.
有人能发现并发布或建议任何解决方案吗?
谢谢,Rich
解决方法
我不明白错误是什么,但是:
>您应该在收到peripheralManager后才开始做广告:didAddService:error:callback.>您可以附加相应的设备控制台日志吗? (使用Organizer-> Phone->控制台)