如何使用iPhone应用程序将RGB信号发送到BLE设备

如何使用iPhone应用程序将RGB信号发送到BLE设备

本文介绍了如何使用iPhone应用程序将RGB信号发送到BLE设备?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在开发iOS应用程序,需要将RGB信号传递给BLE设备,并且基于RGB代码,设备LED会发光。我们正在使用 CBCentralManager 来连接iOS应用程序中蓝牙框架的 CBPeripheral 对象。

We are working on iOS application and in which we need to pass the RGB signal to the BLE device and based on RGB code the device LED will glow. We are doing the connection using CBCentralManager for the CBPeripheral object of Bluetooth Framework in iOS app.

我们正在设置特征和描述符UUID,但仍然无法在BLE设备上发送信号。这是我们用于以十六进制字节格式传递RGB数据的代码。

We are setting the characteristic and descriptor UUID but still we are not able to send the signal on the BLE device. Here is the code we are using to pass RGB data in hex bytes format.

- (void)centralManager:(CBCentralManager )central didConnectPeripheral:(CBPeripheral )peripheral
{
    NSLog(@"connect peripheral");

    unsigned char bytes[] = { 0x5, 0x1, 0x70, 0x70, 0x70, 0x70, 0x48, 0x49,0x48, 0x49, 0x48, 0x65, 0x48, 0x49, 0x48, 0x48};
    NSData *nsData = [[NSData alloc] initWithBytes:bytes length:sizeof(bytes)];

    CBMutableCharacteristic *myCharacteristic = [[CBMutableCharacteristic alloc] initWithType:[CBUUID UUIDWithString:@"00002a46-0000-1000-8000-00805f9b34fb"] properties:CBCharacteristicPropertyWriteWithoutResponse value:nil permissions:CBAttributePermissionsReadable];

    CBMutableDescriptor *yourDescriptor = [[CBMutableDescriptor alloc]initWithType:userDescriptionUUID value:@"00002902-0000-1000-8000-00805f9b34fb"];

    myCharacteristic.descriptors = @[yourDescriptor];

    [myCharacteristic setValue:nsData];


    [self.peripheral writeValue:nsData forCharacteristic:myCharacteristic type:CBCharacteristicWriteWithoutResponse];
    [self.peripheral setNotifyValue:YES forCharacteristic:myCharacteristic];
}

我们这样做正确吗?发送数据或创建 CBMutableCharacteristic 对象是否存在任何问题?

Are we doing it the right way? Is there any issue in sending the data or creating the CBMutableCharacteristic object ?

推荐答案

您不能简单地创建CBMutableCharacteristic-您必须发现CBCharacteristic或从外设的特征属性中检索它。

You cannot simply create a CBMutableCharacteristic - you must discover the CBCharacteristic or retrieve it from the peripheral's characteristics property.

如果您参考文档,将会看到

If you refer to the documentation you will see

您在注释中指出无法找到服务和设备的特性-在这种情况下,建议您检查发现代码或将其添加到问题中。

You state in a comment that you are unable to discover the services and characteristics of your device - in this case I suggest you examine your discovery code or add it to your question.

这篇关于如何使用iPhone应用程序将RGB信号发送到BLE设备?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 13:07