将心率测量服务添加到iPhone作为外围设备

前端之家收集整理的这篇文章主要介绍了将心率测量服务添加到iPhone作为外围设备前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用 Apple BLTE Transfer模拟iPhone作为外围设备.
我的目标是模拟使用心率测量配置文件的心率监测器.
(我知道如何生成数据但需要在外围端定义服务)

我已经在另一侧有一个代码来收集BLE心率监测器的数据.

我需要一些指导如何定义心率服务及其特征(在外围侧).
我还看到了使用特定服务UUID(180D)和UUID的一些特性(例如2A37用于心率测量,2A29用于制造商名称等)我从哪里获得这些数字?它们的定义在哪里?

如果需要任何其他信息请告知.

解决方法

心率服务详情请见 bluetooth developer portal.
假设您已经初始化了名为peripheralManager的CBPeripheralManager,并且您已经收到了具有CBPeripheralManagerStatePoweredOn状态的peripheralManagerDidUpdateState:callback.以下是在此之后如何设置服务的方法.

// 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];

注意:心率服务也是我实施的第一个.好的选择.

猜你在找的Xcode相关文章