[英]Why I don't get instance of paired device via CoreBluetooth?
我有一個設備(以后稱為“掃描儀”)和其他設備(以后稱為“ stone”)。 掃描儀可以掃描寶石並在內置顯示器中顯示其信息。 掃描儀可以通過藍牙發送寶石信息,我想讀取此數據並在我的應用程序中使用它。 我開始執行代碼連接實現,但是發現了一個問題。 這是我的代碼:
import UIKit
import CoreBluetooth
class BluetoothViewController: UIViewController {
var manager:CBCentralManager!
var peripheral:CBPeripheral!
override func viewDidLoad() {
super.viewDidLoad()
manager = CBCentralManager(delegate: self, queue: nil)
}
}
extension BluetoothViewController: CBCentralManagerDelegate {
func centralManagerDidUpdateState(_ central: CBCentralManager) {
if central.state == .poweredOn {
central.scanForPeripherals(withServices: nil, options: nil)
}
else {
print("Bluetooth not available.")
}
}
func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
print("peripheral \(peripheral)")
print("rssi \(RSSI)")
print("advertisementData: \(advertisementData)")
print("--------")
}
func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
print(peripheral)
}
func centralManager(_ central: CBCentralManager, didFailToConnect peripheral: CBPeripheral, error: Error?) {
print(peripheral)
}
func centralManager(_ central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: Error?) {
print(peripheral)
}
}
extension BluetoothViewController: CBPeripheralDelegate {
}
問題是掃描儀沒有出現在
func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber)
和
func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral)
永遠不要打電話。
請注意,我的iPhone已與掃描儀連接,掃描儀會告訴我該連接正在工作。
我想創建檢查掃描儀是否已連接的viewcontroller。
if connected then
get scan information
else
find device and connect
get scan information
請幫我。 :)
在LightBlue Explorer應用程序中,沒有出現我的掃描儀設備。 但與此應用程序完美地工作。
也許使用CoreBluetooth是錯誤的方法。 什么是更好的。 如果上述應用的創建者可以與您交流,則可以這樣做。
感謝@ Paulw11的評論,我意識到我的“掃描儀”使用iPod附件協議。 為了確保我導入ExternalAccessory
框架並檢查設備。
EAAccessoryManager.shared().showBluetoothAccessoryPicker(withNameFilter: nil, completion: nil)
調用此功能后,我看到了附件設備列表,並且我的掃描儀在那里。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.