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

CoreBluetooth に関する Apple のドキュメントによると、以下を使用する必要があります。

- (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 に答える