Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在按钮点击时将数据写入特征-Swift_Ios_Iphone_Swift_Xcode_Bluetooth Lowenergy - Fatal编程技术网

Ios 在按钮点击时将数据写入特征-Swift

Ios 在按钮点击时将数据写入特征-Swift,ios,iphone,swift,xcode,bluetooth-lowenergy,Ios,Iphone,Swift,Xcode,Bluetooth Lowenergy,我已经成功地将一个ios应用程序连接到我的BLE hm-10设备,并以字符串形式发送它的特征。但是,我不知道如何使用@IBAction函数在按下按钮后发送字符串 func peripheral(peripheral: CBPeripheral, didDiscoverCharacteristicsForService service: CBService, error:NSError?) { print("Found \(service.characteristics!.count) c

我已经成功地将一个ios应用程序连接到我的BLE hm-10设备,并以字符串形式发送它的特征。但是,我不知道如何使用@IBAction函数在按下按钮后发送字符串

func peripheral(peripheral: CBPeripheral, didDiscoverCharacteristicsForService service: CBService, error:NSError?)
{
    print("Found \(service.characteristics!.count) characteristics!: \(service.characteristics)")
    _peripheral = peripheral
    _characteristics = service.characteristics
    let string = "off"

    let data = string.dataUsingEncoding(NSUTF8StringEncoding)


    for characteristic in service.characteristics as [CBCharacteristic]!
    {

        if(characteristic.UUID.UUIDString == "FFE1")
        {
            print("sending data")
            peripheral.writeValue(data!, forCharacteristic: characteristic,type: CBCharacteristicWriteType.WithoutResponse)
        }
    }

}

@IBAction func onButtonTouch(sender: UIButton)
{
    let string = "on"

    let data = string.dataUsingEncoding(NSUTF8StringEncoding)

    _peripheral.writeValue(data!, forCharacteristic: _characteristics, type: CBCharacteristicWriteType.WithoutResponse)

}

当我将此代码放入底部显示的@IBAction函数时,它不起作用,因为“characteristic”是一个未解析的标识符。有没有办法将其与外围功能联系起来?我对使用Swift编码比较陌生,因此非常感谢您的帮助。谢谢。

您需要保存该特征。在
onButtonTouch(发送者:)
中,它没有声明!为什么不能将
外围设备
特征
作为全局变量?@Larme感谢您的快速回答。真的很抱歉听起来像个新手,但你能解释一下怎么做吗?谢谢大家!@DashAndRest我该如何让它们成为全局变量?谢谢。让它们成为属性,而不是全局。这修复了我的特征标识符问题,但是我现在遇到了关于viewController的错误,说“Class viewController没有初始值设定项”和CharacteristicsDataType/Class?“一行中的连续声明必须分开”一节。对不起,这是个麻烦。@DanB什么错误<代码>视图控制器我刚才提到的只是一个例子。显示包含代码的整个类。@DanB您必须将
CharacteristicsDataType/class
替换为
[CBCharacteristic]
的实际数据类型。检查更新的应答在“forCharacteristic:\u characteristics!”处获取错误“无法将“[CBCharacteristic]”类型的值转换为预期的参数类型“CBCharacteristic”。非常感谢您的帮助。因为z
\u characteristics
是一个数组,您需要从该数组中正确获取一个特征
class viewController: UIViewController {

    var _peripheral: CBPeripheral?
    var _characteristics: [CBCharacteristic]?

    func peripheral(peripheral: CBPeripheral, didDiscoverCharacteristicsForService service: CBService, error:NSError?)
    {
    print("Found \(service.characteristics!.count) characteristics!: \(service.characteristics)")
    _peripheral = peripheral
    _characteristics = service.characteristics
    let string = "off"

    let data = string.dataUsingEncoding(NSUTF8StringEncoding)


       for characteristic in service.characteristics as [CBCharacteristic]!
        {

            if(characteristic.UUID.UUIDString == "FFE1")
            {
                print("sending data")
                peripheral.writeValue(data!, forCharacteristic: characteristic,type: CBCharacteristicWriteType.WithoutResponse)
            }
        }

    }

    @IBAction func onButtonTouch(sender: UIButton)
    {
    let string = "on"

    let data = string.dataUsingEncoding(NSUTF8StringEncoding)

    let characteristic = //get right on out of _characteristics! array
    _peripheral!.writeValue(data!, forCharacteristic: characteristic, type: CBCharacteristicWriteType.WithoutResponse)

    }
}