iOS Bluetooth delegate connect function not being called

pimlu

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.

Penkey Suresh

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Delegate function not being called iOS Swift

Delegate function from protocol not being called

My delegate function is not being called in Swift

Delegate method is not being called

Swift Delegate Not Being Called

Class delegate method is not being called

NSObject Delegate Methods are not being called

iOS delegate method not called

Delegate method not being called or not firing when called

assigned delegate, but function is not called

Delegate function not called in AppDelegate

IOS Parses PFLoginViewController didLoginUser Delegate is not being called when logging in via Facebook, why?

My delegate function IS being called, still my UI does not update accordingly

custom delegate is not getting called in IOS

Why delegate method is not called - iOS

numberOfSections being called? iOS

Delegate didSelectRowAt Not being called for some cells

Groovy Delegate being called instead of Owner Class

messageComposeViewController didFinishWithResult delegate method not being called

UITableViewController delegate methods being called prior to viewDidLoad()

UICollectionView not showing & delegate/datasource methods not being called

significant change location delegate methods not being called

Swift protocol method is not being called by the delegate

UITableView delegate methods being called before viewDidLoad()

UITableView delegate method not being called in swift 3

Why my delegate methods are not being called?

Delegate method is not being called and giving a nil value

Mousemove function not being called

Function being called but not executing