0

我需要将一个从“a”到“o”的值写入蓝牙设备。该设备使用SPP,我已经通过IOBluetoothRFCOMMChannel. 有类似的功能,writeSync:lenght:但我该如何使用它们?正如我所说,我需要将值从“a”发送到“o”

我试过了:

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

但它不起作用。

苹果有一个示例代码:

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

但我不确定“ATZ”是什么意思。

4

1 回答 1

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];
于 2012-12-12T15:19:18.000 回答