Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asynchronous Swift issues

I am a Javascript developer poking around in Swift and am having a hard time migrating an example Playground app I found (https://github.com/gregiOS/Playgrounds/tree/master/BLE.playground) to a CLI app.

import Cocoa
import PlaygroundSupport

let tableViewController = TableViewController()
let dataSource = tableViewController.dataSource

PlaygroundPage.current.liveView = tableViewController.view

let scanner = BluetoothScanner { scanner in
    scanner.startScanning { (peripheral) in
        print("Discovered peripheral: \(peripheral.tableViewData)")
    }
}

My desire, and what I tried is to just remove import PlaygroundSupport and the dataSource/tableViewController stuff and just have the peripherals printed out to stdout, however the program just exits immediately. I tried using a dispatch group, but that didn't seem to work either:

import Cocoa

let myGroup = DispatchGroup()

print("Scanning...")
myGroup.enter()

let scanner = BluetoothScanner { scanner in
  scanner.startScanning { (peripheral) in
    print("Discovered peripheral: \(peripheral.tableViewData)")
    myGroup.leave()
  }
}

myGroup.notify(queue: .main) {
  print("Finished all requests.")
}

Also tried using myGroup.wait() but it just sat there doing nothing. I believe part of the problem is that the scan runs indefinitely, whereas I just need it to run for 2 seconds or so and stop.

Point is, I'm in over my head and need to create a PoC showing Bluetooth discovery. I would greatly appreciate any pointers.

like image 886
Rob M. Avatar asked Dec 21 '25 08:12

Rob M.


1 Answers

To run asynchronous stuff in a CLI you need a runloop

let runLoop = CFRunLoopGetCurrent()
print("Scanning...")

let scanner = BluetoothScanner { scanner in
  scanner.startScanning { (peripheral) in
    print("Discovered peripheral: \(peripheral.tableViewData)")
    CFRunLoopStop(runLoop)
  }
}

CFRunLoopRun()
like image 132
vadian Avatar answered Dec 24 '25 01:12

vadian



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!