I'm writing an app that interfaces with Bluetooth, and for OO reasons I need to be able to notify 1 to many objects when Bluetooth events happen. I've got a custom Model object:
class BluetoothModel: NSObject, CBCentralManagerDelegate {
var cBCentralManager: CBCentralManager!
var peripherals = [CBPeripheral]()
var count = 0
// MARK: - Singleton Definition
static let instance = BluetoothModel()
private override init() {
super.init()
cBCentralManager = CBCentralManager(delegate: self, queue: dispatch_get_global_queue(QOS_CLASS_UTILITY, 0))
}
// MARK: - CBCentralManagerDelegate
func centralManagerDidUpdateState(central: CBCentralManager) {
cBCentralManager.scanForPeripheralsWithServices(nil, options: nil)
}
func centralManager(central: CBCentralManager, didDiscoverPeripheral peripheral: CBPeripheral, advertisementData: [String : AnyObject], RSSI: NSNumber) {
if !peripherals.contains(peripheral) {
peripherals.append(peripheral)
count++
}
}
...
}
As well as the Controller I'm trying to hook up with KVO:
class MasterViewController: UITableViewController, CBCentralManagerDelegate {
...
override func viewDidLoad() {
super.viewDidLoad()
...
BluetoothModel.instance.addObserver(self, forKeyPath: "count", options: .New, context: nil)
}
...
override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
print("Change!")
}
...
}
I've confirmed that count is being incremented repeatedly, even after the view controller loads and registers as observer. I'm pretty sure no notifications are firing, but I don't know why. Do I need to add something to enable KVO in a custom NSObject? I'm using the latest iOS9 and Swift2.
Make sure the count property is declared with the dynamic keyword. If it doesn't solve your problem, check this out.
Related
WorkAround:
I am creating a utility class let's say BLEScanManager, which is responsible to scan nearby BLE devices. The only job of the utility class is to scan BLE devices and make a list of it.
The other classes can create an object of this BLEScanManager and get an array of BLE devices, like [Bluetooth] (here Bluetooth is a custom modal class).
Now to scan BLE devices, I have created the extension of BLEScanManager in the same class and override its delegate methods like below:
import UIKit
import CoreBluetooth
protocol BLEScanDelegate {
func reloadDeviceList()
}
internal class BLEScanManager: NSObject {
private var centralManager: CBCentralManager?
var devices : [Bluetooth] = []
var delegate: BLEScanDelegate?
override init() {
super.init()
centralManager = CBCentralManager(delegate: self, queue: .main)
}
// MARK:- Custom methods
func isScanning() -> Bool {
return centralManager?.isScanning ?? false
}
func stopScanning() {
centralManager?.stopScan()
}
func startScanning() {
devices.removeAll()
let options: [String: Any] = [CBCentralManagerScanOptionAllowDuplicatesKey:
NSNumber(value: false)]
centralManager?.scanForPeripherals(withServices: nil, options: options)
}
}
extension BLEScanManager : CBCentralManagerDelegate {
func centralManagerDidUpdateState(_ central: CBCentralManager) {
switch central.state {
case .unknown: print("central.state is .unknown")
case .resetting: print("central.state is .resetting")
case .unsupported: print("central.state is .unsupported")
case .unauthorized: print("central.state is .unauthorized")
case .poweredOff: print("central.state is .poweredOff")
case .poweredOn: print("central.state is .poweredOn")
self.startScanning()
#unknown default: fatalError("unknow state")
}
}
func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
print("Discovered \(peripheral)")
var bluetooth = Bluetooth()
bluetooth.name = peripheral.name
bluetooth.identifier = peripheral.identifier.uuidString
bluetooth.state = peripheral.state.rawValue
bluetooth.advertisementData = advertisementData
let power = advertisementData[CBAdvertisementDataTxPowerLevelKey] as? Double
let value: Double = pow(10, ((power ?? 0 - Double(truncating: RSSI))/20))
bluetooth.signalStrength = String(describing: value.round)
// Do not add duplicate device
let fitlerArray : [Bluetooth] = devices.filter { $0.identifier == bluetooth.identifier}
if fitlerArray.count == 0 {
devices.append(bluetooth)
}
self.delegate?.reloadDeviceList()
}
}
The thing here is these methods are exposed in the other classes too.
For example:
I have created an object of BLEScanManager in other class BLEListViewController to show a list of BLE device in UITableView.
class BLEListViewController: UITableViewController {
var scanManager: BLEScanManager!
}
I can access CBCentralManagerDelegate delegate methods in BLEListViewController class using an object scanManager.
Like below,
self.scanManager.centralManager?(<#T##central: CBCentralManager##CBCentralManager#>, didConnect: <#T##CBPeripheral#>)
This should expose the internal utility delegate methods to the outside world.
Question is how to stop exposing these delegates?
Please note that, if I use internal keyword it only hides that specific method. But it still allows accessing all other CBCentralManagerDelegate methods.
There’s nothing you can do about this. It is a problem with all optional protocol methods. The trouble is that optional protocol methods are an Objective C feature. You cannot hide such a method as private, because now Objective C cannot see it and so the method will never be discovered and called. It’s a flaw in the way Swift interfaces with Objective C. You just have to live with it.
What you need to do, is to hide behind a protocol - see the following example.
protocol Exposed {
func run()
}
struct Bluetooth {
func hidden() {
print("This method is hidden - ish")
}
}
extension Bluetooth: Exposed {
func run() {
print("this method is exposed")
}
}
struct BluetoothScanManager {
static func scan() -> [Exposed] {
return [Bluetooth(), Bluetooth()]
}
}
Now, when you run BluetoothScanManager you will get a list of exposed only with the run() method available, but behind the code it is a Bluetooth
for exposed in BluetoothScanManager.scan() {
exposed.run()
exposed.hidden() // not possible
}
Your only way to do this (which also is the correct way in my opinion) is to create a new private object inside the BLEScanManager class that implements the CBCentralManagerDelegate.
Than you can pass this object to the CBCentralManager init method.
If you then need to comunicate from this object to the BLEScanManager you can simply use another delegate with a private protocol so it doesn't show those methods outside of the class.
It would look something like this:
import UIKit
import CoreBluetooth
protocol BLEScanDelegate {
func reloadDeviceList()
}
internal class BLEScanManager: NSObject {
private var centralManager: CBCentralManager?
var devices : [Bluetooth] = []
var delegate: BLEScanDelegate?
let centralManagerDelegate = MyCentralManagerDelegateImplementation()
override init() {
super.init()
centralManagerDelegate.delegate = self
centralManager = CBCentralManager(delegate: centralManagerDelegate, queue: .main)
}
// MARK:- Custom methods
func isScanning() -> Bool {
return centralManager?.isScanning ?? false
}
func stopScanning() {
centralManager?.stopScan()
}
func startScanning() {
devices.removeAll()
let options: [String: Any] = [CBCentralManagerScanOptionAllowDuplicatesKey:
NSNumber(value: false)]
centralManager?.scanForPeripherals(withServices: nil, options: options)
}
}
private extension BLEScanManager: CentralManagerEventsDelegate {
func didUpdateState(){
// Here goes the switch with the startScanning
}
func didDiscoverPeripheral(...) {
// Here goes the logic on new peripheral and the call on self.delegate?.reloadDeviceList
}
}
internal protocol CentralManagerEventsDelegate: class {
func didUpdateState()
func didDiscoverPeripheral(...)
}
internal class MyCentralManagerDelegateImplementation: NSObject, CBCentralManagerDelegate {
weak var delegate: CentralManagerEventsDelegate?
func centralManagerDidUpdateState(_ central: CBCentralManager) {
self.delegate?.didUpdateState()
}
func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
self.delegate?.didDiscoverPeripheral(actualParams)
}
}
So I currently got a bluetooth connection setup between a iPad and iPhone. I've created my testcode in the ViewController and everything works fine. Now I moved it to 2 manager classes one for the CBCentralManager and one for the CBPeripheralManager above those to classes I made a BluetoothManager which is a singleton class and holds some information regarding currently connected devices.
However when doing this I'm facing a problem it seems like the centralManager.connect() call doesn't actually work. I debugged my entire code and after that line nothing seems to happen and I can't seem to figure out why this is or where I'm actually going wrong.
The CentralManager class
import Foundation
import CoreBluetooth
class CentralManager: NSObject {
private var centralManager: CBCentralManager!
var peripherals: [CBPeripheral] = []
override init() {
super.init()
centralManager = CBCentralManager(delegate: self, queue: DispatchQueue.main)
}
}
// MARK: - CBCentralManager Delegate Methods
extension CentralManager: CBCentralManagerDelegate {
func centralManagerDidUpdateState(_ central: CBCentralManager) {
switch central.state {
case .poweredOn:
centralManager.scanForPeripherals(withServices: [BLEConstants.serviceUUID], options: [CBCentralManagerScanOptionAllowDuplicatesKey: true])
default:
break
}
}
func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
if !peripherals.contains(peripheral) {
peripheral.delegate = self
peripherals.append(peripheral)
centralManager.connect(peripheral, options: nil)
}
}
func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
peripheral.discoverServices([BLEConstants.serviceUUID])
}
func centralManager(_ central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: Error?) {
guard let peripheralIndex = peripherals.index(of: peripheral), BluetoothManager.shared.deviceCharacteristic[peripheral] != nil else { return }
peripherals.remove(at: peripheralIndex)
BluetoothManager.shared.deviceCharacteristic.removeValue(forKey: peripheral)
}
}
// MARK: - CBPeripheral Delegate Methods
extension CentralManager: CBPeripheralDelegate {
func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {
for service in peripheral.services! {
if service.uuid == BLEConstants.serviceUUID {
peripheral.discoverCharacteristics(nil, for: service)
}
}
}
func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {
for characteristic in service.characteristics! {
let characteristic = characteristic as CBCharacteristic
if BluetoothManager.shared.deviceCharacteristic[peripheral] == nil {
BluetoothManager.shared.deviceCharacteristic[peripheral] = characteristic
}
}
}
func peripheral(_ peripheral: CBPeripheral, didModifyServices invalidatedServices: [CBService]) {
}
}
The PeripheralManager class
class PeripheralManager: NSObject {
private var peripheralManager: CBPeripheralManager!
override init() {
super.init()
peripheralManager = CBPeripheralManager(delegate: self, queue: nil)
}
}
// MARK: - Manage Methods
extension PeripheralManager {
func updateAdvertising() {
guard !peripheralManager.isAdvertising else { peripheralManager.stopAdvertising(); return }
let advertisingData: [String: Any] = [CBAdvertisementDataServiceUUIDsKey: BLEConstants.serviceUUID,
CBAdvertisementDataLocalNameKey: BLEConstants.bleAdvertisementKey]
peripheralManager.startAdvertising(advertisingData)
}
func initializeService() {
let service = CBMutableService(type: BLEConstants.serviceUUID, primary: true)
let characteristic = CBMutableCharacteristic(type: BLEConstants.charUUID, properties: BLEConstants.charProperties, value: nil, permissions: BLEConstants.charPermissions)
service.characteristics = [characteristic]
peripheralManager.add(service)
}
}
// MARK: - CBPeripheralManager Delegate Methods
extension PeripheralManager: CBPeripheralManagerDelegate {
func peripheralManagerDidUpdateState(_ peripheral: CBPeripheralManager) {
if peripheral.state == .poweredOn {
initializeService()
updateAdvertising()
}
}
func peripheralManager(_ peripheral: CBPeripheralManager, didReceiveWrite requests: [CBATTRequest]) {
for request in requests {
if let value = request.value {
let messageText = String(data: value, encoding: String.Encoding.utf8)
print(messageText ?? "")
}
self.peripheralManager.respond(to: request, withResult: .success)
}
}
}
The BluetoothManager class
class BluetoothManager {
static let shared = BluetoothManager()
private var centralManager: CentralManager!
private var peripheralManager: PeripheralManager!
var deviceCharacteristic: [CBPeripheral: CBCharacteristic] = [:]
var connectedPeripherals: [CBPeripheral] { return centralManager.peripherals }
func setup() {
centralManager = CentralManager()
peripheralManager = PeripheralManager()
}
}
and then in my ViewController didLoad I'm calling BluetoothManager.shared.setup()
Does anyone know why the devices don't seem to connect with eachother or maybe the delegate functions after that just don't get called?
The process starts when the static shared variable is initialized with BluetoothManager(). I am not sure when this happens in Swift, it is either at the very start of the program, or when you use BluetoothManager.setup for the first time.
Initialization of the variable calls the init() method of the BluetoothManager. This will instantiate a CentralManager, and its init() method will be called. This will instantiate a CBCentralManager, which will start the Bluetooth process.
Then you call setup(), which will instantiate a new CentralManager, with its own CBCentralManager. I can imagine something goes wrong with two CBCentralManager.
To solve it, don't use setup(), but initialize the variables in init() instead.
To debug this kind of situation, put breakpoints in all init() methods. Create destructors, and put breakpoints in those as well. Technically, you need destructors anyways, because you need to remove yourself as delegate from the CBCentralManager object.
Note also that you only call scanForPeripherals from centralManagerDidUpdateState. The CBCentralManager can already be in poweredOn state when it starts, this can happen when another app is using Bluetooth at the same time - or when your first CBCentralManager object has already started it. In that case, centralManagerDidUpdateState will never be called.
Are you sure your Singleton is correctly initialized?
Try this:
import Foundation
private let singleton = Singleton()
class Singleton {
static let sharedInstance : Singleton = {
return singleton
}()
let cnetralManager = = CBCentralManager(delegate: self, queue: DispatchQueue.main)
}
It has been now several years that Microchip has released the RN4020 BT LE chip with the private MLDP profile. However, as of yet there is STILL NO PUBLICLY AVAILABLE iOS sample source code available, despite them having an iOS App in the Apple App Store. Has anyone any working code and willing to share/post it?
Thanks!
Tim
I have some working code. I'll give some snippets here. In a first ViewController which conforms to the CBCentralManagerDelegate we have:
var cbc : CBCentralManager? = nil
override func viewDidLoad() {
super.viewDidLoad()
cbc = CBCentralManager(delegate: self, queue: nil)
}
touching a button starts scanning for peripherals
#IBAction func scan(_ sender: Any) {
cbc?.scanForPeripherals(withServices: nil, options: nil)
}
for each peripheral found the following delegate member will be called
func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
// store peripherals here to let select user one
NSLog("name=%#", peripheral.name ?? "unnamed")
}
we store the peripherals in a dictionary and present its names to the user using a table view. If the user selects a peripheral we try to connect to that
#IBAction func connect(_ sender: Any) {
// selectedPeripheral set by selection from the table view
cbc?.connect(selectedPeripheral!, options: nil)
}
successfull connection will result in a call to the following delegate method:
func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
performSegue(withIdentifier: "ConnectPeriph", sender: self)
}
which leads to a second ViewController responsible for the connected state. This ViewController conforms to the CBPeripheralDelegate protocol and declares the following variables:
var periph : CBPeripheral! // selected peripheral
var dataChar : CBCharacteristic? // characteristic for data transfer
let mchpPrivateService : CBUUID = CBUUID(string: "00035B03-58E6-07DD-021A-08123A000300")
let mchpDataPrivateChar : CBUUID = CBUUID(string: "00035B03-58E6-07DD-021A-08123A000301")
first action after connection is to discover the services which the peripheral is offering:
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
periph.delegate = self
periph.discoverServices(nil)
}
this results in calls to this delegate method:
func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {
if let e = error {
NSLog("Error %#", e.localizedDescription)
}
else if let services = peripheral.services {
for s in services {
NSLog("Service=%#", s.uuid.uuidString)
if s.uuid.isEqual(mchpPrivateService) {
peripheral.discoverCharacteristics(nil, for: s)
}
}
}
}
which in turn results in discovering characteristics:
func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {
NSLog("characteristics for service %#", service.uuid.uuidString)
if let characteristics = service.characteristics {
for c in characteristics {
if c.uuid.isEqual(mchpDataPrivateChar) {
peripheral.setNotifyValue(true, for: c)
dataChar = c
}
}
}
}
the only characteristic we are interested in is that with the uuid mchpDataPrivateChar. The request for notification results in calls to:
func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {
NSLog("update value for %#", characteristic.uuid)
if let d = characteristic.value {
var s : String = String()
for b in d {
s.append(Character(UnicodeScalar(b)))
}
NSLog("received \(d.count) bytes: \(s)")
}
}
which completes the receiver on the iOS side. Sending bytes is done via:
#IBAction func sendClicked(_ sender: Any) {
if let d = dataChar, let s=sendEdit.text {
let buffer : [UInt8] = Array(s.utf8)
let data : Data = Data(buffer)
periph.writeValue(data, for: d, type: .withResponse)
}
}
I want to create a method startCentralManager that waits until the delegate method centralManagerDidUpdateState is called and returns a boolean value depending on the central state.
I want to use the method like this:
let centralManager = CentralManager()
let ready = centralManager.startCentralManager()
The CentralManager class should look something like this
class CentralManager: NSObject, CBCentralManagerDelegate {
var centralManager: CBCentralManager?
func startCentralManager() -> Bool {
centralManager = CBCentralManager.init(delegate: self, queue: nil)
}
func centralManagerDidUpdateState(central: CBCentralManager) {
// startCentralManager() should return this
return central.state == CBCentralManagerState.PoweredOn
}
}
How can I solve this?
Thank you very much, in advance.
UPDATE:
There are some other methods, but the situation is almost equal. Another method is the following:
let connected = centralManager.connect<PARAMS>)
In this method if have to call
func scanForPeripheralsWithServices(_ serviceUUIDs: [CBUUID]?, options options: [String : AnyObject]?)
and wait until the delegate method
func centralManager(_ central: CBCentralManager, didDiscoverPeripheral peripheral: CBPeripheral, advertisementData advertisementData: [String : AnyObject], RSSI RSSI: NSNumber)
is called. Here, I have to call
func connectPeripheral(_ peripheral: CBPeripheral, options options: [String : AnyObject]?)
and again wait until the delegate method
func centralManager(_ central: CBCentralManager, didConnectPeripheral peripheral: CBPeripheral)
is called. In case of success, the call to centralManager.connect(<PARAMS>) should return true.
CBCentralManager works asychronously and you get informed of its status through a call to your delegate at some point in time that you have no direct control over.
You can't wait for asynchronous response between two lines of code. You'll have to design your program differently.
If you have a view controller that depends on the readiness of the CBCentralManager, you can make it the delegate and enable its peripheral related controls when your centralManagerDidUpdateState function is called.
for example (if you're using this for a scanner) :
class ScanningViewController:UIViewController,CBCentralManagerDelegate
{
var centralManager: CBCentralManager? = nil
override viewDidLoad()
{
centralManager = CBCentralManager.init(delegate: self, queue: nil)
}
func centralManagerDidUpdateState(central: CBCentralManager)
{
if central.state == CBCentralManagerState.PoweredOn
{
// enable scanning controls here
}
}
}
I found a solution:
class CentralManager: NSObject, CBCentralManagerDelegate {
var centralManager: CBCentralManager?
var ready: Bool = false
func startCentralManager() -> Bool {
centralManager = CBCentralManager.init(delegate: self, queue: nil)
ready = false
while ready == false {
NSRunLoop.currentRunLoop().runMode(NSDefaultRunLoopMode, beforeDate: NSDate.distantFuture())
}
return ready
}
func centralManagerDidUpdateState(central: CBCentralManager) {
ready = central.state == CBCentralManagerState.PoweredOn
}
}
Is this a good programming practice? Is there a better solution?
Thank you.
Create a timer that fires every X seconds
Inside the timer's selector check if connected and invalidate the timer or let it keep checking if not connected
Or consider using a semaphore and a dispatch_wait_forever
I am trying to advertise BLE with the following
var perMan: CBPeripheralManager!
let myCustomServiceUUID: CBUUID = CBUUID(string: "109F17E4-EF68-43FC-957D-502BB0EFCF46")
var myService: CBMutableService!
override func viewDidLoad() {
super.viewDidLoad()
perMan = CBPeripheralManager(delegate: self, queue: nil)
myService = CBMutableService(type: myCustomServiceUUID, primary: true)
perMan.addService(myService)
}
// Broadcast when Bluetooth is on
func peripheralManagerDidUpdateState(peripheral: CBPeripheralManager!) {
if peripheral.state == CBPeripheralManagerState.PoweredOn {
// Start advertising over BLE
self.perMan.startAdvertising([CBAdvertisementDataServiceUUIDsKey: [myService.UUID]])
println("Adertising")
} else if peripheral.state == CBPeripheralManagerState.PoweredOff {
self.perMan.stopAdvertising()
}
}
And want to discover the peripheral with
import UIKit
import CoreBluetooth
class SearchViewController: UIViewController, CBCentralManagerDelegate {
var centralManager: CBCentralManager!
#IBOutlet weak var beaconsTableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
centralManager = CBCentralManager(delegate: self, queue: nil)
// Listen to BLE of IPhones
let services: NSArray = ["7521105F-8937-48B7-A875-66E6FE21D714"]
centralManager.scanForPeripheralsWithServices(nil, options: nil)
}
// Found IPhone
func centralManager(central: CBCentralManager!, didDiscoverPeripheral peripheral: CBPeripheral!, advertisementData: [NSObject : AnyObject]!, RSSI: NSNumber!) {
println("Hallo:")
println(RSSI)
}
// CBCentralManagerDelegate
func centralManagerDidUpdateState(central: CBCentralManager!) {
}
However the didDiscoverPeripheral method is never called. I am testing on two IPhone5.
Can anyone tell me what I am doing wrong?