我们正在开发 iOS 应用程序,其中我们需要将 RGB 信号传递给 BLE 设备,并且基于 RGB 代码,设备 LED 会发光。我们正在使用iOS 应用程序中的蓝牙框架对象进行CBCentralManager
连接。CBPeripheral
我们正在设置特征和描述符 UUID,但仍然无法在 BLE 设备上发送信号。这是我们用来以十六进制字节格式传递 RGB 数据的代码。
- (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
对象有什么问题吗?