Reputation: 85
I am attempting to create an app which will scan for peripheral BLE devices. I am using a BLE Mini which I can see with both the LightBlue and RedBear iPhone applications. I have confirmed that scanning starts when the BLE is powered on, however when I run my program, no BLE devices are discovered. I have looked through a lot of examples for implementing Corebluetooth in iOS and it appears that I have all the required functions. Where am I going wrong? Thank you for the help.
// 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)
}
}
Upvotes: 1
Views: 476
Reputation: 114783
Your didDiscoverPeripheral
delegate method is incorrect.
I notice that you have made it private
; presumably this is because Xcode gave you an error that the function "almost matched the signature" of another method and suggested making it private. Doing so hid this method from external classes and removed the error, but it means that you do not have the didDiscoverPeripheral
delegate method implemented.
You need the _
in the function signature before central:
private func centralManager(_ central: CBCentralManager, didDiscoverPeripheral peripheral: CBPeripheral, advertisementData: [String : AnyObject], RSSI: NSNumber)
{
peripherals.append(peripheral)
tableView.reloadData()
print("saw something")
}
Upvotes: 1