2012-12-12 16 views
0

我需要寫一個從「a」到「o」的值給藍牙設備。該設備使用SPP,我已通過IOBluetoothRFCOMMChannel連接。 有像writeSync:lenght:這樣的功能,但我該如何使用它們?正如我所說的,我需要從「一」到「O」IOBluetoothRFCOMMChannel:如何使用writeSync?

我試圖發送一個值:

[rfcommChannel writeSync:"a" length:1]; 

,但它無法正常工作。

蘋果有一個示例代碼:

[rfcommChannel writeSync:"ATZ\n" length:4]; 

,但我不知道什麼是 「ATZ」 的意思。

回答

0

找到了答案:

- (void) sendData:(NSString *)string toChannel:(IOBluetoothRFCOMMChannel*)rfcommChannel 
{ 
    int i; 
    // Turn the string into data. 
    NSData *data = [string dataUsingEncoding:NSASCIIStringEncoding]; 
    char buffer[ [data length] +4]; 
    char *bytes = (char *) [data bytes]; 
    // Add a CRLF to the start 
    buffer[0] = 13; 
    buffer[1] = 10; 
    // Copy the data into the buffer. 
    for (i=0;i<[data length];i++) 
    { 
     buffer[2+i] = bytes[i]; 
    } 
    // Append a CRLF 
    buffer[ [data length]+2] = 13; 
    buffer[ [data length]+3] = 10; 
    // Synchronously write the data to the channel. 
    [rfcommChannel writeSync:&buffer length:[data length]+4]; 
} 

對於我的具體情況:

[self sendData:@"a" toChannel:self.rfcommChannel];