Search code examples
objective-cnsdatanslogbluetooth-lowenergy

NSData Splits into chunks


I'm NSLogging a stream of NSData that I receive back from an outside source, but for some reason, it keeps breaking itself up into 40 character chunks, and going to a new line once it hits 40 characters. I'm trying to parse through the stream and pick out values in specific places, so it's a huge hassle that it keeps jumping down a line. Does anyone know how this behavior can be prevented? Here is an example of my NSLog:

2013-07-17 14:44:32.638 Test App
[4041:907] data equals  <3e2c042c 31333037 31373032 34302d30 372c0100>
2013-07-17 14:44:32.698 Test App
[4041:907] data equals  <00000000 2c020000 0000002c 03000000 00002cff>
2013-07-17 14:44:32.758 Test App
[4041:907] data equals  <00000000 00>

EDIT: As for relevant code, I'm using a third party BLE library, so I figured it wouldn't be of much use. This is basically the only line of relevant code:

NSData *data = [BLEdevice readReceivedBytes];
NSLog(@"data equals %@", data);

I gave Rob's suggestion a shot, and this was the result:

2013-07-17 15:21:35.399 Test App[4060:907] data equals  <3e2c012c 31333037 31373033 32312d30 372cff00>
2013-07-17 15:21:35.401 Test App[4060:907] data length equals =20
2013-07-17 15:21:35.458 Test App[4060:907] data equals  <00000000>
2013-07-17 15:21:35.460 Test App[4060:907] data length equals =4

It should be streaming back all in just one line, rather than having a 40 character max. Maybe it is a BLE thing.


Solution

  • If BLE sends only small packets there is probably nothing you can do about it. And you probably should not expect that packets of a certain size are returned.

    You should collect all received chunks in an NSMutableData object instead:

    // Init once:
    NSMutableData *collectedData = [NSMutableData data];
    
    // Append received data in your read loop:
    NSData *data = [BLEdevice readReceivedBytes];
    [collectedData appendData:data];
    

    Now you can search for the specific bytes in collectedData.