Question

I am developing an open source block based wrapper for Core Bluetooth.

Currently I am developing the Peripheral Manager part.

I have it broadcasting and I can see the service and its characteristic. When looking at other services I can see they have been given meaningful names such as Battery and Current time.

When I try and provide a meaningful name for any service or characteristic I create it returns the error String Characteristic name does not represent a valid UUID.

My code so far is

   [_peripheralManager startAdvertising:@{CBAdvertisementDataLocalNameKey : @"Peripheral name",
                                       CBAdvertisementDataServiceUUIDsKey : [CBUUID UUIDWithNSUUID:[NSUUID UUID]]}];

CBMutableCharacteristic *transferCharacteristic = [[CBMutableCharacteristic alloc] initWithType:[CBUUID UUIDWithString:@"Characteristic name"]
                                                                                     properties:CBCharacteristicPropertyRead
                                                                                          value:[@"Test value" dataUsingEncoding:NSUTF8StringEncoding]
                                                                                    permissions:CBAttributePermissionsReadable];

CBMutableService *transferService = [[CBMutableService alloc] initWithType:[CBUUID UUIDWithString:@"Service name"]
                                                                   primary:YES];

[transferService setCharacteristics:@[transferCharacteristic]];

[_peripheralManager addService:transferService];

If I replace any initWithType arguments with [CBUUID UUIDWithNSUUID:[NSUUID UUID]] it works but is not nice to show to the user.

Is there a way to set the UUID of the CBService or CBCharacteristic to a meaningful string?

It is also worth noting that the value of the characteristic is null when viewed, bonus points for seeing why this is.

Thanks

Was it helpful?

Solution

The Bluetooth organisation has accepted some services and their characteristics as standards. These can be referred to by a 4 hex digit assigned number when using the Core Bluetooth library - See the CBUUID documentation

User defined UUIDs are not recognised by the Core Bluetooth library as they are not assigned numbers, so you can only display them with their full UUID.

The value of the characteristic will be null if you haven't issued a read or received a notify.

OTHER TIPS

Use the terminal command uuidgen to create a user defined UUID.


As Paul mentioned, the value will be null until you read the value of the characteristic. It seems like a lot of work but after you discover a peripheral you connect to it (don't forget to retain the peripheral and set the peripheral's delegate property), then discover its services, then discover the characteristics for the services, then you may read the value of the characteristics like so:

[peripheral readValueForCharacteristic:characteristic]; //where peripheral is the connected CBPeripheral 

In the CBCentralManagerDelegate method peripheral:didUpdateValueForCharacteristic:error: the value can be read:

NSLog(@"%@",[[NSString alloc]initWithData:characteristic.value encoding:NSUTF8StringEncoding]);

Based on the code you included, it is not necessary to include the advertisement data service UUID key in your options dictionary . If you would like to specify a data service UUID that is specific to your app, start advertisementing like this:

[_peripheralManager startAdvertising:@{CBAdvertisementDataLocalNameKey : @"Peripheral name",
                                   CBAdvertisementDataServiceUUIDsKey : @[[CBUUID UUIDWithString:@"generated-string-using-uuidgen"]]}];

You can then set your CBCentralManager to scan for that specific data service uuid like this:

[_centralManager scanForPeripheralsWithServices:@[[CBUUID UUIDWithString:@"generated-string-using-uuidgen"]] options:nil];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top