IOS使用Slider写入BLE特性

时间:2015-06-22 11:39:26

标签: ios bluetooth-lowenergy core-bluetooth ibaction

真的很难回写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];
}

我错过了什么?

2 个答案:

答案 0 :(得分:0)

感谢Larme的回答:

@property(非原子,强)CBC特征状态特征;当你发现它时:_statusCharacteristic = statusCharacteristicJustDiscovered;然后你可以在你的方法中重复使用它。

这很有效......

答案 1 :(得分:0)

选项是保留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];
            }
        }
    }
}