1

I'm creating an iOS application that receives an input from a force sensor. The Arduino is connected to a BLE Bluetooth transmitter, that sends a data stream of information to the application.

Below is my code:

-(void) bleDidReceiveData:(unsigned char *)data length:(int)length
{
    NSData *d = [NSData dataWithBytes:data length:length];
    NSString *s = [[NSString alloc] initWithData:d encoding:NSUTF8StringEncoding];
    self.label.text = s;

    if (s>150){
        [UIImageView beginAnimations:NULL context:nil];
        [UIImageView setAnimationDuration:0.01];
        [ImageView setAlpha:1];
        [UIImageView commitAnimations];
    }
    else {
        //remove red
        [UIImageView beginAnimations:NULL context:nil];
        [UIImageView setAnimationDuration:1.0];
        [ImageView setAlpha:0];
        [UIImageView commitAnimations];
    }
}

I had two questions:

  • The Bluetooth subsystem sends a stream of data (d) which is a set of numbers representing force impacts on the force sensor. Is there any way for me to go into each element of d or the converted string, s, so that I can use it in my if statement? I need to display the image if any number in the string is above a threshold (the if statement). Or can I directly use the string in the if statement? I would imagine this would require a for loop. I am just not sure how to set it up.

  • To get the threshold, I need to calibrate the force sensor. Is there a way that I can get the mode or average of the data stream or string?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129

1 Answers1

0

If there is a gap or some sort of separator (such as a '-' or '/') between the data stream:

  • use [NSString componentsSeparatedByString] to split the string into it's constituent parts
  • then use [NSString floatValue] on each separated string to get the number
  • compare this value to your threshold in your if statement, show your image if necessary.

If there is no separator in your data stream then there are two options:

1) convert to NSString as you do already, then use characteratIndex[n] inside a for loop to get each character. To convert each returned unichar to a number, you'll need to do some jiggerypokery. See these two threads:

Objective-C NSString for loop with characterAtIndex

Converting Unichar to Int Problem

2) if you know how many bytes are taken up by each value you may be able to skip the NSString completey and use [NSData subDataWithRange:] (inside a for loop) to extract the data and convert to a value.

Good luck!

Community
  • 1
  • 1
AlunAlun
  • 206
  • 1
  • 4
  • The first method works, I have '-' seperating each data point. Thank You! I had a second questions. The data from the arduino is sent as a number (anywhere between 0-500). It displays as characters and random numbers on the application. I know this has to do with the encoding; is there any different encoding I can use in my NSString...encoding: ______ Just so that numbers are displayed. Or can I convert the encoded string into one that displays numbers. – Hiten Patel Apr 08 '13 at 19:20