【问题标题】:Swift not discovering any bluetooth devicesSwift 没有发现任何蓝牙设备
【发布时间】:2017-04-17 20:27:33
【问题描述】:

所以我写了一个(相当)简单的代码来扫描蓝牙设备,并连接到一个具有匹配名称的设备,但是当找到一个 BT 设备时应该调用的函数永远不会被调用。有人可以看看这个,看看你是否能在我的工作中发现一个错误?

我尝试过的事情:

  1. 将扫描初始化中的服务 uuid 更改为 nil 以扫描所有设备
  2. 使用 NSLog 语句检测 didDiscover 程序何时发现设备
  3. 在每个函数开始时要中断的一堆断点。

我对此感到非常困惑,我关注了一个名为 The 12 Steps of Bluetooth 的页面,并仔细检查了苹果开发者参考页面。

import UIKit
import CoreBluetooth


class ViewController: UIViewController, CBCentralManagerDelegate, CBPeripheralDelegate {

    var manager:CBCentralManager!
    var peripheral:CBPeripheral!
    var peripherals = Array<CBPeripheral>()

    let devName = "CHS Bus Loop"
    let servUUID = CBUUID(string: "6e400001-b5a3-f393-e0a9-e50e24dcca9e")
    let txChar = CBUUID(string: "6E400002-B5A3-F393-E0A9-E50E24DCCA9E")
    let rxChar = CBUUID(string: "6E400003-B5A3-F393-E0A9-E50E24DCCA9E")
    var txCharacteristic: CBCharacteristic?

    override func viewDidLoad() {
        super.viewDidLoad()
        manager = CBCentralManager(delegate: self, queue: nil)
        textLabel.text="CHS We Are One"
        // Do any additional setup after loading the view, typically from a nib.
    }

    func centralManagerDidUpdateState(_ central: CBCentralManager) {
        if central.state == CBManagerState.poweredOn {
            central.scanForPeripherals(withServices: nil, options: nil)
        } else {
            print("Bluetooth not available.")
        }
    }



    func centralManager(central: CBCentralManager, didDiscoverperipheral peripheral: CBPeripheral, advertisementData: [String : AnyObject], RSSI: NSNumber) {
        let device = (advertisementData as NSDictionary)
            .object(forKey: CBAdvertisementDataLocalNameKey)
            as? NSString
        if device?.contains(devName) == true{
            self.manager.stopScan()

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

            manager.connect(peripheral, options: nil)
        }

        func centralManager2(central: CBCentralManager, didConnectperipheral peripheral: CBPeripheral){
            peripheral.discoverServices(nil)
        }
        func peripheral(peripheral: CBPeripheral,didDiscoverServices error: NSError?){
            for service in peripheral.services! {
                let thisService = service as CBService

                if service.uuid == txChar{
                    peripheral.discoverCharacteristics(nil, for:thisService)
                }
            }
        }

        func peripheral(
            peripheral: CBPeripheral,
            didDiscoverCharacteristicsForService service: CBService,
            error: NSError?) {
            for characteristic in service.characteristics! {
                let thisCharacteristic = characteristic as CBCharacteristic

                if thisCharacteristic.uuid == rxChar {
                    self.peripheral.setNotifyValue(
                        true, 
                        for: thisCharacteristic
                    )
                }
            }
        }




        peripherals.append(peripheral)
        //tableView.reloadData()

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    //@IBOutlet weak var tableView: UITableView!

    @IBOutlet weak var deviceSelector: UITableView!

    @IBOutlet weak var textField1: UITextField!
    @IBOutlet weak var textField2: UITextField!
    @IBOutlet weak var textField3: UITextField!
    @IBOutlet weak var textField4: UITextField!
    @IBOutlet weak var textField5: UITextField!
    @IBOutlet weak var textField6: UITextField!
    @IBOutlet weak var direct1: UISegmentedControl!
    @IBOutlet weak var direct2: UISegmentedControl!
    @IBOutlet weak var direct3: UISegmentedControl!
    @IBOutlet weak var direct4: UISegmentedControl!
    @IBOutlet weak var direct5: UISegmentedControl!
    @IBOutlet weak var direct6: UISegmentedControl!
    @IBOutlet weak var textLabel: UILabel!

    @IBAction func UpdateDecimal(_ sender: Any) {
        var LED1=""
        var LED2=""
        var LED3=""
        var LED4=""
        var LED5=""
        var LED6=""
        var combine=""

        if direct1.selectedSegmentIndex == 2{
            LED1 = "002"
        }else{
            LED1 = self.textField1.text! + "\(direct1.selectedSegmentIndex)"
        }

        if direct2.selectedSegmentIndex == 2{
            LED2 = "002"
        }else{
            LED2 = self.textField2.text! + "\(direct2.selectedSegmentIndex)"
        }

        if direct3.selectedSegmentIndex == 2{
            LED3 = "002"
        }else{
            LED3 = self.textField3.text! + "\(direct3.selectedSegmentIndex)"
        }

        if direct4.selectedSegmentIndex == 2{
            LED4 = "002"
        }else{
            LED4 = self.textField4.text! + "\(direct4.selectedSegmentIndex)"
        }

        if direct5.selectedSegmentIndex == 2{
            LED5 = "002"
        }else{
            LED5 = self.textField5.text! + "\(direct5.selectedSegmentIndex)"
        }

        if direct6.selectedSegmentIndex == 2{
            LED6 = "002"
        }else{
            LED6 = self.textField6.text! + "\(direct6.selectedSegmentIndex)"
        }

        if LED1.characters.count == 2{
            LED1="0" + LED1
        }
        if LED2.characters.count == 2{
            LED2="0" + LED2
        }
        if LED3.characters.count == 2 {
            LED3="0" + LED3
        }
        if LED4.characters.count == 2{
            LED4="0" + LED4
        }
        if LED5.characters.count == 2{
            LED5="0" + LED5
        }
        if LED6.characters.count == 2{
            LED6="0" + LED6
        }

        if LED1.characters.count + LED2.characters.count + LED3.characters.count + LED4.characters.count + LED5.characters.count + LED6.characters.count != 18{
            textLabel.text = "All MUST have 2 digits"
        }else{
            combine = LED1 + LED2 + LED3 + LED4 + LED5 + LED6
            textLabel.text = combine
        }
        let data = combine.data(using: String.Encoding.utf8)
        peripheral.writeValue(data!, for: txCharacteristic!,
            type:CBCharacteristicWriteType.withoutResponse);


    }
 }

【问题讨论】:

  • 我没有看到你在初始化管理器后调用 scanForPeripherals。如果 centralManagerDidUpdateState(_:) 没有运行,您将永远不会扫描外围设备。
  • @Dare 第 24 行。它在 func centralManagerDidUpdateState 中,它检查状态是否已通电,如果是,则开始扫描。
  • 所以你从来没有接到过didDiscoverPeripheral的电话?您是否尝试过 LightBlue 应用程序以查看它是否可以看到您的外围设备?检查外设名称的广告数据可能不起作用,具体取决于您的外设和广告的内容。
  • 正如 Paulw11 所说,检查您是否可以使用 LightBlue 看到您的设备,它是否真的是 BLE 设备并且可以找到(如果它做广告)。但我猜你的问题在于混合 Swift 3 和旧的 Swift 方法。检查那里 (stackoverflow.com/questions/40491818/…) 以获得适合您需求的答案。
  • 替换:func centralManager(central: CBCentralManager, didDiscoverperipheral peripheral: CBPeripheral, advertisementData: [String : AnyObject], RSSI: NSNumber) => func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber)。再次阅读我的链接答案。检查委托方法的所有真实声明,尤其是没有“_”的委托方法。而且你不能拥有像你这样的centralManager2()...,它也不行。

标签: ios swift bluetooth core-bluetooth


【解决方案1】:

在@Larme的帮助下,我发现有一些swift2函数和一些swift3函数,它们不能一起工作。这是他的全部赞扬:

替换: func centralManager(central: CBCentralManager, didDiscoverperipheral peripheral: CBPeripheral, AdvertisementData: [String : AnyObject], RSSI: NSNumber) => func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, AdvertisementData: [String : Any ], rssi RSSI: NSNumber)。再次阅读我的链接答案。检查委托方法的所有真实声明,尤其是没有“_”的委托方法。而且你不能有一个 centralManager2()... 像你的一样,它也不行。

如果@Larme 希望自己发布答案,我会纠正他的并删除我的。

【讨论】:

    猜你喜欢
    • 2021-05-17
    • 1970-01-01
    • 2012-04-19
    • 2017-03-17
    • 2011-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多