iOS蓝牙无法发现设备

时间:2016-11-04 02:14:29

标签: ios iphone swift bluetooth bluetooth-lowenergy

我正在尝试创建一个可以扫描外围BLE设备的应用。我使用的是BLE Mini,我可以看到LightBlue和RedBear iPhone应用程序。我已确认在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)
    }
}

1 个答案:

答案 0 :(得分:1)

您的didDiscoverPeripheral委托方法不正确。

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

_

之前,您需要功能签名中的central:
private func centralManager(_ central: CBCentralManager, didDiscoverPeripheral peripheral: CBPeripheral, advertisementData: [String : AnyObject], RSSI: NSNumber)
{
    peripherals.append(peripheral)
    tableView.reloadData()
    print("saw something")
}
相关问题