如何正确实现didReceiveWriteRequests? iOS6 CoreBluetooth

时间:2013-01-04 00:27:07

标签: ios ios6 bluetooth core-bluetooth

我实现iOS CoreBluetooth客户端&用于发送数据的服务器

client site
[self.connectedPeripheral writeValue:mainData forCharacteristic:characteristic type:CBCharacteristicWriteWithResponse];

- (void)peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic:(CBCharacteristic *)characteristic 
{
   NSString *s= [[NSString alloc] initWithData:characteristic.value encoding:NSUTF8StringEncoding];
   NSLog(@"didWriteValue characteristic.value: %@ ", s);
} 

和服务器站点

- (void)peripheralManager:(CBPeripheralManager *)peripheral didReceiveWriteRequests:(NSArray *)requests
{
  NSData *res= [[NSString stringWithFormat:@"Hello"] dataUsingEncoding:NSUTF8StringEncoding];
 [self.peripheral updateValue:res
                        forCharacteristic:self.writeCharacteristic
                     onSubscribedCentrals:nil];
 [peripheral respondToRequest:aReq withResult:CBATTErrorSuccess];
}

但是,客户端无法接收任何数据。 任何的想法? 谢谢你的帮助。

2 个答案:

答案 0 :(得分:2)

根据Apple在CoreBluetooth上的文档,你应该使用:

- (void)respondToRequest:(CBATTRequest *)request withResult:(CBATTError)result;

回复中心。当您收到阅读请求时,这也是一种选择

答案 1 :(得分:-2)

使用:

- (BOOL)updateValue:(NSData *)value forCharacteristic:(CBMutableCharacteristic *)characteristic onSubscribedCentrals:(NSArray *)centrals;

首先需要使用

订阅它
- (void)setNotifyValue:(BOOL)enabled forCharacteristic:(CBCharacteristic *)characteristic;

来自客户端。