2013-01-04 90 views
19

我實現的iOS客戶端CoreBluetooth服務器&用於發送數據如何正確實現didReceiveWriteRequests? iOS6的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]; 
} 

但是,客戶端無法接收任何數據。 有什麼想法? 感謝您的幫助。

+1

你發現任何回答您的問題?我也有同樣的問題 :( – eter

回答

-2

要使用:

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

首先需要使用

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

從客戶端訂閱它。

2

根據蘋果的CoreBluetooth文檔,你應該使用:

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

回覆的central.This也是一種選擇,當你收到讀請求