まず、BLEのProfileの1つであるGATT Profile(Generic Attribute Profile)に登場するオブジェクトについて説明します。
文字通りサービスです。例えば温度計サービスや体重計サービスなどが考えられます。ServiceにはUUIDが付いています。
CoreBluetoothでは、次のようにしてServiceオブジェクトを生成します。
NSString* const MY_UUID = @"7B5FC8F1-66E3-442F-A4EE-BA18D0DE01D8";
// Create UUID
CBUUID *uuid = [CBUUID UUIDWithString:MY_UUID];
// Create Service
CBMutableService *service = [[CBMutableService alloc] initWithType:uuid primary:YES];
実際にデータを提供する人です。Serviceの子供になり、こいつもやはりUUIDで識別されます。例えば温度計サービスの場合、Characteristicとして
のようなものが考えられます。
CoreBluetoothでは、次のようにしてCharacteristicオブジェクトを生成し、Serviceに紐付けます。
NSString* const MY_UUID = @"AB0B5B1C-14B2-4D5B-8AB3-80558B6B2243";
// Create UUID
CBUUID *uuid = [CBUUID UUIDWithString:MY_UUID];
// create characteristic
CBMutableCharacteristic *myCharacteristic =
[[CBMutableCharacteristic alloc] initWithType:uuid
properties:CBCharacteristicPropertyRead
value:nil
permissions:CBAttributePermissionsReadable];
// set characteristics
service.characteristics = @[myCharacteristic];