蓝牙无法发现设备

时间:2016-08-07 09:56:16

标签: ios iphone swift bluetooth core-bluetooth

首先,我知道这个问题在SO上已经被问了很多。我经历了所有人试图解决它但无济于事。

我有一个简单的应用程序,可扫描蓝牙设备。这是我的代码。

import CoreBluetooth
import UIKit

class ViewController: UIViewController, CBCentralManagerDelegate, CBPeripheralDelegate {

    var manager: CBCentralManager!
    var peripheral: CBPeripheral!

    override func viewDidLoad() {
        super.viewDidLoad()

        manager = CBCentralManager(delegate: self, queue: nil)
    }

    // MARK: - CBCentralManagerDelegate
    func centralManagerDidUpdateState(central: CBCentralManager) {
        print(#function)

        switch central.state {
        case .Unsupported:
            print("Unsupported")
        case .Unauthorized:
            print("Unauthorized")
        case .PoweredOn:
            print("Powered On")
            central.scanForPeripheralsWithServices(nil, options: nil)
        case .Resetting:
            print("Resetting")
        case .PoweredOff:
            print("Powered Off")
        case .Unknown:
            print("Unknown")
        }
    }

    func centralManager(central: CBCentralManager, didDiscoverPeripheral peripheral: CBPeripheral, advertisementData: [String : AnyObject], RSSI: NSNumber) {
        print(#function)

        print("Discovered \(peripheral.name) at \(RSSI)")

        if peripheral.name!.containsString(name) {
            manager.stopScan()

            self.peripheral = peripheral
            self.peripheral.delegate = self

            manager.connectPeripheral(peripheral, options: nil)
        }
    }

    func centralManager(central: CBCentralManager, didConnectPeripheral peripheral: CBPeripheral) {
        print(#function)
    }

    func centralManager(central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: NSError?) {
        central.scanForPeripheralsWithServices(nil, options: nil)
    }


    // MARK: - CBPeripheralDelegate
    func peripheral(peripheral: CBPeripheral, didDiscoverServices error: NSError?) {
        print(#function)

        guard let services = peripheral.services else {
            return
        }

        for service in services {
            print(service.UUID)
            if service.UUID == serviceUUID {
                peripheral.discoverCharacteristics(nil, forService: service)
            }
        }
    }

    func peripheral(peripheral: CBPeripheral, didDiscoverCharacteristicsForService service: CBService, error: NSError?) {
        print(#function)

        guard let characteristics = service.characteristics else {
            return
        }

        for characteristic in characteristics {
            print(characteristic.UUID)
            if characteristic.UUID == scratchUUID {
                peripheral.setNotifyValue(true, forCharacteristic: characteristic)
            }
        }
    }

    func peripheral(peripheral: CBPeripheral, didUpdateValueForCharacteristic characteristic: CBCharacteristic, error: NSError?) {
        print(#function)

        var count: UInt32 = 0
        if characteristic.UUID == scratchUUID {
            characteristic.value!.getBytes(&count, length: sizeof(UInt32))
            print(String(format: "%llu", count))
        }
    }

}

我在iPhone 5(iOS 9.3.4)中运行它。我有一台运行Windows的笔记本电脑,一部Android手机,一台iPad Air 2和我的Mac全部蓝牙都打开了。但这些设备都没有被发现。调用centralManagerDidUpdateState方法并开始扫描,但就是这样。 didDiscoverPeripheral委托方法永远不会被调用。

我重复了这个过程,这一次是在iPad Air 2中运行的,但结果相同。

如果我转到设备上的蓝牙菜单,我会看到其他设备被发现。

在一些SO答案中,我看到在主队列中运行它会起作用。像这样,manager = CBCentralManager(delegate: self, queue: dispatch_get_main_queue())。但这对我来说也不起作用。

我真的很感激你的帮助。

1 个答案:

答案 0 :(得分:1)

您的其他设备可能没有宣传BLE服务(如评论中提到的Paulw11)。您提到的所有设备都不会显示为可发现,除非它们位于“蓝牙设置”页面中。

有适用于iOS,Android和Windows的应用,可以通过LE进行广告宣传。最容易使用的是LightBlue for iOS。在iPhone上运行您的应用程序,而其他iOS设备正在做广告,您应该看到广告。在Windows或Android上使用LE广告扫描仪来仔细检查LightBlue是否正在做广告可能是有用的。

  

注意 Windows尚不支持蓝牙LE的外设角色,   所以,即使你能看到广告,你也不会   能够连接到它。

相关问题