Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List devices that are in range of Bluetooth device in Swift

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. :)

like image 874
Xenph Yan Avatar asked Jun 20 '14 06:06

Xenph Yan


People also ask

How do I know which Bluetooth devices are active?

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.

What is the maximum number of devices allowed to Bluetooth device?

Pairing information can be registered for up to eight Bluetooth wireless devices.

How do I add a Bluetooth device to Swift?

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.

What is the procedure that handles Bluetooth device discovery called?

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.


1 Answers

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, ...

like image 130
Thomas Zoechling Avatar answered Nov 10 '22 01:11

Thomas Zoechling