gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 05:17:45 24 4
gpt4 key购买 nike

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

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com