OGeek|极客世界-中国程序员成长平台

标题: ios - 如何在IOS 7中将警报级别写入警报级别的特征? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 09:29
标题: ios - 如何在IOS 7中将警报级别写入警报级别的特征?

我是 iOS 开发新手,正在研究适用于 IOS 的低功耗蓝牙(BLE,蓝牙 4.0)。

我想知道如何在 IOS 7 上使用 Immediate Alert Service

我可以从 BLE 设备 扫描、连接和发现服务。 接下来是连接到Immediate alert Service并将警报级别的特征写入BLE设备.

我已经定义了 Immediate alert ServiceAlert level 的 UUID,如下代码所示。

#define IMMEDIATE_ALERT_UUID           @"00001802-0000-1000-8000-00805f9b34fb"
#define ALERT_LEVEL_UUID    @"00002a06-0000-1000-8000-00805f9b34fb"

以下代码是关于连接到即时警报服务

[peripheral discoverServices[[CBUUID UUIDWithString:IMMEDIATE_ALERT_UUID]]];

连接IMMEDIATE_ALERT_UUID后的警报级别特征的代码如下关注。

- (void)peripheralCBPeripheral *)peripheral didDiscoverServicesNSError *)error
{
    for (CBService *service in peripheral.services) {
        [peripheral discoverCharacteristics[[CBUUID UUIDWithString:ALERT_LEVEL_UUID]] forService:service];
    }
}

订阅警报级别特征

- (void)peripheralCBPeripheral *)peripheral didDiscoverCharacteristicsForServiceCBService *)service errorNSError *)error
{
      if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:ALERT_LEVEL_UUID]]) {

            // If it is, subscribe to it
            [peripheral setNotifyValue:YES forCharacteristic:characteristic];
        }
}

但是如何将警报级别写入警报级别的特征??

提前致谢。



Best Answer-推荐答案


-(void)peripheralCBPeripheral *)peripheral didDiscoverCharacteristicsForServiceCBService *)service errorNSError *)error{
    if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:ALERT_LEVEL_UUID]]){

    uint8_t val = 0 //enter the value which you want to write.
    NSData* valData = [NSData dataWithBytesvoid*)&val length:sizeof(val)];

    [peripheral writeValue:valData forCharacteristic:characteristictype:CBCharacteristicWriteWithResponse];
}

放参数CBCharacteristicWriteWithResponse会调用CBPeripheralDelegate方法 - (void)peripheralCBPeripheral *)peripheral didWriteValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error{ }

关于ios - 如何在IOS 7中将警报级别写入警报级别的特征?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23148548/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4