eChung00
eChung00

Reputation: 633

Can bluetooth trigger a certain action in an app?

I am planning to start an ios application and was wondering if there is a way to trigger certain action such as finding location using google map on bluetooth connection (or disconnection)?

I know an apple map application has similar feature where it shows where your car is parked. Any references would be appreiciate.

Thanks.

Upvotes: 0

Views: 388

Answers (2)

Gihan
Gihan

Reputation: 2536

Well there delegate methods which get fired after different states of connection. The best way I suggest you to do is implement the following delegate methods.

func centralManager(_ central: CBCentralManager, 
                  didConnect peripheral: CBPeripheral) {

}

func centralManager(_ central: CBCentralManager, 
     didDisconnectPeripheral peripheral: CBPeripheral, 
                       error: Error?) {
}

And call a function to get your location in these cases. Additionally there are other delegate methods such as

func centralManagerDidUpdateState(_ central: CBCentralManager) { 
    }
func centralManager(_ central: CBCentralManager, 
            didFailToConnect peripheral: CBPeripheral, 
                       error: Error?) {
}

But these cases does not cover the all the cases of connection and disconnection. But depending on your requirement you can implement either one. These are CBCentralManagerDelegate delegates.

CBCentralManager

CBCentralManagerDelegate

Upvotes: 0

Agent Smith
Agent Smith

Reputation: 2923

You can try this in your viewController where you want to check:

var manager: CBCentralManager!

 override func viewDidLoad(){
     super.viewDidLoad()
     manager = CBCentralManager()
     manager.delegate = self
 }

//CBCentralManager Delegate method

func centralManagerDidUpdateState(_ central: CBCentralManager) {

  switch central.state {
       case .poweredOn:
           //poweredOn
       case .resetting:
           //resetting
       case .unauthorized:
           //unauthorized
       case .unknown:
           //unknown
       case .unsupported:
           //UnSupported
       case .poweredOff:
          //Bluetooth is not Connected.Please Enable it
  }

the status in delegate will tell you what's the status of your Bluetooth.

Hope it helps!

Upvotes: 1

Related Questions