如何在IOS 7中将警报级别写入警报级别的特征?

时间:2014-04-18 06:19:19

标签: ios bluetooth bluetooth-lowenergy core-bluetooth

我是iOS开发的新手,并为IOS研究蓝牙低功耗(BLE,蓝牙4.0)。

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

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

我已将 Immediate alert Service Alert 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]]];

连接到 characteristics of alert level 后,有关连接到 IMMEDIATE_ALERT_UUID 的代码如下所示。

- (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 ??

提前致谢。

1 个答案:

答案 0 :(得分:2)

-(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{ }