1

I am developing for BLE in objective-C.

I define the UUID like the following code:

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

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

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

Thanks in advance.

Matthias Bauch
  • 89,811
  • 20
  • 225
  • 247
Martin
  • 2,813
  • 11
  • 43
  • 66
  • not getting what u actually want here?? – Paresh Navadiya Feb 26 '15 at 08:04
  • @Prince I have edit , I want to get the parameter for characteristic via UUID and pass it to writeCharacteristic function. – Martin Feb 26 '15 at 08:10
  • You want to create an UUID which includes a string then retrieve this information at the reception? – Duyen-Hoa Feb 26 '15 at 08:14
  • You would normally discover the services and characteristics of the peripheral when you connect to it - stash the CBCharacteristic in a property and then you can write to it. – Paulw11 Feb 26 '15 at 09:53
  • Similar to this question. http://stackoverflow.com/questions/28741931/how-to-send-rgb-signal-to-ble-device-using-iphone-application – AppAspect Feb 26 '15 at 12:13

2 Answers2

5

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;
}
Salmo
  • 1,788
  • 1
  • 18
  • 29
1

You need to set a delegate for the peripheral:

peripheral.delegate = self;

In didConnectToPeripheral you discover the peripheral's services. In the peripheral's didDiscoverServices callback you then discover characteristics. In didDiscoverCharacteristics you then loop through each characteristic and save them in a variable.

- (void)peripheral:(CBPeripheral *)peripheral
didDiscoverCharacteristicsForService:(CBService *)service
             error:(NSError *)error
{
    if (error) {
        NSLog(@"Error discovering characteristics: %@", error.localizedDescription);
    } else {
        NSLog(@"Discovered characteristics for %@", peripheral);

        for (CBCharacteristic *characteristic in service.characteristics) {

            if ([characteristic.UUID.UUIDString isEqualToString: LEDStateCharacteristicUUID]) {
                // Save a reference to it in a property for use later if you want
                _LEDstateCharacteristic = characteristic;

                [peripheral writeValue:data forCharacteristic: _LEDstateCharacteristic type:CBCharacteristicWriteWithoutResponse];
            }
        }
    }
}
Kane Cheshire
  • 1,654
  • 17
  • 20