I have the following code in a Xcode 6 playground:
import Cocoa
import IOBluetooth
class BlueDelegate : IOBluetoothDeviceInquiryDelegate {
func deviceInquiryComplete(sender: IOBluetoothDeviceInquiry, error: IOReturn, aborted: Bool) {
aborted
var devices = sender.foundDevices()
for device : AnyObject in devices {
if let thingy = device as? IOBluetoothDevice {
thingy.getAddress()
}
}
}
}
var inquiry = IOBluetoothDeviceInquiry(delegate: BlueDelegate())
inquiry.start()
I'm just getting started with Bluetooth under OSX, and all I currently would like is a list of devices in range.
It doesn't seem to be calling my delegate method at all.
I'm new to OSX development and Swift, so be gentle. :)
To find an active Bluetooth device, first make sure you have Bluetooth enabled on your smartphone. Next, download Wunderfind for your iPhone or Android device and launch the app. Immediately, you'll see a list of Bluetooth devices that your smartphone has detected using its built-in Bluetooth radio.
Pairing information can be registered for up to eight Bluetooth wireless devices.
To connect your mobile phone to the Suzuki Swift's Bluetooth, you must have the vehicle stationary and your phone's Bluetooth switched on and set to Discoverable. The next step is to press the Setup button on the car's media-control panel.
In Bluetooth terms, device discovery is known as inquiry. There are two types of inquiry: general and limited. Devices respond to inquiry requests according to their discoverable mode.
To tell a Playground that your code does something in the background, you have to import XCPlayground
and call XCPSetExecutionShouldContinueIndefinitely()
.
This will keep the IOBluetoothDeviceInquiry alive in the Playground and allow it to call the delegate method when finished.
import Cocoa
import IOBluetooth
import XCPlayground
class BlueDelegate : IOBluetoothDeviceInquiryDelegate {
func deviceInquiryComplete(sender: IOBluetoothDeviceInquiry, error: IOReturn, aborted: Bool) {
aborted
println("called")
var devices = sender.foundDevices()
for device : AnyObject in devices {
if let thingy = device as? IOBluetoothDevice {
thingy.getAddress()
}
}
}
}
var delegate = BlueDelegate()
var inquiry = IOBluetoothDeviceInquiry(delegate: delegate)
inquiry.start()
XCPSetExecutionShouldContinueIndefinitely()
While the above approach works, I find it easier to create simple, traditional test projects for tasks that need concepts like async-code, delegation, ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With