Mac OSX: IOBluetooth + Arduino: writeSync:length: data malformed?

Hi there,

I’m having trouble sending data from my mac to the bluetooth arduino board.

It appears that the data being sent is being malformed in some way, as the arduino sees random values for each of the bytes being sent from the mac.

NSData creation
I create a NSData object with the following structure.

byte 0: uint8_t tag
byte 1: uint8_t operation code
byte 2: uint8_t terminator

e.g. 0x01 0x20 0xff

I check that the data has not been malformed in any way before I call

[ writeSync:&data length:[data length]];

The data is always ok.

Arduino Serial Read
For debugging purposes I do the following - for every byte that the Arduino board receives, it will return 32 bytes of data with the incoming byte as the first byte, followed by 30 0x00’s then a terminator byte. See below.

void serialEvent() {
  if (Serial.available() > 0) {
    int readByte =;

void sendMessage(byte tag) {
  byte message[32];
  message[0] = tag;//tag;

  int maxCounter = 32;
  for( int counter = 1; counter < maxCounter; counter++ ) {
    if( counter < maxCounter - 1 ) {
      message[counter] = op_reservered;
    } else {
      message[counter] = op_terminator;

  Serial.write(message, 32);

I have tested this using CoolTerm and it works well.

IOBluetooth Read Data
When the Arduino response is received I add it to a buffer then parse it in 32byte chunks

- (void)parseData:(NSData *)data
    uint8_t terminator;
    [ getBytes:&terminator range:NSMakeRange(kBufferLength - 1, sizeof(uint8_t))];

    if( terminator == op_terminator ) {
        uint8_t tag;
        uint8_t opcode;

        [ getBytes:&tag range:NSMakeRange(0, sizeof(uint8_t))];
        [ getBytes:&opcode range:NSMakeRange(1, sizeof(uint8_t))];
        [self.delegate connection:self];

However the first byte - the “tag” - seems to be a random number no matter how many times I try. This random tag is always divisible by 16 however.

I have tried sending hardcoded values from the ardunio to the mac and these are transmitted perfectly, so I know it’s not the arduino Serial.write method that’s incorrect.

Does anyone have any ideas why this may be happening? I’m literally pulling my hair out because of it.