简体   繁体   中英

IOS Write to BLE Characteristic using Slider

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?

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...

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM