简体   繁体   English

iOS使用Slider写入BLE特性

[英]IOS Write to BLE Characteristic using Slider

Really struggling with writing back to a BLE Peripheral. 真正努力回写BLE外围设备。 Please help... 请帮忙...

I'm connected and have read the characteristics available and wish to write back from a IBAction Slider: 我已连接并阅读了可用的特性,并希望从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; 
    }
}

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: 感谢Larme的回答:

@property (nonatomic, strong) CBCharacteristic statusCharacteristic; @属性(非原子的,强的)CB特征状态特征; When you discovers it: _statusCharacteristic = statusCharacteristicJustDiscovered; 当您发现它时:_statusCharacteristic = statusCharacteristicJustDiscovered; Then you can reuse it in your method. 然后,您可以在您的方法中重用它。

This worked great... 这很棒...

An option is to keep the CBPeripheral object. 一种选择是保留CBPeripheral对象。 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];
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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