Can't write data to external bluetooth device using External Accessory Framework

2

I have followed the sample code EADemo provided by Apple. I have successfully connected to IAP2 Bluetooth MFI device below BLE i.e. version (2.x) with my application and read data from external device.

But when I tried to write some commands to external Bluetooth device, not getting any response from external bluetooth device for that particular command. As external device is below BLE we are using External Accessory framework.

Below is code while writing the string command using UTF8:

  const char *buf = [[_stringToSendText text] UTF8String];
      if (buf)
      {
        uint32_t len = (uint32_t)strlen(buf) + 1;
        [[EADSessionController sharedController] writeData:[NSDatadataWithBytes:buf length:len]];
      }

Below is the code for writing the data by converting the string into hexCode:

    const char *buf = [[_hexToSendText text] UTF8String];
NSMutableData *data = [NSMutableData data];
if (buf)
{
    uint32_t len = (uint32_t)strlen(buf);

    char singleNumberString[3] = {'\0', '\0', '\0'};
    uint32_t singleNumber = 0;
    for(uint32_t i = 0 ; i < len; i+=2)
    {
        singleNumberString[0] = buf[i];
        singleNumberString[1] = buf[i + 1];
        sscanf(singleNumberString, "%x", &singleNumber);
        uint8_t tmp = (uint8_t)(singleNumber & 0x000000FF);
        [data appendBytes:(void *)(&tmp) length:1];
    }
    [[EADSessionController sharedController] writeData:data];
}

Below code is used to write data to external device:

    - (void)_writeData {
        while (([[_session outputStream] hasSpaceAvailable]) && ([_writeData length] > 0))
        {
          NSInteger bytesWritten = [[_session outputStream] write:[_writeData bytes] maxLength:[_writeData length]];
          if (bytesWritten == -1)
          {
            NSLog(@"write error");
            break;
          }
          else if (bytesWritten > 0)
          {
            [_writeData replaceBytesInRange:NSMakeRange(0, bytesWritten) withBytes:NULL length:0];
          }
        }
}

For example when i am writing "$info" to external device device should send response about device information but we are not getting any kind of response from external device.

I am getting bytesWritten value as same as i as written data so i have successfully send the data but issue is i am not getting any response from external device.

I am working in XCODE 9.0 and testing on iOS version 10.3.3 and 11.2.1

Thanks

ios
objective-c
bluetooth
external-accessory
mfi
asked on Stack Overflow Jan 8, 2018 by sumit kumar • edited Mar 21, 2018 by sumit kumar

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0