2

I found a cool tutorial to code an app that lists all available Bluetooth Devices on an iOS 10 iPhone in Swift. It works O-K. Running the app on my iPhone with bluetooth on, it finds and presents my Macbook, a linux computer, and random 'unnamed' devices in a tableView. However, it cannot find my BeatsByDre wireless headphones, Raspberry Pi in Discoverable Mode nor a simple bluetooth dongle plugged into a computer (running linux).

My question is: what the hell am I not understanding/doing wrong?

Here's my code for the Table View:

    import CoreBluetooth
    import UIKit

    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) {
        scanBLEDevice()
    }
    func scanBLEDevice(){
        manager?.scanForPeripherals(withServices: nil, options: nil)

        DispatchQueue.main.asyncAfter(deadline: .now() + 60.0) {
            self.stopScanForBLEDevice()
        }

    }
    func stopScanForBLEDevice(){
        manager?.stopScan()
        print("scan stopped")
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        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
        return cell
    }

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let peripheral = peripherals[indexPath.row]
        manager?.connect(peripheral, options: nil)
    }

    //CBCentralMaganerDelegate code
    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 parentview
        parentView?.mainPeripheral = peripheral
        peripheral.delegate = parentView
        peripheral.discoverServices(nil)
        // set 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!)
    }

Basically, I look for peripherals and list them to this Table View Controller. How come I can't see my wireless headphones in the list but I can see them under Settings>Bluetooth? Am I completely misunderstanding what a Peripheral is? I've read the Apple docs and then some. What am I supposed to look for in my code? Should I be using Beacons/iBeacon?

Paulw11
  • 108,386
  • 14
  • 159
  • 186
AMK1234321
  • 65
  • 1
  • 2
  • 8

1 Answers1

3

You will only be able to discover Bluetooth Low Energy (BLE) devices.

The devices you listed are Bluetooth 2.1 devices and cannot be seen by Core Bluetooth or are not advertising GATT services.

Paulw11
  • 108,386
  • 14
  • 159
  • 186
  • The Wireless Headphones are BLE4.0 – AMK1234321 Nov 08 '16 at 23:05
  • and the BLE Dongle is BLE 4.0 as well. How would I find those? As in, what other framework would I use? – AMK1234321 Nov 08 '16 at 23:06
  • They need to be advertising a GATT service. You can do this on the Raspberry Pi using Bluez (I believe, I have never done it). I doubt that headphones will be using GATT even if they are Bluetooth 4.0 You can also confirm with the LightBlue app on the app store – Paulw11 Nov 08 '16 at 23:08
  • Do you know if the Rasp-Pi3 OnBoard Bluetooth will do this? I've also been working the RPi and again, the app does not see the Rpi. I've installed Bluez on the Rpi and downloaded LightBlue on my iPhone. Even LightBlue doesn't discover the RPi when it's in Discover-Mode – AMK1234321 Nov 08 '16 at 23:11
  • Discover isn't applicable; you need to be advertising a GATT service – Paulw11 Nov 08 '16 at 23:12
  • Also, thank you. I've seen the GATT information but was unsure if that's what I wanted or just hcitools... – AMK1234321 Nov 08 '16 at 23:12
  • sweet - this gives me new key words to lookup. Already found more helpful links... – AMK1234321 Nov 08 '16 at 23:14
  • Small add-on: BLE is available since 4.0 version, but being a Bluetooth 4.+ doesn't mean it's BLE compatible. BLE is a "fork" inside Bluetooth, separating it from "Classical Bluetooth", that's why even if your Headphone is Bluetooth 4.0 doesn't means it's BLE compatible, plus it had to advertise in the BLE mode (GATT, etc.) – Larme Nov 09 '16 at 11:12
  • So future reference or for anyone who's also having issues: I got the raspberry Pi3 to advertise in BLE mode. Helpful links in the order that everything should be installed on your Pi3 (or Pi with Bluetotoh Dongle) https://www.pi-supply.com/make/fix-raspberry-pi-3-bluetooth-issues/ http://www.bluez.org/download/ ($sudo wget http://www.bluez.org/download/bluez-5.43.tar.xz) https://urbanjack.wordpress.com/2014/06/05/how-to-set-bluez-into-ble-or-le-only-mode-ibeacon/ Then you can start advertising with `$sudo hciconfig hci0 leadv` stop advertising with: `$sudo hciconfig hci0 noleadv` – AMK1234321 Nov 09 '16 at 21:43
  • answered here - (http://stackoverflow.com/questions/40636726/nearby-bluetooth-devices-using-swift-3-0) – Dylan Mar 22 '17 at 14:26