gpt4 book ai didi

ios - Microchip RN4020 -> MLDP模式iOS示例代码

转载 作者:可可西里 更新时间:2023-11-01 05:40:16 27 4
gpt4 key购买 nike

Microchip 发布具有私有(private) MLDP 配置文件的 RN4020 BT LE 芯片已经有好几年了。然而,到目前为止,仍然没有公开可用的 iOS 示例源代码可用,尽管他们在 Apple App Store 中有一个 iOS 应用程序。有没有人有任何工作代码并愿意分享/发布它?

谢谢!

蒂姆

最佳答案

我有一些工作代码。我会在这里给出一些片段。在符合 CBCentralManagerDelegate 的第一个 ViewController 中,我们有:

var cbc : CBCentralManager? = nil

override func viewDidLoad() {
super.viewDidLoad()
cbc = CBCentralManager(delegate: self, queue: nil)
}

触摸一个按钮开始扫描外围设备

@IBAction func scan(_ sender: Any) {
cbc?.scanForPeripherals(withServices: nil, options: nil)
}

对于找到的每个外围设备,将调用以下委托(delegate)成员

func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
// store peripherals here to let select user one
NSLog("name=%@", peripheral.name ?? "unnamed")
}

我们将外围设备存储在字典中,并使用表格 View 将其名称呈现给用户。如果用户选择了一个外设,我们会尝试连接到它

@IBAction func connect(_ sender: Any) {
// selectedPeripheral set by selection from the table view
cbc?.connect(selectedPeripheral!, options: nil)
}

成功的连接将调用以下委托(delegate)方法:

func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
performSegue(withIdentifier: "ConnectPeriph", sender: self)
}

这导致第二个 ViewController 负责连接状态。此 ViewController 符合 CBPeripheralDelegate 协议(protocol)并声明以下变量:

var periph : CBPeripheral!  // selected peripheral
var dataChar : CBCharacteristic? // characteristic for data transfer

let mchpPrivateService : CBUUID = CBUUID(string: "00035B03-58E6-07DD-021A-08123A000300")
let mchpDataPrivateChar : CBUUID = CBUUID(string: "00035B03-58E6-07DD-021A-08123A000301")

连接后的第一个 Action 是发现外设提供的服务:

override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)

periph.delegate = self
periph.discoverServices(nil)
}

这导致调用此委托(delegate)方法:

func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {
if let e = error {
NSLog("Error %@", e.localizedDescription)
}
else if let services = peripheral.services {
for s in services {
NSLog("Service=%@", s.uuid.uuidString)
if s.uuid.isEqual(mchpPrivateService) {
peripheral.discoverCharacteristics(nil, for: s)
}
}
}
}

这反过来会导致发现特征:

func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {
NSLog("characteristics for service %@", service.uuid.uuidString)
if let characteristics = service.characteristics {
for c in characteristics {
if c.uuid.isEqual(mchpDataPrivateChar) {
peripheral.setNotifyValue(true, for: c)
dataChar = c
}
}
}
}

我们唯一感兴趣的特征是具有 uuid 的 mchpDataPrivateChar。通知请求导致调用:

func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {
NSLog("update value for %@", characteristic.uuid)
if let d = characteristic.value {
var s : String = String()
for b in d {
s.append(Character(UnicodeScalar(b)))
}
NSLog("received \(d.count) bytes: \(s)")
}
}

这就完成了 iOS 端的接收器。通过以下方式发送字节:

@IBAction func sendClicked(_ sender: Any) {
if let d = dataChar, let s=sendEdit.text {
let buffer : [UInt8] = Array(s.utf8)
let data : Data = Data(buffer)
periph.writeValue(data, for: d, type: .withResponse)
}
}

关于ios - Microchip RN4020 -> MLDP模式iOS示例代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40785023/

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