问题
I have a task of comparing Manufacturers data to a string to display the BLE devices .
-(void)centralManager:(CBCentralManager *):didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
{
NSString *manufac=[advertisementData objectForKey:CBAdvertisementDataManufacturerDataKey];
NSString *string=@"57394423 4334445 55";
if([string isEqualToString:manufac])
NSLog(@"yes it is the device");
else
NSLog(@"sorry not a device");
}
'manufac' when printed gives result as <57394423 4334445 55> which doesnot prove to be correct at any time. How can it be compared ? please help.
回答1:
The object returned by the CBAdvertisementDataManufacturerDataKey
is an NSData
object.
You are assigning it to an NSString
object which is wrong.
To get the UUID you could do the following:
NSData *manufacturer = [advertisementData objectForKey:CBAdvertisementDataManufacturerDataKey];
NSUUID *uuid = [[NSUUID alloc] initWithUUIDBytes:manufacturer.bytes];
And then compare this UUID to the one you are searching for. If you need an actual NSString
representation, use uuid.UUIDString
.
来源:https://stackoverflow.com/questions/34942484/objective-c-ble-device-manufacturers-data-comparison