Mac命令行应用程序上的CoreBluetooth [英] CoreBluetooth on Mac Command line application

查看:90
本文介绍了Mac命令行应用程序上的CoreBluetooth的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建一个使用CoreBluetooth的命令行应用程序。问题是,它在命令行应用程序上不起作用。

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

我已经移动了 CoreBluetooth 代码(一个实现CBCentralManagerDelegate协议的类-我们将此类称为 myBLEManager ),从命令行应用程序项目调用到另一个Mac OS GUI应用程序。

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.

我在 ViewDidLoad()中运行了一些测试-超级样本,我只是初始化了一个 myBLEManager CBCentralManager 的实例在初始化时,然后调用 scanForPeripherals

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

这是我在CLI和GUI项目中所做的。区别在于 centralManagerDidUpdateState 从未在CLI项目中调用。

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.

推荐答案

大多数Apple框架中的回调都是通过应用程序的主运行循环传递的。如果您的命令行工具没有运行循环,则它将无法接收以这种方式发送的回调。

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.

添加足够了:

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

}

这篇关于Mac命令行应用程序上的CoreBluetooth的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆