U_D
U_D

Reputation: 741

Add Heart rate measurement service to iPhone as peripheral

I'm using Apple BLTE Transfer to emulate the iPhone as a peripheral. My goal is to simulate a heart rate monitor that uses the heart rate measurement profile. (I know how to generate the data but needs to define the service on the peripheral side)

I've already have a code on the other side to collect data from BLE heart rate monitors.

I need some guidance how to define the Heart rate service and it's characteristics (ON the peripheral side). I've also seen the use of specific service UUID (180D) and some characteristics UUID's (such as 2A37 for Heart rate measurement, 2A29 for manufacturer name etc.) Where do I get those numbers? and where they are defined?

If any other information need please advise.

Upvotes: 4

Views: 3465

Answers (2)

allprog
allprog

Reputation: 16780

The heart rate service is detailed on the bluetooth developer portal. Say you have a CBPeripheralManager named peripheralManager initialized and you already received the peripheralManagerDidUpdateState: callback with the CBPeripheralManagerStatePoweredOn state. Here is how you can set up the service itself after this.

// Define the heart rate service
CBMutableService *heartRateService = [[CBMutableService alloc] 
      initWithType:[CBUUID UUIDWithString:@"180D"] primary:true];

// Define the sensor location characteristic    
char sensorLocation = 5;
CBMutableCharacteristic *heartRateSensorLocationCharacteristic = [[CBMutableCharacteristic alloc]
      initWithType:[CBUUID UUIDWithString:@"0x2A38"] 
        properties:CBCharacteristicPropertyRead 
             value:[NSData dataWithBytesNoCopy:&sensorLocation length:1] 
       permissions:CBAttributePermissionsReadable];

// Define the heart rate reading characteristic    
char heartRateData[2]; heartRateData[0] = 0; heartRateData[1] = 60;
CBMutableCharacteristic *heartRateSensorHeartRateCharacteristic = [[CBMutableCharacteristic alloc] 
      initWithType:[CBUUID UUIDWithString:@"2A37"]
        properties: CBCharacteristicPropertyNotify
             value:[NSData dataWithBytesNoCopy:&heartRateData length:2]
       permissions:CBAttributePermissionsReadable];

// Add the characteristics to the service 
heartRateService.characteristics = 
      @[heartRateSensorLocationCharacteristic, heartRateSensorHeartRateCharacteristic];

// Add the service to the peripheral manager    
[peripheralManager addService:heartRateService];

After this you should receive the peripheralManager:didAddService:error: callback indicating the successful addition. You should add the device information service (0x180A) similarly Finally, you should start advertising with:

NSDictionary *data = @{
    CBAdvertisementDataLocalNameKey:@"iDeviceName", 
    CBAdvertisementDataServiceUUIDsKey:@[[CBUUID UUIDWithString:@"180D"]]};

[peripheralManager startAdvertising:data];

Note: The heart rate service was the first I implemented too. Good choice. ;)

Upvotes: 3

Tommy Devoy
Tommy Devoy

Reputation: 13549

Everything regarding Gatt Specifications can be found on the Bluetooth Developer Site. What you need to do is basically this:

1.)Set up your CBPeripheralManager

2.)After it is powered on, create the CBMutableService and CBMutableCharacteristics that match the Heart rate service. Advertise them and you'll be good to go.

Upvotes: 2

Related Questions