19

我实现了 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];
}

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

4

2 回答 2

2

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

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

回复中央。这也是收到阅读请求时的选择

于 2014-03-12T09:12:41.793 回答
-2

要使用:

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

您首先需要使用订阅它

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

从客户端。

于 2013-03-09T11:56:27.333 回答