Question

I need to write a value from "a" to "o" to a bluetooth device. The device uses SPP and I'm already connected via IOBluetoothRFCOMMChannel. There are functions like writeSync:lenght: but how do I use them? As I said, I need to send a value from "a" to "o"

I tried:

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

but it isn't working.

Apple has an example code with:

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

but I'm not sure what "ATZ" means.

Was it helpful?

Solution

Found the answer:

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

For my specific case:

[self sendData:@"a" toChannel:self.rfcommChannel];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top