Domanda

I'm receiving data successfully in the BLE app I've built for iPhone/iPad however, converting the bytes to a single NSString for a UILabel display has proven challenging at best.

With an input of "123456789" the bytes get broken in to 4 separate messages.

This is the didUpdateValueForCharacteristic method I'm using for testing:

NSString *myStr = [[NSString alloc] initWithData:characteristic.value encoding:NSASCIIStringEncoding];

 [self.dataBuffer appendData:characteristic.value];
 [self.textView setText:[[NSString alloc] initWithData:self.dataBuffer encoding:NSASCIIStringEncoding]];

 //NSUInteger bytes = [self.dataBuffer length];
 NSLog(@"Data: %@", myStr);

Xcode Log:

2014-03-10 22:53:27.771 BTLE-Sensor[2860:60b] Data: 1
2014-03-10 22:53:27.775 BTLE-Sensor[2860:60b] Data: 2345
2014-03-10 22:53:27.777 BTLE-Sensor[2860:60b] Data: 678
2014-03-10 22:53:27.779 BTLE-Sensor[2860:60b] Data: 9

And the label of course displays only the last received byte: 9

Obviously I'd like to have the UILabel display: 123456789

I've tried appendData functions and more "for" loop iterations than I'd like to remember!

Mike

È stato utile?

Soluzione

You're not really appending the string, you're creating and setting it every time as your textView's text. Do this:

 [self.textView setText:[self.textView.text stringByAppendingString:[[NSString alloc] initWithData:self.dataBuffer encoding:NSASCIIStringEncoding]]];
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top