【发布时间】:2018-12-08 03:19:13
【问题描述】:
我是一名使用 Swift 的 Javascript 开发人员,我很难将我找到的示例 Playground 应用程序 (https://github.com/gregiOS/Playgrounds/tree/master/BLE.playground) 迁移到 CLI 应用程序。
import Cocoa
import PlaygroundSupport
let tableViewController = TableViewController()
let dataSource = tableViewController.dataSource
PlaygroundPage.current.liveView = tableViewController.view
let scanner = BluetoothScanner { scanner in
scanner.startScanning { (peripheral) in
print("Discovered peripheral: \(peripheral.tableViewData)")
}
}
我的愿望和我尝试的只是删除 import PlaygroundSupport 和 dataSource/tableViewController 东西,然后将外围设备打印到标准输出,但是程序会立即退出。我尝试使用调度组,但这似乎也不起作用:
import Cocoa
let myGroup = DispatchGroup()
print("Scanning...")
myGroup.enter()
let scanner = BluetoothScanner { scanner in
scanner.startScanning { (peripheral) in
print("Discovered peripheral: \(peripheral.tableViewData)")
myGroup.leave()
}
}
myGroup.notify(queue: .main) {
print("Finished all requests.")
}
还尝试使用myGroup.wait(),但它只是坐在那里无所事事。我相信问题的一部分是扫描无限期地运行,而我只需要它运行 2 秒左右然后停止。
重点是,我想不通,需要创建一个显示蓝牙发现的 PoC。我将不胜感激。
【问题讨论】:
标签: swift core-bluetooth