IOS 使用滑块写入 BLE 特性

IOS Write to BLE Characteristic using Slider

真的很难写回 BLE 外围设备。请帮忙...

我已连接并已阅读可用的特性并希望从 IBAction 滑块回信:

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

并调用以下写入:

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

我错过了什么?

感谢 Larme 的回答:

@属性(非原子,强)CBCharacteristic statusCharacteristic;当你发现它时:_statusCharacteristic = statusCharacteristicJustDiscovered;然后你可以在你的方法中重用它。

效果很好...

一个选项是保留 CBPeripheral 对象。在检测到服务和特征后,您可以 运行 像这样:

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