【问题标题】:iOS Bluetooth Does Not Discover DevicesiOS 蓝牙未发现设备
【发布时间】:2017-03-17 19:01:27
【问题描述】:

我正在尝试创建一个可以扫描外围 BLE 设备的应用。我正在使用可以在 LightBlue 和 RedBear iPhone 应用程序中看到的 BLE Mini。我已经确认在 BLE 通电时开始扫描,但是当我运行我的程序时,没有发现任何 BLE 设备。我浏览了很多在 iOS 中实现 Corebluetooth 的示例,看来我拥有所有必需的功能。我哪里错了?谢谢你的帮助。

//  BlueToothVC.swift
import UIKit
import CoreBluetooth
class BlueToothVC: UIViewController, UITableViewDataSource, UITableViewDelegate, CBCentralManagerDelegate 
{
    @IBOutlet weak var tableView: UITableView!
    var centralManager: CBCentralManager!
    var peripherals: Array<CBPeripheral> = Array<CBPeripheral>()

    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.delegate = self
        tableView.dataSource = self
        centralManager = CBCentralManager(delegate: self, queue: DispatchQueue.main)


        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false

        // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
        // self.navigationItem.rightBarButtonItem = self.editButtonItem()
    }

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

    //CoreBluetooth methods
    func centralManagerDidUpdateState(_ central: CBCentralManager)
    {
        if (central.state == CBManagerState.poweredOn)
        {
            print("scanning")
            self.centralManager?.scanForPeripherals(withServices: nil, options: nil)
        }
        else
        {
            // do something like alert the user that ble is not on
        }
    }

    private func centralManager(central: CBCentralManager, didDiscoverPeripheral peripheral: CBPeripheral, advertisementData: [String : AnyObject], RSSI: NSNumber)
    {
        peripherals.append(peripheral)
        tableView.reloadData()
        print("saw something")
    }

    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return peripherals.count
    }


    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = self.tableView.dequeueReusableCell(withIdentifier: "BTCell")! as! BTCell
        let peripheral = peripherals[indexPath.row]
        cell.label.text = peripheral.name
        return cell
    }

    @IBAction func touchCancel(_ sender: AnyObject) {
        self.navigationController?.popViewController(animated: true)
    }
}

【问题讨论】:

    标签: ios iphone swift bluetooth bluetooth-lowenergy


    【解决方案1】:

    您的didDiscoverPeripheral 委托方法不正确。

    我注意到你成功了private;这大概是因为 Xcode 给了您一个错误,即该函数“几乎匹配另一个方法的签名”并建议将其设为私有。这样做会从外部类中隐藏此方法并消除错误,但这意味着您没有实现 didDiscoverPeripheral 委托方法。

    central:之前的函数签名中需要_

    private func centralManager(_ central: CBCentralManager, didDiscoverPeripheral peripheral: CBPeripheral, advertisementData: [String : AnyObject], RSSI: NSNumber)
    {
        peripherals.append(peripheral)
        tableView.reloadData()
        print("saw something")
    }
    

    【讨论】:

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