1

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 ?

Mohd Prophet
  • 1,511
  • 10
  • 17
AppAspect
  • 4,461
  • 2
  • 30
  • 46
  • We tried this code but not helpful. https://github.com/RedBearLab/iOS/tree/master/Examples/BLE%20RGB/BLE%20RGB – AppAspect Feb 26 '15 at 12:05
  • 2
    How have you set `self.peripheral`? This needs to be the peripheral that was passed to the delegate method. Also, you would normally call `discoverServices` and `discoverCharacteristicsForService` to get the `CBCharacteristic` rather than just creating a `CBMutableCharacteristic` – Paulw11 Feb 26 '15 at 12:22
  • We have done the same way you are saying but just provided the final method code data so that we can find the correct solution here. Anything wrong we are doing in this method? – AppAspect Feb 26 '15 at 12:24
  • 2
    Yes, you cannot simply create a CBMutableCharacteristic - you must discover the CBCharacteristic or retrieve it from the peripheral's characteristics property. Refer to the documentation - a CBMutableCharacteristic represents a *local* characteristic, not a remote characteristic – Paulw11 Feb 26 '15 at 12:27
  • I agree with @Paulw11. You should read the Apple BLE example (especially the Thermometer one), to understand how that works. – Larme Feb 26 '15 at 12:43
  • @Paulw11: Actually we are not getting the peripheral's characteristics property from the device we have and that is the reason we have to create custom characteristics here and passing it for the device so that it can blink the LED with RGB signal. What do we need to do if there is no characteristics coming from device? Just for your reference: this is working fine in Android. – AppAspect Feb 26 '15 at 12:49
  • 1
    You need to look at your discovery code - you must retrieve the characteristics from the peripheral. – Paulw11 Feb 26 '15 at 12:55
  • I can second @Paulw11 as I'm just learning / playing around with Core Bluetooth right this moment. You MUST traverse the stack in the manner Paul said - find the device, connect to it, get its service(s), discover the characteristics for the service, and ONLY then can you set the value you want (in my case I just want to read a value, but I have to go through this same procedure. As Central you cannot use a 'mutable' that you create from scratch - each service/characteristic is non mutable and returned in the appropriate delegate method. [Paul you should make your comments into an answer.] – David H Feb 26 '15 at 14:35

1 Answers1

0

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.

Paulw11
  • 108,386
  • 14
  • 159
  • 186