如何以编程方式获取iphone中蓝牙(ON / OFF)的状态 [英] How to get the status of bluetooth (ON/OFF) in iphone programmatically

查看:182
本文介绍了如何以编程方式获取iphone中蓝牙(ON / OFF)的状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图以编程方式获取iPhone / iPod蓝牙的状态,无论是开启还是关闭。
是否可以使用一些Apple API或第三方API。

I trying to get the Status of iPhone/iPod Bluetooth that whether it is ON or OFF programmatically. Is it possible using some Apple API or third party API.

推荐答案

Sam的答案,我以为我会分享
您可以在不使用私有API的情况下这样做,但有几点需要注意:

A little bit of research into Sam's answer that I thought I'd share You can do so without utilizing private API, but with a few caveats:


  • 它只适用于iOS 5.0+

  • 它只适用于$ b $的设备b支持蓝牙LE规格(iPhone 4S +,第5代iPod +,iPad
    第3代+)

  • 简单分配该类将导致您的应用程序请求使用蓝牙堆栈的权限用户(可能不需要),如果他们拒绝,你唯一能看到的就是CBCentralManagerStateUnauthorized

  • 蓝牙状态的检索是异步的,也是连续的。您需要设置一个委托以获取状态更改,因为检查新分配的蓝牙管理器的状态将返回CBCentralManagerStateUnknown

  • It will only work on iOS 5.0+
  • It will only work on devices that support the bluetooth LE spec (iPhone 4S+, 5th Generation iPod+, iPad 3rd Generation+)
  • Simply allocating the class will cause your application to ask permission to use the bluetooth stack from the user (may not be desired), and if they refuse, the only thing you'll see is CBCentralManagerStateUnauthorized
  • Retrieval of bluetooth state is async, and continuous. You will need to setup a delegate to get state changes, as checking the state of a freshly allocated bluetooth manager will return CBCentralManagerStateUnknown

这是说,这种方法似乎确实提供了蓝牙堆栈状态的实时更新。

That being said, this method does seem to provide real time updates of bluetooth stack state.

包含CoreBluetooth框架后,

After including the CoreBluetooth framework,

#import <CoreBluetooth/CoreBluetooth.h>

这些测试很容易使用:

- (void)detectBluetooth
{
    if(!self.bluetoothManager)
    {
        // Put on main queue so we can call UIAlertView from delegate callbacks.
        self.bluetoothManager = [[CBCentralManager alloc] initWithDelegate:self queue:dispatch_get_main_queue()];
    }
    [self centralManagerDidUpdateState:self.bluetoothManager]; // Show initial state
}

- (void)centralManagerDidUpdateState:(CBCentralManager *)central
{
    NSString *stateString = nil;
    switch(self.bluetoothManager.state)
    {
        case CBCentralManagerStateResetting: stateString = @"The connection with the system service was momentarily lost, update imminent."; break;
        case CBCentralManagerStateUnsupported: stateString = @"The platform doesn't support Bluetooth Low Energy."; break;
        case CBCentralManagerStateUnauthorized: stateString = @"The app is not authorized to use Bluetooth Low Energy."; break;
        case CBCentralManagerStatePoweredOff: stateString = @"Bluetooth is currently powered off."; break;
        case CBCentralManagerStatePoweredOn: stateString = @"Bluetooth is currently powered on and available to use."; break;
        default: stateString = @"State unknown, update imminent."; break;
    }
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Bluetooth state"
                                                     message:stateString
                                                    delegate:nil
                                          cancelButtonTitle:@"ok" otherButtonTitles: nil];
    [alert show];
}

这篇关于如何以编程方式获取iphone中蓝牙(ON / OFF)的状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆