How to send RGB signal to BLE device using iPhone application?

烂漫一生 提交于 2020-01-03 03:15:08

问题


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.

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

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


回答1:


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

CBMutableCharacteristic objects represent the characteristics of a local peripheral’s service (local peripheral devices are represented by CBPeripheralManager objects)

and

CBCharacteristic objects in particular represent the characteristics of a remote peripheral’s service (remote peripheral devices are represented by CBPeripheral objects)

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.



来源:https://stackoverflow.com/questions/28741931/how-to-send-rgb-signal-to-ble-device-using-iphone-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!