GDL 50 отображается в меню «Настройки» > «Bluetooth». и я также могу с ним соединиться. Другие приложения, такие как ForeFlight, могут получать и отображать данные от него.
Однако следующий быстрый код не отображает устройства (сопряженные или нет):
< pre class="lang-swift Prettyprint-override">
Код: Выделить всё
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
}
}
Код: Выделить всё
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)
}
}
Я буду очень благодарен за любую помощь и/или подсказки. в правильном направлении.
TIA
Ссылки:
[*]https: //static.garmin.com/pumac/190-02087-02_B.pdf
https://static.garmin.com/pumac/190-02087-10_03.pdf
Подробнее здесь: https://stackoverflow.com/questions/787 ... 17-with-sw