jjzjj

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

coder 2023-09-28 原文

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/

有关ios - Microchip RN4020 -> MLDP模式iOS示例代码的更多相关文章

  1. ruby-on-rails - Rails - 子类化模型的设计模式是什么? - 2

    我有一个模型:classItem项目有一个属性“商店”基于存储的值,我希望Item对象对特定方法具有不同的行为。Rails中是否有针对此的通用设计模式?如果方法中没有大的if-else语句,这是如何干净利落地完成的? 最佳答案 通常通过Single-TableInheritance. 关于ruby-on-rails-Rails-子类化模型的设计模式是什么?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.co

  2. ruby - 解析 RDFa、微数据等的最佳方式是什么,使用统一的模式/词汇(例如 schema.org)存储和显示信息 - 2

    我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i

  3. ruby-on-rails - 如何从 format.xml 中删除 <hash></hash> - 2

    我有一个对象has_many应呈现为xml的子对象。这不是问题。我的问题是我创建了一个Hash包含此数据,就像解析器需要它一样。但是rails自动将整个文件包含在.........我需要摆脱type="array"和我该如何处理?我没有在文档中找到任何内容。 最佳答案 我遇到了同样的问题;这是我的XML:我在用这个:entries.to_xml将散列数据转换为XML,但这会将条目的数据包装到中所以我修改了:entries.to_xml(root:"Contacts")但这仍然将转换后的XML包装在“联系人”中,将我的XML代码修改为

  4. ruby - 如何在续集中重新加载表模式? - 2

    鉴于我有以下迁移:Sequel.migrationdoupdoalter_table:usersdoadd_column:is_admin,:default=>falseend#SequelrunsaDESCRIBEtablestatement,whenthemodelisloaded.#Atthispoint,itdoesnotknowthatusershaveais_adminflag.#Soitfails.@user=User.find(:email=>"admin@fancy-startup.example")@user.is_admin=true@user.save!ende

  5. ruby-on-rails - rspec should have_select ('cars' , :options => ['volvo' , 'saab' ] 不工作 - 2

    关闭。这个问题需要detailsorclarity.它目前不接受答案。想改进这个问题吗?通过editingthispost添加细节并澄清问题.关闭8年前。Improvethisquestion在首页我有:汽车:VolvoSaabMercedesAudistatic_pages_spec.rb中的测试代码:it"shouldhavetherightselect"dovisithome_pathit{shouldhave_select('cars',:options=>['volvo','saab','mercedes','audi'])}end响应是rspec./spec/request

  6. ruby - 如何验证 IO.copy_stream 是否成功 - 2

    这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下

  7. ruby-on-rails - Nokogiri:使用 XPath 搜索 <div> - 2

    我使用Nokogiri(Rubygem)css搜索寻找某些在我的html里面。看起来Nokogiri的css搜索不喜欢正则表达式。我想切换到Nokogiri的xpath搜索,因为这似乎支持搜索字符串中的正则表达式。如何在xpath搜索中实现下面提到的(伪)css搜索?require'rubygems'require'nokogiri'value=Nokogiri::HTML.parse(ABBlaCD3"HTML_END#my_blockisgivenmy_bl="1"#my_eqcorrespondstothisregexmy_eq="\/[0-9]+\/"#FIXMEThefoll

  8. Ruby 文件 IO 定界符? - 2

    我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示:foo4.500bar3.001.33foobar如何读取由空格而不是换行符分隔的文件?有什么方法可以设置File("file.txt").foreach方法以使用空格而不是换行符作为分隔符? 最佳答案 接受的答案将slurp文件,这可能是大文本文件的问题。更好的解决方案是IO.foreach.它是惯用的,将按字符流式传输文件:File.foreach(filename,""){|string|putsstring}包含“thisisanexample”结果的

  9. ruby - 是否有用于序列化和反序列化各种格式的对象层次结构的模式? - 2

    给定一个复杂的对象层次结构,幸运的是它不包含循环引用,我如何实现支持各种格式的序列化?我不是来讨论实际实现的。相反,我正在寻找可能会派上用场的设计模式提示。更准确地说:我正在使用Ruby,我想解析XML和JSON数据以构建复杂的对象层次结构。此外,应该可以将该层次结构序列化为JSON、XML和可能的HTML。我可以为此使用Builder模式吗?在任何提到的情况下,我都有某种结构化数据-无论是在内存中还是文本中-我想用它来构建其他东西。我认为将序列化逻辑与实际业务逻辑分开会很好,这样我以后就可以轻松支持多种XML格式。 最佳答案 我最

  10. Get https://registry-1.docker.io/v2/: net/http: request canceled while waiting - 2

    1.错误信息:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:requestcanceledwhilewaitingforconnection(Client.Timeoutexceededwhileawaitingheaders)或者:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:TLShandshaketimeout2.报错原因:docker使用的镜像网址默认为国外,下载容易超时,需要修改成国内镜像地址(首先阿里

随机推荐