扫描不适用于iOS8和连接设备的心率外围设备

时间:2014-09-25 20:32:38

标签: ios bluetooth

我使用以下代码连接到蓝牙心率传感器。在iOS8和设置下连接的蓝牙设备之前,这一功能正常。当我按“忘记此设备”时,我的代码再次运行。如何修改代码以连接“连接心率传感器?”

//请求CBCentralManager使用服务UUID 0x180D扫描心率外设 - (void)startScan {

[self.manager scanForPeripheralsWithServices:[NSArray arrayWithObject:[CBUUID UUIDWithString:@"180D"]]
                                     options:nil];


for (CBPeripheral *peripheral in self.heartRateMonitors) {
    if (peripheral.state == CBPeripheralStateConnected) {
        NSUInteger serviceIdx = [peripheral.services indexOfObjectPassingTest:^BOOL(CBService *obj, NSUInteger idx, BOOL *stop) {
            return [obj.UUID isEqual:@"180D"];
        }];

        if (serviceIdx == NSNotFound) {
            [peripheral discoverServices:@[@"180D"]];
            continue;
        }

        // have we discoverd the Heart Rate Measurement Characteristic?
        CBService *service = peripheral.services[serviceIdx];
        NSUInteger charIdx = [service.characteristics indexOfObjectPassingTest:^BOOL(CBCharacteristic *obj, NSUInteger idx, BOOL *stop) {
            return [obj.UUID isEqual:@"180D"];
        }];

        if (charIdx == NSNotFound) {
            [peripheral discoverCharacteristics:@[@"180D"] forService:service];
            continue;
        }

        // Are we subscribed?
        CBCharacteristic *characteristic = service.characteristics[charIdx];
        if (!characteristic.isNotifying) {
            [peripheral setNotifyValue:YES forCharacteristic:characteristic];
        }
    }
}

}

0 个答案:

没有答案