0

I have a viewcontroller that scans for Bluetooth devices and displays the name and UUID in a table view. It picks up every device but not my Android phone. Is there something wrong with my code or is the Android device blocking me? I've switched the Android device Bluetooth on and off and there is no change in the device detected when refreshing my view controller.

import UIKit
import CoreBluetooth

class ScanTableViewController: UITableViewController, CBCentralManagerDelegate {

    var peripherals:[CBPeripheral] = []
    var manager:CBCentralManager? = nil
    var parentView:MainViewController? = nil

    override func viewDidLoad() {
        super.viewDidLoad()    
    }

    override func viewDidAppear(_ animated: Bool) {
        scanBLEDevices()
    }    

    // MARK: - Table view data source
    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

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

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "scanTableCell", for: indexPath)
        let peripheral = peripherals[indexPath.row]
        //cell.textLabel?.text = peripheral.name //##print identifier

        let deviceUUID = peripheral.identifier.uuidString
        let deviceName = peripheral.name
        cell.textLabel?.text = "Name \(String(describing: deviceName)) UUID \(String(describing: deviceUUID))"

        return cell
    }

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let peripheral = peripherals[indexPath.row]

        manager?.connect(peripheral, options: nil)
    }

    // MARK: BLE Scanning
    func scanBLEDevices() {
        //if you pass nil in the first parameter, then scanForPeriperals will look for any devices.
        manager?.scanForPeripherals(withServices: nil, options: nil)

        //stop scanning after 3 seconds
        DispatchQueue.main.asyncAfter(deadline: .now() + 3.0) {
            self.stopScanForBLEDevices()
        }
    }

    func stopScanForBLEDevices() {
        manager?.stopScan()
    }

    // MARK: - CBCentralManagerDelegate Methods
    func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
        if(!peripherals.contains(peripheral)) {
            peripherals.append(peripheral)
        }

        self.tableView.reloadData()
    }

    func centralManagerDidUpdateState(_ central: CBCentralManager) {
        print(central.state)
    }

    func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {

        //pass reference to connected peripheral to parent view
        parentView?.mainPeripheral = peripheral
        peripheral.delegate = parentView
        peripheral.discoverServices(nil)

        //set the manager's delegate view to parent so it can call relevant disconnect methods
        manager?.delegate = parentView
        parentView?.customiseNavigationBar()

        if let navController = self.navigationController {
            navController.popViewController(animated: true)
        }

        print("Connected to " +  peripheral.name!)
    }

    func centralManager(_ central: CBCentralManager, didFailToConnect peripheral: CBPeripheral, error: Error?) {
        print(error!)
    }

}
  • 1
    What BLE service is your Android phone advertising? BLE scanning is only meant to find BLE devices that are advertising, not "Bluetooth devices" generally. Do other BLE scanning applications (LightBlue for example) see your phone? – Rob Napier May 28 '20 at 15:44
  • In the Bluetooth settings under Available Devices, I see BLEPeripheralApp so I assume this Huawei P30 is BLE compatible. – Sebastien Desemberg May 28 '20 at 15:57
  • "BLE compatible" does not mean "currently advertising over BLE." Do you believe that this phone is advertising over BLE? (Why do you believe that?) – Rob Napier May 28 '20 at 16:31

0 Answers0