【问题标题】:Why I don't get instance of paired device via CoreBluetooth?为什么我没有通过 CoreBluetooth 获得配对设备的实例?
【发布时间】:2017-02-21 11:39:51
【问题描述】:

我有一个设备(后来命名为“扫描仪”)和其他设备(后来命名为“石头”)。扫描仪可以扫描宝石并在内置显示器上显示它们的信息。扫描仪可以通过蓝牙发送宝石信息,我想读取这些数据并在我的应用程序中使用它。我开始了代码连接实现,但我发现了一个问题。这是我的代码:

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 已与扫描仪连接,扫描仪会告诉我连接正常。

我想达到什么目标?

我想创建检查扫描仪是否已连接的视图控制器。

if connected then
    get scan information
else
    find device and connect
get scan information

请帮助我。 :)

编辑

LightBlue Explorer app 中,我的扫描仪设备没有出现。 但是有了这个app 就完美了。

也许使用 CoreBluetooth 是一种错误的做法。什么是更好的。如果上述应用程序的创建者可以沟通,那么您就有可能这样做。

【问题讨论】:

  • 您的扫描仪设备是否在宣传 BLE 服务?从应用商店试用 LightBlue 应用。
  • @Paulw11 如果没有怎么办?
  • 您需要显式连接到didDiscoverPeripheral 中的外围设备,以便调用didConnect 方法。不知道为什么你的设备没有被发现。这些“石头”到底是什么?
  • 石头无关紧要。我想将我的设备与扫描仪连接,但它没有出现在didDiscoverPeripheral
  • 该设备的说明书显示它使用 iPod 附件协议,因此该设备在 MFi 程序下。这意味着它使用外部附件框架和传统蓝牙。

标签: ios swift core-bluetooth external-accessory


【解决方案1】:

感谢@Paulw11 的评论我意识到我的“扫描仪”使用了 iPod 附件协议。确保我导入 ExternalAccessory 框架并检查设备。

    EAAccessoryManager.shared().showBluetoothAccessoryPicker(withNameFilter: nil, completion: nil)

调用此函数后,我看到了附件设备列表,并且我的扫描仪在那里。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-30
    • 1970-01-01
    相关资源
    最近更新 更多