【问题标题】:How to write the alert level to thecharacteristics of alert level in IOS 7?如何在IOS 7中将警报级别写入警报级别的特征?
【发布时间】:2014-04-18 06:19:18
【问题描述】:

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

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

我可以通过BLE devicescan , connect and discover the Service。 接下来是连接到Immediate alert Service并将characteristics of alert level写入BLE device

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

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

以下代码是关于连接到Immediate alert Service

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

连接IMMEDIATE_ALERT_UUID后连接characteristics of alert level的代码如下。

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

订阅characteristics of alert level

- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error
{
      if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:ALERT_LEVEL_UUID]]) {

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

但是如何将警报级别写入characteristics of alert level ??

提前致谢。

【问题讨论】:

  • writeValue:forCharacteristic:type:?

标签: ios bluetooth bluetooth-lowenergy core-bluetooth


【解决方案1】:
-(void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)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 dataWithBytes:(void*)&val length:sizeof(val)];

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-16
    相关资源
    最近更新 更多