user1255603
user1255603

Reputation: 567

Can't get Core Bluetooth to work in Swift iOS playground

I am new to swift. I am not able to get a callback to centralManagerDidUpdateState:: w/ following in playground (i.e.: i thought initialization would call back into centralManagerDidUpdateState):

import CoreBluetooth
class BTDiscovery:NSObject,
CBCentralManagerDelegate {

    func centralManagerDidUpdateState(central: CBCentralManager!) {
        println("here")
    }
}

var bt = BTDiscovery()

Is Core Bluetooth supporting in the iOS Swift playground? I tried this for OSX playground and IOBluetooth. This also didn't work. What am I doing wrong?

Thank you.

Upvotes: 0

Views: 1681

Answers (4)

BilalReffas
BilalReffas

Reputation: 8328

It's possible to use Bluetooth in Swift Playground. Please note you have to use PlaygroundBluetooth. You can find more informations about it here Documentation Framework PlaygroundBluetooth

This is how you can scan using the central.

let managerDelegate: PlaygroundBluetoothCentralManagerDelegate = <# manager delegate instance #>

let manager = PlaygroundBluetoothCentralManager(services: nil)
manager.delegate = managerDelegate

Please note PlaygroundBluetooth is just a subset of CoreBluetooth therefore you can not do everything which is normally possible in CoreBluetooth, but I think it's still great for simple things to have fun with.

Upvotes: 0

BilalReffas
BilalReffas

Reputation: 8328

Either XCPSetExecutionShouldContinueIndefinitely nor PlaygroundPage.current.needsIndefiniteExecution will work.

Please keep in mind CoreBluetooth only works on device. Which means currently it won't work on the Playground.

Upvotes: 0

Paulw11
Paulw11

Reputation: 114992

You can only use Core Bluetooth on an actual iOS device; it is not supported in the simulator, and by extension, it is not supported in a Playground as this is also executing on your Mac rather than on an iOS device.

Upvotes: 2

David Berry
David Berry

Reputation: 41236

I think what you're running into is the playground is inherently synchronous while the BlueTooth discovery is asynchronous. To allow it to work you need to add some things to your playground to allow asynchronous operation:

import XCPlayground
XCPSetExecutionShouldContinueIndefinitely(continueIndefinitely: true)

Also note that since the iOS playground is run on the simulator, I wouldn't necessarily expect CB to work there at all.

You also have more fundamental problems in that you're not doing anything to actually trigger discovery. You need to create an instance of CBCentralManager and use it to drive the discovery process:

import Cocoa
import XCPlayground
import CoreBluetooth

class BTDiscovery:NSObject, CBCentralManagerDelegate {

    func centralManagerDidUpdateState(central: CBCentralManager!) {
        println("here")
    }

}

var bt = BTDiscovery()
var central = CBCentralManager(delegate: bt, queue: dispatch_get_main_queue())

XCPSetExecutionShouldContinueIndefinitely(continueIndefinitely: true)

Upvotes: 2

Related Questions