如何从Objective-C中的UUID获取特性? [英] How to get the characteristic from UUID in objective-C?

查看:129
本文介绍了如何从Objective-C中的UUID获取特性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为Objective-C开发BLE.

I am developing for BLE in objective-C.

我用以下代码定义UUID:

I define the UUID like the following code:

    static NSString *const LEDStateCharacteristicUUID = @"ffffffff-7777-7uj7-a111-d631d00173f4";

我想通过以下代码向BLE设备写入特性,它需要传递3个参数:1.数据2.特性3.类型

I want to write characteristic to BLE device by following code , it need to pass 3 parameter:1.Data 2.Characteristic 3.type

CBCharacteristic *chara = ??? // how to set the characteristic via above UUID let it can pass to following function?

[peripheral writeValue:data forCharacteristic:chara type:CBCharacteristicWriteWithoutResponse];

如何通过上面的UUID设置特征以使其可以传递给writeCharacteristic函数?

How to set the characteristic via above UUID let it can pass to writeCharacteristic function?

谢谢.

推荐答案

首先,您需要知道所需的服务UUID和所需的特征UUID.当您拥有这些UUID时,可以使用以下逻辑来获取正确的特征实例:

First you need to know what service UUID and what characteristic UUID from this service you want. When you have these UUIDs you can use this logic below to get the right characteristic instance:

- (CBCharacteristic *)characteristicWithUUID:(CBUUID *)characteristicUUID forServiceUUID:(CBUUID *)serviceUUID inPeripheral:(CBPeripheral *)peripheral {

    CBCharacteristic *returnCharacteristic  = nil;
    for (CBService *service in peripheral.services) {

       if ([service.UUID isEqual:serviceUUID]) {
           for (CBCharacteristic *characteristic in service.characteristics) {

                if ([characteristic.UUID isEqual:characteristicUUID]) {

                    returnCharacteristic = characteristic;
                }
            }
        }
    }
    return returnCharacteristic;
}

这篇关于如何从Objective-C中的UUID获取特性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆