如何从 Swift 中的 BLE 外设特征写入回调中提取数据?

Posted

技术标签:

【中文标题】如何从 Swift 中的 BLE 外设特征写入回调中提取数据?【英文标题】:How to extract data from the BLE Peripheral characteristic write callback in Swift? 【发布时间】:2018-09-06 04:56:42 【问题描述】:

我正在用 Swift 语言开发一个 ios 应用程序。该应用程序充当具有 4 个自定义可写 GATT 特性的 BLE 外围设备。每当客户端编写任何特征时,iOS都会通过以下函数触发回调:

func peripheralManager(_ peripheral: CBPeripheralManager, didReceiveWrite requests: [CBATTRequest`])

.我想知道如何从上面的回调函数中提取特征数据。谁能通过分享示例代码帮助我理解?

【问题讨论】:

【参考方案1】:

斯威夫特 4

func peripheralManager(peripheral: CBPeripheralManager, didReceiveWriteRequests requests: [CBATTRequest])

    for request in requests
    
        if request.characteristic.UUID.isEqual(characteristic.UUID) 
        
            // Set the request's value
            // to the correspondent characteristic
            characteristic.value = request.value
        
  
    peripheralManager.respondToRequest(requests[0], withResult: .Success)

也许对你有用

go through that link for more about BLE

【讨论】:

以上是关于如何从 Swift 中的 BLE 外设特征写入回调中提取数据?的主要内容,如果未能解决你的问题,请参考以下文章

未调用 BLE 外设委托

BLE 特性更改回调仅针对写入后的第一次更改执行

如何在 iOS 应用程序中快速写入 BLE 特征

Swift BLE 外设 writeValue 不起作用

如何在 android 中将特征写入 BLE GATT 服务器?

如何在 Swift 中读取 BLE 特征浮点数