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

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

问题描述

我试图以编程方式获取 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+
  • 它仅适用于支持蓝牙 LE 规格(iPhone 4S+、第 5 代 iPod+、iPad第三代+)
  • 简单地分配类将导致您的应用程序向用户请求使用蓝牙堆栈的许可(可能不需要),如果他们拒绝,您将唯一看到的是 CBCentralManagerStateUnauthorizediOS7+ 修订版:现在可以防止上述删除,请参阅下面的评论,其中指向这个答案,其中说明您可以设置 CoreBluetooth 的CBCentralManagerOptionShowPowerAlertKey 选项为 NO 以防止权限提示.
  • 蓝牙状态的检索是异步且连续的.您需要设置一个委托来获取状态更改,因为检查新分配的蓝牙管理器的状态将返回 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 iOS7+ Revision: Aforementioned strike-through can now be prevented, see comments below which point to this answer which explains you can set CoreBluetooth's CBCentralManagerOptionShowPowerAlertKey option to NO to prevent permissions prompt.
  • 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 中蓝牙(开/关)的状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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