swift - 使用 Swift 中的按钮通过蓝牙发送数据

标签 swift button bluetooth bluetooth-lowenergy

我正在尝试通过单击按钮通过蓝牙发送数据。使用以下代码打开应用程序时,我可以发送数据:

var manager: CBCentralManager!
var device: CBPeripheral?
var characteristics: [CBCharacteristic]?
var serviceUUID = "1234"
var char1 = "FFE1"
let deviceName = "HMSoft"

func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {
    DidDiscoverChar.text = "Characteristic found!"

    device = peripheral
    characteristics = service.characteristics

    var value: UInt8 = 1

    let data = NSData(bytes: &value, length: MemoryLayout<UInt8>.size)

    for characteristic in service.characteristics as [CBCharacteristic]!
    {
        if(characteristic.uuid.uuidString == "FFE1")
        {
            device?.writeValue(data as Data, for: characteristic,type: CBCharacteristicWriteType.withoutResponse)

        }
    }
}

但是当我尝试使用按钮发送它时:

@IBAction func ledOn(_ sender: AnyObject) {
    var value: UInt8 = 1
    let data = NSData(bytes: &value, length: MemoryLayout<UInt8>.size)

    device?.writeValue(data as Data, for: characteristics,type: CBCharacteristicWriteType.withoutResponse)
}

我得到错误:

Cannot convert value of type '[CBCharacteristic]?' to expected argument type 'CBCharacteristic'

这似乎是一项简单的任务,但作为一个新手,我被困住了。

编辑:(对 rmaddy 回答的回应)

正如您所建议的,它确实可以遍历数组,如下所示。

@IBAction func ledOn(_ sender: AnyObject) {

    var value: UInt8 = 1

    let data = NSData(bytes: &value, length: MemoryLayout<UInt8>.size)


    for characteristic in characteristics as [CBCharacteristic]!
    {

        if(characteristic.uuid.uuidString == "FFE1")
        {
            device?.writeValue(data as Data, for: characteristic,type: CBCharacteristicWriteType.withoutResponse)

        }
    }
}

作为一名程序员,我想提高,我希望代码更高效。所以我想我可以在按钮函数之外进行迭代,以避免每次按下按钮时都进行迭代。为了说明我在 Playground 上做了一个简单的例子:

var characteristicFFE1: String = ""
var array = ["r1", "r2", "r3", "FFE1", "r4"]

for uuidString in array {
    if uuidString == "FFE1" {
       characteristicFFE1 = uuidString
    }
}

然后像这样在 IBAction 的 writeValue 中直接使用 characteristicFFE1:

@IBAction func ledOn(_ sender: AnyObject) {
var value: UInt8 = 1
let data = NSData(bytes: &value, length: MemoryLayout<UInt8>.size)

device?.writeValue(data as Data, for: characteristicsFFE1,type: CBCharacteristicWriteType.withoutResponse)
}

我尝试完成上面的操作,如下图:

var characteristicFFE1: CBCharacteristic?

 override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    for characteristic in characteristics as [CBCharacteristic]!  //ERROR HERE
    {

        if(characteristic.uuid.uuidString == "FFE1")
        {
            characteristicFFE1 = characteristic

        }
    }

但我收到错误:Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value.

有人知道如何解决这个问题吗?我阅读了有关可选值和展开的内容,但在这里仍然感到困惑。

最佳答案

你的问题在这里

  device?.writeValue(data as Data, for: characteristics,type: CBCharacteristicWriteType.withoutResponse)

你想发送一个你在这里声明的 CBCharacteristic 类型的可选数组

   var characteristics: [CBCharacteristic]?

虽然参数应该是 CBCharacteristic 类型的值,但您应该在像这样使用之前对其进行初始化

   var characteristics: CBCharacteristic?

这是来自 Docs 的方法签名

func writeValue(_ data: Data, for characteristic: CBCharacteristic,type:CBCharacteristicWriteType)

关于swift - 使用 Swift 中的按钮通过蓝牙发送数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48844845/

相关文章:

Swift3 UIWebView 委托(delegate)方法未被调用

ios - 无法在 Swift xcode 6.3.1 中使用类型为 'functionName' 的参数列表调用 '([(nameOfClass)])'

ios - 翠鸟图像加载错误

ios - 为什么具有单个 Cocoapod 依赖项的样板 Cocoa Touch Framework 项目中的样板单元测试甚至没有运行就失败了?

kotlin - 点击后传递参数

Javascript:从按钮调用对话框

android - 如何将 Android 应用程序正确连接到支持蓝牙的 Arduino 微 Controller 上的 RFCOMM socket ?

javascript - 使用 javascript 删除按钮

ios - 为什么 iOS 蓝牙会多次发现同一个设备?

ios - Beacon 未发现 iOS