【发布时间】:2016-11-08 22:58:34
【问题描述】:
我找到了 cool tutorial 来编写一个应用程序,该应用程序在 Swift 中列出了 iOS 10 iPhone 上所有可用的蓝牙设备。它工作正常。在打开蓝牙的 iPhone 上运行该应用程序,它会在 tableView 中找到并显示我的 Macbook、Linux 计算机和随机“未命名”设备。但是,它找不到我的 BeatsByDre 无线耳机、处于可发现模式的 Raspberry Pi,也找不到插入计算机(运行 linux)的简单蓝牙加密狗。
我的问题是:我到底在理解什么/做错了什么?
这是我的表格视图代码:
import CoreBluetooth
import UIKit
class ScanTableViewController: UITableViewController,CBCentralManagerDelegate {
var peripherals:[CBPeripheral] = []
var manager: CBCentralManager? = nil
var parentView:MainViewController? = nil
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidAppear(_ animated: Bool) {
scanBLEDevice()
}
func scanBLEDevice(){
manager?.scanForPeripherals(withServices: nil, options: nil)
DispatchQueue.main.asyncAfter(deadline: .now() + 60.0) {
self.stopScanForBLEDevice()
}
}
func stopScanForBLEDevice(){
manager?.stopScan()
print("scan stopped")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return peripherals.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "scanTableCell", for: indexPath)
let peripheral = peripherals[indexPath.row]
cell.textLabel?.text = peripheral.name
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let peripheral = peripherals[indexPath.row]
manager?.connect(peripheral, options: nil)
}
//CBCentralMaganerDelegate code
func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
if (!peripherals.contains(peripheral)){
peripherals.append(peripheral)
}
self.tableView.reloadData()
}
func centralManagerDidUpdateState(_ central: CBCentralManager) {
print(central.state)
}
func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
// pass reference to connected peripheral to parentview
parentView?.mainPeripheral = peripheral
peripheral.delegate = parentView
peripheral.discoverServices(nil)
// set manager's delegate view to parent so it can call relevant disconnect methods
manager?.delegate = parentView
parentView?.customiseNavigationBar()
if let navController = self.navigationController{
navController.popViewController(animated: true)
}
print("Connected to "+peripheral.name!)
}
func centralManager(_ central: CBCentralManager, didFailToConnect peripheral: CBPeripheral, error: Error?) {
print(error!)
}
基本上,我会寻找外围设备并将它们列出到这个 Table View Controller 中。为什么我在列表中看不到我的无线耳机,但我可以在“设置”>“蓝牙”下看到它们?我是否完全误解了外围设备是什么?我已经阅读了 Apple 文档,然后是一些。我应该在我的代码中寻找什么?我应该使用 Beacons/iBeacon 吗?
【问题讨论】:
-
该代码在上面不起作用。在扫描外围设备之前,您需要确保状态为 powerOn。
标签: ios swift bluetooth core-bluetooth