【问题标题】:How to write data from iOS Swift 3 app to BLE (HM-10)?如何将数据从 iOS Swift 3 应用程序写入 BLE (HM-10)?
【发布时间】:2017-05-19 00:35:25
【问题描述】:

我正在尝试将数据从 iPhone 发送到 HM-10

我已经与蓝牙设备建立连接,但是我不知道如何发送数据,这是外围委托功能吗?

当一个按钮被按下时,一个固定的整数(1,2,3,4...)将被发送到 BLE 设备一次。 此外,我有滑块,我希望将滑块的值发送到

我该怎么做?

谢谢

【问题讨论】:

    标签: ios swift xcode bluetooth bluetooth-lowenergy


    【解决方案1】:

    在您使用 CBPheral.writeValue 函数将数据写入设备之前,您必须发现这些特征、服务并保持对它们的强引用。

    苹果的教程中描述了所有内容:https://developer.apple.com/library/content/documentation/NetworkingInternetWeb/Conceptual/CoreBluetooth_concepts/PerformingCommonCentralRoleTasks/PerformingCommonCentralRoleTasks.html#//apple_ref/doc/uid/TP40013257-CH3-SW1

    它在 Obj-C 中,但很容易将其翻译成 swift

    【讨论】:

    • 感谢您的回答,但是我不太擅长 swift,因此从 Obj-C 更改为 swift 是我不需要的复杂问题的另一层:/
    【解决方案2】:

    您正在寻找这种方法:

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

    见:https://developer.apple.com/reference/corebluetooth/cbperipheral/1518747-writevalue

    【讨论】:

    • 嗨 Lars,我看到了您引用的链接,但是当我尝试在按钮 IBAction func 位中执行 writeValue 函数时,没有 writeValue 选项。例如我会做mainPeripheral。并且没有选择'writeValue'的选项
    • 您是否已经拥有来自外围设备发现的 CBPeripheral 实例?
    • 不,我有两个视图控制器。在一个视图控制器上,我有一个用于连接蓝牙的 tableview。在第二个视图控制器上,我有要按下以将信息发送到蓝牙的按钮。在第二个视图控制器上,我目前仅使用 CBPeripheralDelegate。这是正确的吗?
    • Lars,我设法让 writeValue 出现。但是,我将哪些信息放入 Data/CBCharacteristic 中?我不确定那里。这个例子有一些但不是完整的答案(stackoverflow.com/questions/35794107/swift-ble-communications)
    • 你知道特征接受多少字节作为输入吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-02-13
    • 1970-01-01
    • 2016-01-12
    • 2020-01-18
    • 1970-01-01
    • 2019-05-21
    • 2017-02-16
    相关资源
    最近更新 更多