Create a 128Bit UUID with String

nivek picture nivek · Sep 4, 2012 · Viewed 8k times · Source

I tried to create a 128Bit with the iOS CoreBluetooth framework.

The code that I have written is here:

//16Bit Value-String
static NSString * const ADVERTISING_SERVICE_16=@"FFC0";

//Var for 128Bit String
static NSString * ADVERTISING_SERVICE;


//A Base UUID 
static NSString * const BASE_UUID=@"0405060708090A0B0C0D0E0F";

+ (NSString*) get128BitUUID:(NSString*)uuid{
    return [[NSString alloc] initWithFormat:@"0000%@%@",uuid,BASE_UUID];
}

ADVERTISING_SERVICE = [UUIDFuncs get128BitUUID:ADVERTISING_SERVICE_16];

And now when I try to get a UUID with this:

if([service.UUID isEqual:[CBUUID UUIDWithString:ADVERTISING_SERVICE] ]){
    [peripheral discoverCharacteristics:nil forService:service];
}

I get this error message:

2012-09-04 14:18:06.127 blukiiFirmwareTest[3154:707] *** Terminating app due to uncaught
exception 'Invalid UUID string', reason: 'String 0000FFC00405060708090A0B0C0D0E0F 
does not represent a valid UUID'

I don't understand why; the string is a 16 Byte long UUID, this is 128 Bit so why am i getting this error?

Answer

Joris Kluivers picture Joris Kluivers · Sep 18, 2012

On the command line type uuidgen to create a new random uuid:

0C50D390-DC8E-436B-8AD0-A36D1B304B18

You'll see it is the form of 8-4-4-4-12 as mentioned by the wikipedia page linked by @PenguinCoder too.

Creating a CBUUID with a string in this format will work:

CBUUID *uuid = [CBUUID UUIDWithString:@"0C50D390-DC8E-436B-8AD0-A36D1B304B18"];