Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS Bluetooth delegate connect function not being called

I'm trying to listen to all Bluetooth connect/disconnect events. Even though the delegate's centralManagerDidUpdateState function is called, nothing happens when I connect or disconnect Bluetooth devices.

I'm confused as to what's going wrong. I initialize the Central Manager/delegate like this:

var btDelegate: CBCentralManagerDelegate = BluetoothDelegate()
var btManager = CBCentralManager(delegate: btDelegate, queue: nil)

BluetoothDelegate is defined like so:

import Foundation
import CoreBluetooth

class BluetoothDelegate : NSObject, CBCentralManagerDelegate {

    func centralManager(central: CBCentralManager!, didConnectPeripheral peripheral: CBPeripheral!) {
        println("connect") //this line is not called
    }
    func centralManager(central: CBCentralManager!, didDisconnectPeripheral peripheral: CBPeripheral!, error: NSError!) {
        println("disconnect") //this line is not called
    }

    func centralManagerDidUpdateState(central: CBCentralManager!) {
        println("state update") //this line is called
    }
}

Note: I can continuously receive more state update events such as when I toggle Bluetooth, even though connect and disconnect are not called.

like image 682
pimlu Avatar asked May 20 '15 17:05

pimlu


1 Answers

From your code it looks like you haven't started scanning for peripherals. Once you have confirmed that your central is in powered on state from centralManagerDidUpdateState method you should start scanning for your peripherals.

(The bluetooth devices you connected from bluetooth settings panel are irrelevant. You can't have access to them. (as far as I know) Incase you want to scan and find out your device on your own below code will help)

func centralManagerDidUpdateState(central: CBCentralManager!) {
  switch (central.state) {
  case CBCentralManagerState.PoweredOff:
    break
  case CBCentralManagerState.PoweredOn:
    startScan() // start scanning once the bluetooth is On
    break
  default:
    break
  }
}

And your startScan method will be (You can provide service UUID, use nil if you want all )

func startScan(){
  if let central = btManager {
    central.scanForPeripheralsWithServices(nil, options: nil)
    println("started Scanning")
  }
}

After that whenever you discover a peripheral didDiscoverPeripheral method will be called first.

func centralManager(central: CBCentralManager!, didDiscoverPeripheral peripheral: CBPeripheral!, advertisementData: [NSObject : AnyObject]!, RSSI: NSNumber!) {
  println(peripheral)
  // btManager.connectPeripheral(peripheral, options: nil)
}

From there you collect the peripheral and then invoke connectPeripheral method of CBCentralManager. If the connection is successful didConnectPeripheral method will be called.

You should go through this documentation for complete details

like image 135
Penkey Suresh Avatar answered Oct 13 '22 04:10

Penkey Suresh