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

标签 ios bluetooth bluetooth-lowenergy core-bluetooth

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

我想知道如何在 IOS 7 上使用即时警报服务

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

我定义了即时警报服务的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)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error
{
    for (CBService *service in peripheral.services) {
        [peripheral discoverCharacteristics:@[[CBUUID UUIDWithString:ALERT_LEVEL_UUID]] forService:service];
    }
}

订阅警报级别的特征

- (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];
        }
}

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

提前致谢。

最佳答案

-(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)外设:(CBPeripheral *)外设 didWriteValueForCharacteristic:(CBCharacteristic *)特征错误:(NSError *)error{ }

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

相关文章:

ios - ParseClientConfiguration - 未解析的 ios sdk v1.12.0

ios - 在 Swift 3+ 中使用 NSPredicate 对自定义对象进行过滤

android - Android 和 Linux (RPi) 之间的蓝牙连接在第一次写入操作时丢失

qt - QBluetoothSocketPrivate::__ q_readNotify()14错误

iOS:在范围内时自动重新建立蓝牙 BLE 连接

android - BLE 如何从设备下载所有数据?

flutter - 在flutter中将字符串转换为Uint8Array

iphone - 我可以从我的服务器获取应用内购买的价格吗?

ios - 一旦我在STPAddCardViewController()上选择完成,应用程序就会加载并且不会停止加载

java - Android - 连接到蓝牙模块