See all Discoverable Bluetooth Devices Nearby - Swift iOS XCode

10,833

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.

Share:
10,833
AMK1234321
Author by

AMK1234321

Updated on December 01, 2022

Comments

  • AMK1234321
    AMK1234321 over 1 year

    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?

    • d0n13
      d0n13 almost 5 years
      That code won't work above. You need to make sure the status is poweredOn before you scan for peripherals.
  • AMK1234321
    AMK1234321 over 7 years
    The Wireless Headphones are BLE4.0
  • AMK1234321
    AMK1234321 over 7 years
    and the BLE Dongle is BLE 4.0 as well. How would I find those? As in, what other framework would I use?
  • Paulw11
    Paulw11 over 7 years
    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
  • AMK1234321
    AMK1234321 over 7 years
    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
  • Paulw11
    Paulw11 over 7 years
    Discover isn't applicable; you need to be advertising a GATT service
  • AMK1234321
    AMK1234321 over 7 years
    Also, thank you. I've seen the GATT information but was unsure if that's what I wanted or just hcitools...
  • AMK1234321
    AMK1234321 over 7 years
    sweet - this gives me new key words to lookup. Already found more helpful links...
  • Larme
    Larme over 7 years
    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.)
  • AMK1234321
    AMK1234321 over 7 years
    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) pi-supply.com/make/fix-raspberry-pi-3-bluetooth-issues bluez.org/download ($sudo wget bluez.org/download/bluez-5.43.tar.xz) urbanjack.wordpress.com/2014/06/05/… Then you can start advertising with $sudo hciconfig hci0 leadv stop advertising with: $sudo hciconfig hci0 noleadv
  • Dylan
    Dylan about 7 years