1

i am trying to get a substring from a parent string. parent string is mac address of device available in a dictionary. But when i try to convert it it si giving me error as below "[_NSInlineData substringFromIndex:]: unrecognized selector sent to instance".

I just want to remove some of the chars from the parent string (here it is mac address like "<0001ui3 234563>" ) and get "ui3 234563".

   // This is my nsdictionary.
  advertisementdata = {     kCBAdvDataIsConnectable = 1;     kCBAdvDataLocalName = "test123";     kCBAdvDataManufacturerData = <00029r21 6y051rt2>;     kCBAdvDataServiceUUIDs =     (         FFF0     );     kCBAdvDataTxPowerLevel = 0; }

 // I want 9r216y051rt from kCBAdvDataManufacturerData;
 NSString *str2;


 str2=[advertisementData objectForKey:@"kCBAdvDataManufacturerData"];

 NSString *str3;
 str3=[str2 substringFromIndex:5];

 // here I do not get the required 12 character string from str2 string.

 get NSInlinedata error for all NSString method.

Thanks,

Purushottam zende
  • 552
  • 1
  • 6
  • 20

3 Answers3

2

Note that iOS CoreBlueTooth advertisementData is binary NSData:

https://developer.apple.com/library/ios/documentation/CoreBluetooth/Reference/CBCentralManagerDelegate_Protocol/index.html#//apple_ref/doc/constant_group/Advertisement_Data_Retrieval_Keys

Documentation clearly states:

CBAdvertisementDataManufacturerDataKey
A NSData object containing the manufacturer data of a peripheral.

BTW, when you have a NSDictionary value type NSData*, you get the error by casting this NSData into a NSString*

// this is a bug!

NSString* str2=[advertisementData objectForKey:@"kCBAdvDataManufacturerData"];

To inspect the dictionary value try

NSData* advData = [advertisementData objectForKey:@"kCBAdvDataManufacturerData"];

Unfortunately, since this is binary, you need to interpret it according to the peripheral manufacturer data documentation. There is no easy way to transform this into a NSString*

Bamaco
  • 592
  • 9
  • 25
1

Sounds like you have an NSData object, and you're trying to use an NSString method to access it. You can convert your NSData object using the callinitWithData: encoding: Then apply the substringFromIndex call to your new NSString.

Owen Hartnett
  • 5,925
  • 2
  • 19
  • 35
  • Yes, i tried that (please check my new code) , but in my variable "macaddress" is giving empty. – Purushottam zende Oct 29 '14 at 17:06
  • In your code above, you have kCBAdvDataManufacturerData = <00029r21 6y051rt2>, it should be kCBAdvDataManufacturerData = @"<00029r21 6y051rt2>" – Owen Hartnett Oct 29 '14 at 18:50
  • I am not creating it. I printed what i saw in console. Its actually coming from search functionality of Hardware Bluetooth devices. I have lots of them. – Purushottam zende Oct 30 '14 at 04:37
  • OK, let's consider that it's NSData. In that case, change NSString *str2; to NSData * str2; Then change NSString * str3; to NSString * str3 = [[NSString alloc] initWithData: str2 encoding: NSUTF8StringEncoding]; Then do NSString * str4 = [str3 substringFromIndex:5]; Does that make it work? – Owen Hartnett Oct 30 '14 at 17:56
0

The point that you have NSInlineData and what you need is hexadecimal representation of data object.

First step should be to get a hexadecimal representation of data object's content in property list format.

 NSData *datakCBAdvDataManufacturerData = [advertisementData valueForKey:@"kCBAdvDataManufacturerData"];
 NSString *strmanufacturerData      = datakCBAdvDataManufacturerData.description;
 NSString *ChkStr = [strmanufacturerData substringWithRange:NSMakeRange(10, 8)];//depends upon your requirement.
 NSLog(@"%@",ChkStr);

 unsigned int outVal;
 NSScanner* scanner = [NSScanner scannerWithString:ChkStr];
 [scanner scanHexInt:&outVal];
 NSLog(@"%@",outVal);

This outVal variable will contain value what you need.

Aditya Aggarwal
  • 517
  • 6
  • 18