Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to correctly implement didReceiveWriteRequests? iOS6 CoreBluetooth

I implement iOS CoreBluetooth client & server for sending data

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

and

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

and server site

- (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];
}

however, client can't receive any data. Any idea? Thanks for your help.

like image 257
Ken Avatar asked Jan 04 '13 00:01

Ken


1 Answers

According to Apple's doc on CoreBluetooth, you should use:

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

to reply to the central.This is also a choice when you receive a reading request

like image 182
XuTao Avatar answered Oct 23 '22 10:10

XuTao