Search code examples
iosswiftcore-bluetoothexternal-accessory

Why I don't get instance of paired device via CoreBluetooth?


I have a device (named later "scanner") and other devices (named later "stone"). Scanner can scan stones and display their information on build in display. Scanner can send stones information via Bluetooth and I want to read this data and use it in my app. I started code connection implementation but I discovered a problem. Here is my code:

import UIKit
import CoreBluetooth

class BluetoothViewController: UIViewController {

    var manager:CBCentralManager!
    var peripheral:CBPeripheral!

    override func viewDidLoad() {
        super.viewDidLoad()

        manager = CBCentralManager(delegate: self, queue: nil)
    }
}

extension BluetoothViewController: CBCentralManagerDelegate {

    func centralManagerDidUpdateState(_ central: CBCentralManager) {
        if central.state == .poweredOn {
            central.scanForPeripherals(withServices: nil, options: nil)
        }
        else {
            print("Bluetooth not available.")
        }
    }

    func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
        print("peripheral \(peripheral)")
        print("rssi \(RSSI)")
        print("advertisementData: \(advertisementData)")
        print("--------")
    }

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

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

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

extension BluetoothViewController: CBPeripheralDelegate {

}

The problem is scanner doesn't appear in

func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber)

and

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

never call.

Note that my iPhone is connected with a scanner and scanner tell me the connection is working.

What I want to achieve?

I want to create viewcontroller which check if scanner is connected.

if connected then
    get scan information
else
    find device and connect
get scan information

Please help me. :)

Edit

In LightBlue Explorer app my scanner device doesn't appear. But with this app works perfectly.

Maybe using CoreBluetooth is a wrong way to do this. What is better. If creators of above app can communicate thee is a possibility to do it.


Solution

  • Thanks @Paulw11's comment I realized my "scanner" uses iPod Accessory Protocol. To make sure I import ExternalAccessory framework and check for devices.

        EAAccessoryManager.shared().showBluetoothAccessoryPicker(withNameFilter: nil, completion: nil)
    

    After call this function I saw list of accessory devices and my scanner was there.