0
我發現了一個cool tutorial編寫一個應用程序,列出了在iOS 10 iPhone在斯威夫特所有可用的藍牙設備。它適用於O-K。使用藍牙在我的iPhone上運行應用程序,它會查找並呈現我的Macbook,一臺Linux計算機以及隨機的'未命名'設備。但是,它無法找到我的BeatsByDre無線耳機,Raspberry Pi處於可發現模式,也沒有找到插入計算機(運行Linux)的簡單藍牙適配器。查看所有發現的藍牙設備附近的餐廳 - 斯威夫特的iOS的XCode
我的問題是:我究竟在我不理解/做錯了什麼?
這裏是我的表視圖代碼:
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!)
}
基本上,我期待與外圍設備和它們列出來此表視圖控制器。如何在列表中看不到我的無線耳機,但我可以在設置>藍牙下看到它們?我完全誤解Peripheral是什麼?我已經閱讀了蘋果文檔,然後閱讀了一些。我在代碼中尋找什麼?我應該使用Beacon/iBeacon嗎?
的無線耳機是BLE4.0 – AMK1234321
和BLE加密狗是BLE 4.0爲好。我將如何找到這些?如在,我會使用其他什麼框架? – AMK1234321
他們需要廣告GATT服務。你可以使用Bluez在Raspberry Pi上做到這一點(我相信,我從來沒有這樣做過)。我懷疑耳機是否會使用GATT,即使它們是藍牙4.0也可以使用應用商店中的LightBlue應用程序進行確認 – Paulw11