nrf8001 Bluetooth LE transmission gets stuck after a few pac

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
Squeegy
 
Posts: 16
Joined: Wed May 07, 2014 2:51 am

nrf8001 Bluetooth LE transmission gets stuck after a few pac

Post by Squeegy »

I had this nrf8001 breakout board communicating with an iOS app for a bit and it was flawless. It was fast and reliable. My hopes were high.
Then I decided that I wanted to send a bit more data to the iPhone and everything fell apart.

I know you can only send 20 bytes at a time, that's not the problem. The problem seems to be when you send many packets. Exactly how many packets before it chokes up seems to vary. The board seems to be able to receive a huge amount of data very quickly without any issues, however.

Here's the relevant bits of the Arduino side:

Code: Select all

#define ADAFRUITBLE_REQ 10
#define ADAFRUITBLE_RDY 1     // This should be an interrupt pin, on Uno thats #2 or #3
#define ADAFRUITBLE_RST 9
Adafruit_BLE_UART BTLEserial = Adafruit_BLE_UART(ADAFRUITBLE_REQ, ADAFRUITBLE_RDY, ADAFRUITBLE_RST);

void btleTest() {
  uint8_t buffer[20];
  for (uint8_t i = 0; i < 20; i++) { buffer[i] = i; }
  while(true) {
    BTLEserial.write(buffer, 20);
    delay(1000);
  }
}
And the iOS side data receiver in Swift.

Code: Select all

    func peripheral(peripheral: CBPeripheral!, didUpdateValueForCharacteristic characteristic: CBCharacteristic!, error: NSError!) {
        NSLog("Received data: %@", readCharacteristic!.value)
    }
What I get in my XCode log when it runs and connects:

Code: Select all

Received data: <00010203 04050607 08090a0b 0c0d0e0f 10111213>
Received data: <00010203 04050607 08090a0b 0c0d0e0f 10111213>
Received data: <00010203 04050607 08090a0b 0c0d0e0f 10111213>
Received data: <00010203 04050607 08090a0b 0c0d0e0f 10111213>
Received data: <00010203 04050607 08090a0b 0c0d0e0f 10111213>
And that's it. It sends roughly between 4-12 packets, and then just stops. If I disconnect and then reconnect, I can then send another 4-12 packets before it stops. The Arduino continues to loop fine. The transmissions just start silently failing to arrive.

Any idea what's going on here? It's driving me insane. I simply want to send a few hundred bytes shortly after initial connection, and having an indeterminate amount of data simply not arrive is kind of deal breaker.

User avatar
ktownsend
 
Posts: 1447
Joined: Thu Nov 05, 2009 2:18 am

Re: nrf8001 Bluetooth LE transmission gets stuck after a few

Post by ktownsend »

I really can't offer any help whatsoever on the iOS side, and it's kind of out of the scope of what we can directly support, but I'd suggest picking up a cheap sniffer which will at least let you see what's going on over the air. The data should be arriving, but it's hard to say what's getting lost on the iOS side.

Have you tested this with our own iOS app to see if all of the data is arriving? That at least rules certain things out.

TI has a sniffer for $49 (with free shipping on their store) that lets you use their free sniffer SW and it might help see what's happening between the two devices: https://estore.ti.com/CC2540EMK-USB-CC2 ... Id=8149282

Nordic also has a very good sniffer package based on their nRF51822 that pushes data into Wireshark (and in my opinion is better than the TI sniffer because of that), but you need to purchase the $99 nRF51822-EK to get access to it.

If you're doing some serious BLE development, either of these will be very valuable tools and an excellent investment.

Can you try with the official Adafruit app and see if the bytes are being dropped or not first, though?

Update: Here's a link to the app that includes a UART Monitor: https://itunes.apple.com/app/adafruit-b ... 25974?mt=8 You can also try with the official Nordic app that should work out of the box with our UART service: https://itunes.apple.com/app/nrf-uart/id614594903?mt=8

Squeegy
 
Posts: 16
Joined: Wed May 07, 2014 2:51 am

Re: nrf8001 Bluetooth LE transmission gets stuck after a few

Post by Squeegy »

Thanks for the info!

I did try the Adafruit app, and when I connect in UART mode I see the exact same behavior. In my first 6 attempts to connect I get exactly 4 packets of 20 bytes in the output window and then it completely stops. So if the problem is on the iOS side, it's not only in my own code.

Aaaaaaand as I was testing things while typing this reply I think I figured it out!

This results in a clogged pipe after a few packets:

Code: Select all

  while(true) {
    BTLEserial.write(buffer, 20);
    delay(1000);
  }
Where this keeps sending successfully forever:

Code: Select all

  while(true) {
    BTLEserial.write(buffer, 20);
    BTLEserial.pollACI(); // magic line here
    delay(1000);
  }
Something in pollACI() must clear some sort of internal buffer or something? I looked at the method but I'm not familiar enough with C or with the underlying libraries to figure out what it's doing.

I guess for most cases it works fine if you have pollACI() in your loop() and only send a few bytes each loop. But if you want to send larger amounts of data in a single loop() turns out you have to pollACI() !

This would probably be a good note to add to the docs of the library somewhere. Or perhaps Adafruit_BLE_UART::write() could be enhanced to do it's own pollACI() when necessary?

Either way, I think my problem has been solved. Thank you for the insights and suggestions on how to level up my BTLE-fu.

Squeegy
 
Posts: 16
Joined: Wed May 07, 2014 2:51 am

Re: nrf8001 Bluetooth LE transmission gets stuck after a few

Post by Squeegy »

Another quirk I've noticed is that packets can arrive out of order if you send them too fast. Making a delay in that loop pretty much required as well. The Adafruit lib seems to have a 35ms delay in it, but maybe that's too short. I padded it an extra 50ms and order is now more guaranteed.

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”