CoreBluetooth on Mac Command line application

荒凉一梦 提交于 2019-12-12 18:28:33

问题


I'm trying to build a Command Line app that uses CoreBluetooth. Problem is, it doesn't work on command line apps.

I've moved the my CoreBluetooth code (a class that implements the CBCentralManagerDelegate protocol - let's call this class myBLEManager) from the Command Line app project to another Mac OS GUI App.

I ran some tests in ViewDidLoad() -- supersample, I just init a myBLEManager that creates an instance of CBCentralManager on initialization, then calls scanForPeripherals.

This is what I do in both the CLI and GUI projects. Difference is centralManagerDidUpdateState never gets called in the CLI project. but it does in the GUI Mac app.


回答1:


Callbacks in most Apple frameworks are delivered through your application's main run loop. If your command-line tool does not have a run loop, it cannot receive callbacks that are sent this way.

Without a runloop, the only way for the framework to invoke your callback would be to run it on another thread, which could lead to weird behavior in an application that didn't expect that.

It's sufficient to add:

let runLoop = RunLoop.current
let distantFuture = Date.distantFuture
while running == true && runLoop.run(mode: RunLoopMode.defaultRunLoopMode, before: distantFuture) {

}


来源:https://stackoverflow.com/questions/42634584/corebluetooth-on-mac-command-line-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!