我需要将“a”到“o”的值写入蓝牙设备。该设备使用 SPP ,我已通过IOBluetoothRFCOMMChannel
连接。
有writeSync:lenght:
等功能,但我如何使用它们?正如我所说,我需要将值从“a”发送到“o”
我试过了:
[rfcommChannel writeSync:"a" length:1];
但它不起作用。
Apple有一个示例代码:
[rfcommChannel writeSync:"ATZ\n" length:4];
但我不确定“ATZ”是什么意思。
答案 0 :(得分: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];