Corebluetooth NSMutableArray with characteristic values - ios

Using the setNotify=YES property I want to create an array populated with the converted 'characteristic.value' values from the below code (Xcode7.2). So far I've only got a null array, all the same values, or only one value at a time
I've implemented
#property (strong, nonatomic) NSMutableArray *voltageArray; //didn't work so
I deleted and tried the following...
The method that I tried to use it in was didUpdateCharacteristic,
if (characteristic.isNotifying)
{
NSString *stringFromData = [[NSString alloc] initWithData:characteristic.value
encoding:NSUTF8StringEncoding];
NSNumber *number = [NSNumber numberWithUnsignedShort:stringFromData];
number = #([number floatValue] *5/2034);
NSLog(#"Characteristic Value: %#", _voltageArray);
}
I also tried the following in the didUpdateValueMethod, along with countless other things,
_voltageArray=[[NSMutableArray alloc] init];
[self.voltageArray addObject:number];
NSLog(#"Array: %#", _voltageArray);
The best I've gotten is all the same value or all null. How do I get these unsigned voltage values into an array?
Updated Solution
#property (strong, nonatomic) NSMutableArray *voltageArray;
-(void)peripheral: didUpdateValueForCharacteristic: error:{
if (characteristic.isNotifying)
{
if(!self.voltageArray){//Checks if voltageArray has been initialised
self.voltageArray = [[NSMutableArray alloc] init];//If not, initialise the array
}
// generate characteristic value
NSString *stringFromData = [[NSString alloc] initWithData:characteristic.value encoding:NSUTF8StringEncoding];
unsigned short baseValue = (unsigned short)stringFromData;
NSNumber *number = [NSNumber numberWithUnsignedShort:baseValue];
number = #([number floatValue] *5/2034);
NSLog(#"Characteristic Value: %#", number);
// give potential recording
self.voltageLabel.text = [NSString stringWithFormat:#"%#",number];
// datasource points y-axis
[_voltageArray addObject:number];
NSLog(#"Array: %#", _voltageArray);

There are a couple of errors that may be causing your issue.
First:
NSString *stringFromData = [[NSString alloc] initWithData:characteristic.value
encoding:NSUTF8StringEncoding];
NSNumber *number = [NSNumber numberWithUnsignedShort:stringFromData];
number = #([number floatValue] *5/2034);
The first method requires a unsigned short type as an input, but you are passing a NSString. You could do this instead:
NSString *stringFromData = [[NSString alloc] initWithData:characteristic.value
encoding:NSUTF8StringEncoding];
unsigned short baseValue = (unsigned short)stringFromData.intValue;
float convertedValue = (float)baseValue *5.0/2034.0; //5.0/2034.0 -> so it typecasts the result value as a float, not int
NSNumber *number = [NSNumber numberWithFloat: convertedValue];
[self.voltageArray addObject:number];
Second, it seems that you are initialising your array every time the didUpdateValueForCharacteristic gets called. Try this instead:
-(void)peripheral: didUpdateValueForCharacteristic: error:{
if (characteristic.isNotifying)
{
if(!self.voltageArray){//Checks if voltageArray has been initialised
self.voltageArray = [[NSMutableArray alloc] init];//If not, initialise the array
}
NSString *stringFromData = [[NSString alloc] initWithData:characteristic.value
encoding:NSUTF8StringEncoding];
unsigned short baseValue = (unsigned short)stringFromData.intValue;
float convertedValue = (float)baseValue *5.0/2034.0;
NSNumber *number = [NSNumber numberWithFloat: convertedValue];
[self.voltageArray addObject:number];
}
}
Hope this helps.

Related

Assigning charAtIndex to stringWithCharacters gives invalid cast warning and bad access error

I'm trying to grab firstname and lastname from firstname+lastname.
int loop=0;
NSMutableString *firstname = [[NSMutableString alloc]init];
NSMutableString *fullName = [[NSMutableString alloc]initWithString:#"Anahita+Havewala"];
for (loop = 0; ([fullName characterAtIndex:loop]!='+'); loop++) {
[firstname appendString:[NSString stringWithCharacters:(const unichar *)[fullName characterAtIndex:loop] length:1]];
}
NSLog(#"%#",firstname);
I tried typecasting from unichar to const unichar* because characterAtIndex returns a unichar but stringWithCharacters accepts a const unichar.
This causes a cast from smaller integer type warning and the app crashes (bad access) when this line is encountered.
Why are string operations so complicated in Objective C?
You can easily get first name and last using componentsSeparatedByString: method.
NSMutableString *fullName = [[NSMutableString alloc]initWithString:#"Anahita+Havewala"];
NSArray *components = [fullName componentsSeparatedByString:#"+"];
NSString *firstName = components[0];
NSString *lastName = components[1];
Note: You need to do proper array bounds check. Also you can use NSScanner for the same purpose.
Try this out:
NSMutableString *firstname = [[NSMutableString alloc] init];
NSMutableString *fullName = [[NSMutableString alloc] initWithString:#"Anahita+Havewala"];
for (NSUInteger loop = 0; ([fullName characterAtIndex:loop]!='+'); loop++) {
unichar myChar = [fullName characterAtIndex:loop];
[firstname appendString:[NSString stringWithFormat:#"%C", myChar]];
}
NSLog(#"%#", firstname);

Access NSArray from dictionary

I have a chat application. When I send push notification from the server, it sends me a NSArray value which I use it to forward user profile with NSNumber.
I printed push notification response with alertview and I added response picture. You can see the response of push notification.
I talked about "customDictionary" key. I want to access this '6297' value and after I want to convert to NSNumber.
Here is my code:
#property (nonatomic, copy) NSNumber *userID;
NSArray *array = [[[userInfo valueForKey:#"aps"] valueForKey:#"customDictionary"]array];
NSString *myArrayString = [array description];
NSNumberFormatter *format = [[NSNumberFormatter alloc] init];
format.numberStyle = NSNumberFormatterDecimalStyle;
self.userID = [format numberFromString:myArrayString];
userID is returning null. Is anybody can help me ? Thank you.
I think that the value of key 'customDictionary' is string, try this
NSString *unformattedString = [[userInfo valueForKey:#"aps"] valueForKey:#"customDictionary"];
NSCharacterSet *specialCharSet = [NSCharacterSet characterSetWithCharactersInString:#"[]"];
NSString *newString = [unformattedString stringByTrimmingCharactersInSet:specialCharSet];
NSLog(#"newString : %#",newString);
See your key "aps" give you a dictionary which actually doesn't contain the "customDictionary" key, its outside of your "aps" dictionary. Try the code given below. Good luck!!
NSArray *array = [[userInfo valueForKey:#"customDictionary"] array];
NSString *myArrayString = [array description];
NSNumberFormatter *format = [[NSNumberFormatter alloc] init];
format.numberStyle = NSNumberFormatterDecimalStyle;
self.userID = [format numberFromString:myArrayString];

add convert UTF8 string to array

i converting Hebrew string and trying yo add it to array , this is the code:
NSMutableArray * myarray;
// #property(strong,nonatomic) NSMutableArray *stringArray;
for (NSString * item in stringArray) {
NSData * UTF8 = [item dataUsingEncoding:NSUTF8StringEncoding];
NSString * myString = [[NSString alloc] initWithData:UTF8 encoding:NSUTF8StringEncoding];
NSLog(#"myString = %#",myString); // Hebrew str log fine
[myarray addObject:myString];
}
NSString * ConvetData = [NSString stringWithFormat:#"%#",myarray];
NSLog(#"ConvetData = %#",ConvetData); // null
newitem.ingredient = ConvetData;
the array coms out as null , how can i crate one string from my convert data?
You need to initialize myarray: NSMutableArray *myarray =[[NSMutableArray alloc] init];

conversion from NSMutableArray of NSData to NSMutableArray of NSString?

my requirement is to convert the NSMutualArray of NSData into NSMutualArray of NSString, Is there any method or function which directly works for above condition, or we have to deal with individual element of each array?
My code:
for (int i = 0; i < [newTutorials count]; i++)
{
// mutableArray[i] = [[NSString alloc] initWithData:newTutorials[i] encoding:NSUTF8StringEncoding];
NSLog(#"url: %#: ",newTutorials[i]);
}
NSLog(#"%#",mutableArray);
I am not aware of anyway besides visiting each element, like:
NSMutableArray *mutableArray; // your array of objects
for (int i = 0; i < [mutableArray count]; i++)
mutableArray[i] = [[NSString alloc] initWithData:mutableArray[i] encoding:NSUTF8StringEncoding];
Answer by Firo is correct. Only change required is the syntax used to set/replaced object in NSMutableArray.
Correct syntax is :
[yourMutableArray replaceObjectAtIndex:i withObject:
[[NSString alloc] initWithData:[yourMutableArray objectAtIndex:i] encoding:NSASCIIStringEncoding];

Converting an NSArray component into an integer or decimal number

I have a case where the data read from the CSV file in the app has to be converted into an integer, has to be plotted later. Currently it doesn't recognize when the data is saved as
int i=[[rows objectAtIndex:0] componentsSeparatedByString:#","];
This is the implemented code.
-(void)connection :(NSURLConnection *) connection didReceiveData:(NSData *)data{
[self serverConnect];
response = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
//NSLog(response);
NSString *stripped1 = [response stringByReplacingOccurrencesOfString:#"\r" withString:#""];
NSArray *rows = [stripped1 componentsSeparatedByString:#"\n"];
NSArray *components;
for (int i=0;i<[rows count]; i++) {
if(i == 0 || [[rows objectAtIndex:i] isEqualToString:#""]){
continue;
}
components = [[rows objectAtIndex:i] componentsSeparatedByString:#","];
NSLog(#"data1:%# data2:%# data3:%#", [components objectAtIndex:0] ,[components objectAtIndex:1],[components objectAtIndex:2]);
}
data1, data2 and data3 are supposed to be integers.
Thanks a lot.
componentsSeparatedByString returns substrings, or instances of NSString.
components = [[rows objectAtIndex:i] componentsSeparatedByString:#","];
You just need to take each member of 'components' and get it's intValue, like so:
int myInt = [[components objectAtIndex:n] intValue];
NSArray and NSMutableArray can only contains objects. So get the integer value from it, use [object intValue]. If you need to add an integer to an array, create a NSNumber object from the integer and insert it. I know Rayfleck answered your question and i just want to point out the way how array works in iOS. Hope this helps.

Resources