iphone - 为 iPhone 添加心率测量服务作为外设

标签 iphone ios xcode core-bluetooth

我正在使用 Apple BLTE Transfer将 iPhone 模拟为外围设备。 我的目标是模拟使用心率测量配置文件的心率监测器。 (我知道如何生成数据,但需要在外围定义服务)

我已经在另一端有一个代码来从 BLE 心率监测器收集数据。

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

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

最佳答案

心率服务详见 bluetooth developer portal . 假设您有一个名为 peripheralManagerCBPeripheralManager 已初始化,并且您已经收到带有 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/

相关文章:

Xcode 有时会删除链接库

ios - 在 Swift 中分配一个值,但它认为它是 nil

ios - 在应用程序委托(delegate)中下载 firebase 数据

ios - Firebase iOS swift : how to pick a random userProfile?

ios - 找不到 Apple Match-O 链接器错误库 Xcode 6.4

iphone - 在 iOS 6.0 中获取 facebook 访问 token

iphone - 使用一个代码库 : framework for scaling layouts and assets for HTML5 apps on iPhones or iOS devices? 为非 Retina 和 Retina 显示器提供服务

ios - 将值从 TableView Controller 传递到 View Controller

iphone - 这是 UIImage 的有效使用吗?

xcode - Youtube 视频无法在 iPad 上全屏播放