我正在使用 Apple BLTE Transfer将 iPhone 模拟为外围设备。 我的目标是模拟使用心率测量配置文件的心率监测器。 (我知道如何生成数据,但需要在外围定义服务)
我已经在另一端有一个代码来从 BLE 心率监测器收集数据。
我需要一些指导来定义心率服务及其特征(在外设端)。 我还看到了特定服务 UUID (180D) 的使用和一些 UUID 的特征(例如 2A37 用于心率测量,2A29 用于制造商名称等)我从哪里得到这些数字?它们在哪里定义?
如果需要任何其他信息,请告知。
最佳答案
心率服务详见 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];
注意:心率服务也是我首先实现的。好的选择。 ;)
关于iphone - 为 iPhone 添加心率测量服务作为外设,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18099081/