BluetoothLE Enable ?

Hi

 

I have a project to work with BluetoothLE. I try to run the sample code from the ‘adafruit/Basic-Chat/BLECentralViewController.swift’ from github.

I put the break point ‘if central.state == CBManagerState.poweredOn’ to this statement. It always said that ‘Bluetooth Disabled- Make sure your Bluetooth is turned on’.

 

System Preference/Bluetooth is ‘ON’ and I use BT wireless keyboard and mouse.

Mac Mini (Late 2014) OS: 10.13.3

 

When I debug with XCode, I try to check the status of CBCentralManagerState. Result is CBCentralManagerStateUnknown. 

How can I enable the Bluetooth?

 

Thank you for your assistance and help.

 

Dko

 

note: I also try the code from Apple Developer ‘https://developer.apple.com/library/content/samplecode/HeartRateMonitor/‘.

The result is almost the same. ‘Bluetooth is not turn on..’,  to CBCentralManagerStateUnknown

 

switch ([manager state])

{   case CBCentralManagerStateUnsupported:

       state = @”The platform/hardware doesn’t support Bluetooth Low Energy.”;

       break;

   case CBCentralManagerStateUnauthorized:

       state = @”The app is not authorized to use Bluetooth Low Energy.”;

       break;

   case CBCentralManagerStatePoweredOff:

       state = @”Bluetooth is currently powered off.”;

       break;

   case CBCentralManagerStatePoweredOn:

       return TRUE;

   case CBCentralManagerStateUnknown:

   default:

       return FALSE;

}

 

The following is the code from ‘adafruit/Basic-Chat/BLECentralViewController.swift’ from github’.

/* Invoked when the central manager’s state is updated.

This is where we kick off the scan if Bluetooth is turned on.

*/

func centralManagerDidUpdateState(_ central: CBCentralManager) {

   if central.state == CBManagerState.poweredOn {

       // We will just handle it the easy way here: if Bluetooth is on, proceed…start scan!

       print(“Bluetooth Enabled”)

       startScan()

         } else {

       //If Bluetooth is off, display a UI alert message saying “Bluetooth is not enable” and “Make sure that your bluetooth is turned on”

       print(“Bluetooth Disabled- Make sure your Bluetooth is turned on”)

       let alertVC = UIAlertController(title: “Bluetooth is not enabled”, message: “Make sure that your bluetooth is turned on”, preferredStyle: UIAlertControllerStyle.alert)

       let action = UIAlertAction(title: “ok”, style: UIAlertActionStyle.default, handler: { (action: UIAlertAction) -> Void in

           self.dismiss(animated: true, completion: nil)

       })

       alertVC.addAction(action)

       self.present(alertVC, animated: true, completion: nil)

       }

    }

Powered by WPeMatico

About

You may also like...

Comments are closed.