RN42 Bluetooth disconnects on iOS within seconds from streaming data

I am trying to read data from a device via Bluetooth 2.1 using RN-42. The device connects to the iPhone or iPad Mini, and the data is not transmitted for long, but the iOS and BT module turns off (does not work) for several seconds (less than 10). The device outputs data of 5-10 kB / s, therefore in the Bluetooth specification. Something Ive also noticed that when I run the NSInputStream function, [NSInputStream read: maxLength:], the number of bytes returned is always 158 or less. The app and the hardware aren't crashing, but Bluetooth is just awkward.

The device still sends data to RN42 even after disconnecting, which reduces the likelihood of a problem from the electronics side. This setting also works great on Android devices. I can transfer data without any outages or failures.

Ive tried things ...

  • followed by an External Accessory example, EADemo, provided by Apple.
  • purely using run-loop instead of polling.
  • put the stream in the background stream, as suggested in this post.
  • Removing all NSLogs to improve performance.
  • compiled in debug and release modes.

One thing that works is slowing down data transfer (i.e. less than 5 kB / s), as it allows the iOS and BT module to stay in touch and transmit data until disconnected.

#define EAD_INPUT_BUFFER_SIZE 1024

/**
 * Stream delegate
 */
- (void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)eventCode
{
    switch (eventCode) {
[... other cases ...]

        case NSStreamEventHasBytesAvailable:
        {            
            uint8_t buf[EAD_INPUT_BUFFER_SIZE];
            unsigned int len = 0;
            len = [(NSInputStream *)aStream read:buf maxLength:EAD_INPUT_BUFFER_SIZE];

            if(len) {
                // Read successful, process data
            } else {
                // Fail
            }     
            break;
        }
        default:
            break;
    }
}

/**
 * Stream delegate with polling (for better or worse)
 */
    [...]
        case NSStreamEventHasBytesAvailable:
        {            
            while ([[_session inputStream] hasBytesAvailable])
            {
                // Read the data
                NSInteger bytesRead = [[_session inputStream] read:_buf maxLength:EAD_INPUT_BUFFER_SIZE];

                if (bytesRead > 0) {
                    // Read successful, process data

                } else if (bytesRead == 0) {
                    // End of buffer reached
                    return;

                } else if (bytesRead == -1) {
                    // Failed to read
                    return;
                }
            }
            break;
    [...]
+4
1

Microchip (, Roving Networks, RN42) , , "", - RN42.

RN42 iOS, , 2,5-3 /... Android - , 35 / ( SPP).

RN42, BT-, , iOS ( iAP).

:

  • WiFi.
  • iAP RN42 ( 35 /).
  • .
  • vanilla RN42 PIC, iAP.

5- ... Bluetooth, Apple.

, 4- UART .

+5

All Articles