【问题标题】:Cannot find Bluetooth device with Swift code scanner on iOS在 iOS 上找不到带有 Swift 代码扫描仪的蓝牙设备
【发布时间】:2019-08-25 21:39:27
【问题描述】:

我正在将非标准蓝牙设备与我的 iOS 应用配对。

我发现 iPhone 只检测特定的设备类。测试是在: - 联网笔记本电脑 - 类:0x02010c - 手表 - 类:0xff0704

这些设备已通过内置蓝牙扫描仪成功找到。我也能够与他们配对。

我正在我的应用程序中进行扫描并尝试与设备配对。不幸的是,在用 Swift 编写我的蓝牙扫描仪后,我的手机没有检测到该设备。

class ViewController: UIViewController, CBCentralManagerDelegate {  
    //MARK: Properties
    @IBOutlet weak var scanButton: UIButton!
    @IBOutlet weak var devicesList: UITextView!
    private var centralManager: CBCentralManager!
    private var bluetoothOn: Bool!
    private var scanInProgress: Bool!

    //MARK: Initialization
    override func viewDidLoad() {
        super.viewDidLoad()

        centralManager = CBCentralManager(delegate: self, queue: .main, options: [CBCentralManagerScanOptionAllowDuplicatesKey: NSNumber(value: false)])
        bluetoothOn = false
        scanInProgress = false
    }

    //MARK: Actions
    @IBAction func scanDevices(_ sender: UIButton) {
        if bluetoothOn && !scanInProgress {
            scanInProgress = true
            centralManager.scanForPeripherals(withServices: nil, options: nil)
        } else {
            centralManager.stopScan()
        }
    }

    func centralManagerDidUpdateState(_ central: CBCentralManager) {
        if central.state == .poweredOn {
            print("Bluetooth is On")
            bluetoothOn = true
         }
    }

    func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
        let line1 = "Name   : \(peripheral.name ?? "(No name)")"
        let line2 = "\nRSSI     : \(RSSI)"
        let line3 = "\nUUID     : \(peripheral.identifier)"
        var lineN = ""
        let lineEnd = "\n------------------------------------------------------\n"

        for ad in advertisementData {
            lineN.append("\nAD Data: \(ad)")
        }

        devicesList.text += "\n \(line1) + \(line2) + \(line3) + \(line4) + \(line5) +\(lineN) + \(lineEnd)"
        print(devicesList.text as Any)
    }
}

我希望我的代码能够找到与内置系统扫描仪完全一样的蓝牙设备。为什么我的扫描仪找不到设备?内置蓝牙扫描器和 Swift API 扫描方案有区别吗?

【问题讨论】:

  • 那是因为您在扫描设备时将服务设置为 nil。
  • 它可以毫无问题地检测到我周围的所有其他设备。据我所知,设置 nil 可以扫描该区域内的所有设备。
  • 如果你这么说...
  • “如果 serviceUUIDs 参数为 nil,则返回所有发现的外围设备,无论其支持的服务如何” - Apple 文档 developer.apple.com/documentation/corebluetooth/…
  • 您只能通过 Core Bluetooth 发现 BLE 外设广告 GATT 服务。您的外围设备使用什么配置文件?

标签: ios swift iphone bluetooth swift4


【解决方案1】:

我发现使用 CoreBluetooth 框架找不到任何 BLE 设备。苹果只是阻止了这种可能性。 唯一的选择是使用私有 API 来连接我想要的任何蓝牙设备。最好的解决方案是从 GitHub 获取 BeeTee 项目。它只是展示了如何使用 Private API 扫描蓝牙设备。

这个解决方案工作得很好,不幸的是,它使应用程序无法进入 AppStore。苹果不允许这种解决方案,因此应用程序将无法通过验证过程。

【讨论】:

  • 我尝试了 LightBlue 和 BlueCap 应用程序,但没有人在搜索中找到我的打印机。我们现在该怎么办?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-26
  • 1970-01-01
相关资源
最近更新 更多