现在已经有几年了,Microchip已经发布了带有私有MLDP的RN4020 BT LE芯片。然而,尽管他们在苹果应用商店中有一个iOS应用程序,但到目前为止仍然没有公开可用的iOS示例源代码。有没有人有工作代码,愿意分享/张贴?
谢谢!
时间
发布于 2016-12-18 00:39:45
我有一些可以工作的代码。我将在这里给出一些代码片段。在符合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)
}对于找到的每个外围设备,将调用以下委托成员
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")
}我们将外围设备存储在字典中,并使用表视图将其名称呈现给用户。如果用户选择了外围设备,我们会尝试连接到该外围设备
@IBAction func connect(_ sender: Any) {
// selectedPeripheral set by selection from the table view
cbc?.connect(selectedPeripheral!, options: nil)
}成功的连接将导致调用以下委托方法:
func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
performSegue(withIdentifier: "ConnectPeriph", sender: self)
}这导致负责连接状态的第二ViewController。此ViewController符合CBPeripheralDelegate协议,并声明了以下变量:
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")连接后的第一个动作是发现外围设备提供的服务:
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
periph.delegate = self
periph.discoverServices(nil)
}这将导致对此委托方法的调用:
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)
}
}https://stackoverflow.com/questions/40785023
复制相似问题