alienfromouterspace
alienfromouterspace

Reputation: 257

How to get ADS-B data over bluetooth from Germin GDL 50 into iPad IOS 17 with swift

I'm trying to build an iPad app (iOS 17.x) to receive ADS-B data from Germin GDL 50 device.

The GDL 50 shows up in Settings > Bluetooth and I can also pair with it. The other apps like ForeFlight can receive and display data from it.

The following swift code however doesn't show the devices (paired or not):

class BluetoothManager: NSObject, CBCentralManagerDelegate, ObservableObject {
  var centralManager: CBCentralManager!

    override init() {
        super.init()
        centralManager = CBCentralManager(delegate: self, queue: nil)
    }

    func centralManagerDidUpdateState(_ central: CBCentralManager) {
        if central.state == .poweredOn {
            // The Bluetooth is powered on, you can start scanning or retrieve connected peripherals
            retrieveConnectedDevices()
        } else {
            // Handle other states accordingly
        }
    }

    func retrieveConnectedDevices() {
        let connectedPeripherals = centralManager.retrieveConnectedPeripherals(withServices: [])
        print("BT: Connected Peripherals: \(connectedPeripherals)")
        // Handle the connected peripherals as needed
    }
}

I also tried scanning with this code...

class BluetoothManager: NSObject, CBCentralManagerDelegate, CBPeripheralDelegate, ObservableObject {
    var centralManager: CBCentralManager!
    var adsbPeripheral: CBPeripheral?
    
    override init() {
        super.init()
        centralManager = CBCentralManager(delegate: self, queue: nil)
        print("BT: BlueToothManager.init()")
    }
    
    // MARK: - CBCentralManagerDelegate
    
    func centralManagerDidUpdateState(_ central: CBCentralManager) {
        print("BT: BlueToothManager.centralManagerDidUpdateState()")
        switch central.state {
        case .poweredOn:
            print("BT: BlueToothManager.centralManagerDidUpdateState() : (.poweredOn) Start scanning for peripherals")
            centralManager.scanForPeripherals(withServices: nil, options: nil)
        default:
            print("BT: BlueToothManager.centralManagerDidUpdateState() : Bluetooth is not available")
        }
    }
    
    func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
        if let name = peripheral.name {
            print("BT: got device \(name)")
        }
        if let name = peripheral.name, name.contains("GDL") {
            logs.append("BT: BlueToothManager.centralManager() : Found GDL 50")
            adsbPeripheral = peripheral
            adsbPeripheral?.delegate = self
            centralManager.stopScan()
            centralManager.connect(peripheral, options: nil)
        }
    }
    
    func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
        print("BT: BlueToothManager.centralManager() : Discover services on the connected peripheral")
        peripheral.discoverServices(nil)
    }
    
    // MARK: - CBPeripheralDelegate
    
    func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {
        print("BT: BlueToothManager.peripheral() : Discover characteristics for each service")
        if let services = peripheral.services {
            for service in services {
                peripheral.discoverCharacteristics(nil, for: service)
            }
        }
    }
    
    func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {
        print("BT: BlueToothManager.peripheral() : Subscribe to characteristics that provide ADS-B data")
        if let characteristics = service.characteristics {
            for characteristic in characteristics {
                peripheral.setNotifyValue(true, for: characteristic)
            }
        }
    }
    
    func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {
        print("BT: BlueToothManager.peripheral() : Handle received data")
        if let data = characteristic.value {
            handleReceivedData(data)
        }
    }
    
    func handleReceivedData(_ data: Data) {
        logs.append("BT: BlueToothManager.handleReceivedData() : Process the received ADS-B data \(data)")
        let decoder = ADSBDecoder(bt: self)
        if let decodedMessage = decoder.decodeMessage(data) {
            print("BT: BlueToothManager.handleReceivedData() : Decoded Message: \(decodedMessage)")
        }
    }
    func startScanning() {
        print("BT: BlueToothManager.startScanning() : This method can be called from ContentView to start scanning")
        centralManager?.scanForPeripherals(withServices: nil, options: nil)
    }
}

Which shows my other BT-enabled devices around me, but not GDL 50.

I will be very grateful for any help and/or pointers in the right direction.

TIA

References:

Upvotes: 0

Views: 54

Answers (0)

Related Questions