List devices that are in range of Bluetooth device in Swift

前端 未结 1 514
南笙
南笙 2020-12-10 06:27

I have the following code in a Xcode 6 playground:

import Cocoa
import IOBluetooth

class BlueDelegate : IOBluetoothDeviceInquiryDelegate {
    func deviceIn         


        
相关标签:
1条回答
  • 2020-12-10 06:40

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

    0 讨论(0)
提交回复
热议问题