【问题标题】:Add Heart rate measurement service to iPhone as peripheral将心率测量服务作为外围设备添加到 iPhone
【发布时间】:2013-08-08 13:48:41
【问题描述】:
我正在使用Apple BLTE Transfer 将 iPhone 模拟为外围设备。
我的目标是模拟使用心率测量配置文件的心率监测器。
(我知道如何生成数据但需要在外围端定义服务)
我已经在另一端有一个代码,用于从 BLE 心率监测器收集数据。
我需要一些指导如何定义心率服务及其特征(在外围设备端)。
我还看到了特定服务 UUID (180D) 和一些特征 UUID 的使用(例如用于心率测量的 2A37、用于制造商名称的 2A29 等)我从哪里获得这些数字?以及它们在哪里定义?
如果需要任何其他信息,请告知。
【问题讨论】:
标签:
iphone
ios
xcode
core-bluetooth
【解决方案1】:
有关 Gatt 规范的所有内容都可以在 Bluetooth Developer Site 上找到。你需要做的基本上是这样的:
1.)设置您的CBPeripheralManager
2.)开机后,创建匹配心率服务的CBMutableService和CBMutableCharacteristics。给他们做广告,你会很高兴的。
【解决方案2】:
心率服务详见bluetooth developer portal。
假设您初始化了一个名为 peripheralManager 的 CBPeripheralManager,并且您已经收到了带有 CBPeripheralManagerStatePoweredOn 状态的 peripheralManagerDidUpdateState: 回调。在此之后,您可以通过以下方式自行设置服务。
// 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];
在此之后,您应该会收到 peripheralManager:didAddService:error: 回调,表明添加成功。你应该添加device information service (0x180A) 最后,你应该开始做广告:
NSDictionary *data = @{
CBAdvertisementDataLocalNameKey:@"iDeviceName",
CBAdvertisementDataServiceUUIDsKey:@[[CBUUID UUIDWithString:@"180D"]]};
[peripheralManager startAdvertising:data];
注意:心率服务也是我实施的第一个服务。好的选择。 ;)