Check bluetooth state in iOS

2019-08-02 18:49发布

I have an application (I am not going to submit this app to apple app store) using which I want to check whether bluetooth is turned on. If it is turned on then I have to display an alert.

    - (void)centralManagerDidUpdateState:(CBCentralManager *)central{
  switch (central.state) {
    case CBCentralManagerStatePoweredOn:{
      //alert view
      break;
    }
  }

And in viewdidload I did like this

  CBCentralManager * manager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];

but this is not working in ipad2 with ios 5.1.

the problem is central.state is always null.

I want the same scenario to work from ios 3.0 to ios 6 beta. Is there any common code for checking bluetooth state.

Any possible code is welcome, even code with private api.

3条回答
可以哭但决不认输i
2楼-- · 2019-08-02 19:01

I´m also not getting CBCentralManagerStateUnsupported on an iPhone 4 running iOS7. I opened a bug ticket with Apple.

http://openradar.appspot.com/15564675 is the radar

and https://github.com/deadfalkon/btLeState the repository

查看更多
兄弟一词,经得起流年.
3楼-- · 2019-08-02 19:19

CBCentralManager is for using Bluetooth Smart (the Low Energy part in Bluetooth 4.0). This is a new technology only recently introduced in iOS / OS X devices. Current support is in iPhone 4s and the new iPad. The iPad 2 does NOT have support for this technology. Also the CBCentralManager is only available from iOS 5 and up.

If you want to check traditional Bluetooth state you will have to find another way to do that.

In your case central.state should actually equal CBCentralManagerStateUnsupported.

查看更多
小情绪 Triste *
4楼-- · 2019-08-02 19:22

You want to look at the BluetoothManager API. The BluetoothManager.framework is the private framework that this API lives in. You can link to it from within your Xcode project, or use dlopen to open it ("/System/Library/PrivateFrameworks/BluetoothManager.framework/BluetoothManager") dynamically.

The important calls would be

- (BOOL)powered;
- (BOOL)enabled;
- (BOOL)setPowered:(BOOL)arg1;
- (BOOL)setEnabled:(BOOL)arg1;

To get an instance of the BluetoothManager, use this:

BluetoothManager* mgr = [BluetoothManager sharedInstance];
查看更多
登录 后发表回答