蓝牙LE writeValue问题

时间:2017-05-08 08:32:26

标签: ios objective-c bluetooth core-bluetooth

这是我的问题:

我有一个小bluetooth tracker:使用Core Bluetooth,我成功连接到设备并获得了服务和特性。然而,这个设备里面有一个振铃器,我正努力让它响起来......

我有这项服务和特色:

<CBService: 0x1742737c0, isPrimary = YES, UUID = 1802> <CBCharacteristic: 0x1742a1e60, UUID = 2A06, properties = 0x4, value = (null), notifying = NO>

使用属性: writeWithoutResponse

所以,我试图从&#39; null&#39;中修改该特征的值。到1或2,但价值永远不会更新,我不明白为什么。 我使用this作为文档来了解什么服务/特性做什么。在这种情况下,我尝试修改立即警报服务中的警报级别特征。

这是我的代码:

- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(nonnull CBService *)service error:(nullable NSError *)error {
    NSLog(@"Discovered service : %@", service);

    if (peripheral != _tracker) {
        return;
    }

    for (CBCharacteristic *characteristic in service.characteristics) {
        NSLog(@"- %@", characteristic);

        if ([characteristic.UUID.UUIDString isEqualToString:@"2A06"] && [service.UUID.UUIDString isEqualToString:@"1802"]) {

            _alertLevel = characteristic;  

            int ring = 1;
            NSData *data = [NSData dataWithBytes:&ring length:1];
            NSLog(@"data : %@", data);

            [self logCharacteristicProperties:_alertLevel.properties];

            [NSTimer scheduledTimerWithTimeInterval:2.0
                                         target:self
                                       selector:@selector(readValue)
                                       userInfo:nil
                                        repeats:YES];

            [self.tracker writeValue:data forCharacteristic:_alertLevel type:CBCharacteristicWriteWithoutResponse];   
        }
    }
    NSLog(@"");
}

- (void)readValue {
    [_tracker readValueForCharacteristic:_alertLevel];
}

- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error {

    if (characteristic != _alertLevel) {
        return ;
    }

    NSLog(@"updated data -> '%@'", _alertLevel.value);
}

_tracker 变量是对连接的外围设备的引用。

_alertLevel 变量是对警报级别特征的引用。

结果输出:

2017-05-08 09:56:13.914641+0200 AppName[1148:225988] update data -> '(null)'
2017-05-08 09:56:15.924735+0200 AppName[1148:225988] update data -> '(null)'
2017-05-08 09:56:17.934123+0200 AppName[1148:225988] update data -> '(null)'
2017-05-08 09:56:19.914376+0200 AppName[1148:225988] update data -> '(null)'
2017-05-08 09:56:21.984195+0200 AppName[1148:225988] update data -> '(null)'
2017-05-08 09:56:23.933688+0200 AppName[1148:225988] update data -> '(null)'
2017-05-08 09:56:25.913973+0200 AppName[1148:225988] update data -> '(null)'
2017-05-08 09:56:28.103446+0200 AppName[1148:225988] update data -> '(null)'

有谁知道为什么值保持为空? 非常感谢!

0 个答案:

没有答案