IOS Write to BLE Characteristic using Slider

311 views Asked by At

Really struggling with writing back to a BLE Peripheral. Please help...

I'm connected and have read the characteristics available and wish to write back from a IBAction Slider:

-(IBAction)SrControlIndex:(UISegmentedControl *)sender
{
    switch (_SRControl.selectedSegmentIndex)
    {
        case 0:
            [self writeModeCharacteristic:Status_UUID data:[@"00" dataUsingEncoding:NSUTF8StringEncoding]];
            NSLog(@"First Sel");
            break;
        case 1:
            NSLog(@"Second Sel");
            break;
        default: 
            break; 
    }
}

And calls the following write:

-(void)writeModeCharacteristic:(CBCharacteristic *)ModeCharacteristic data:(NSData*)data
    {
    [ModeCharacteristic.service.peripheral writeValue:data     forCharacteristic:ModeCharacteristic type:CBCharacteristicWriteWithResponse];
}

What am I missing?

2

There are 2 answers

0
duck1970 On BEST ANSWER

Thanks to Larme for the answer:

@property (nonatomic, strong) CBCharacteristic statusCharacteristic; When you discovers it: _statusCharacteristic = statusCharacteristicJustDiscovered; Then you can reuse it in your method.

This worked great...

0
istirbu On

An option is to keep the CBPeripheral object. After detecting services and characteristics you can run something like this:

for (CBService *service in self.peripheral.services) {
    if ([service.UUID isEqual:[CBUUID UUIDWithString:@"YOUR-SERVICE-UUID"]]) {
        for (CBCharacteristic *characteristic in service.characteristics) {
            if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:@"YOUR-CHARACTERISTIC-UUID"]]) {
                [self.peripheral writeValue:data forCharacteristic:characteristic type:CBCharacteristicWriteWithResponse];
            }
        }
    }
}