检查蓝牙是否已启用?

时间:2011-07-11 22:33:31

标签: iphone objective-c ios ipad bluetooth

我只是想简单检查一下是否在设备上启用了蓝牙。

我不想更改应用内部的状态(或根本不更改),使用私有API,越狱设备或执行任何会导致Apple拒绝应用的内容。

我只想知道蓝牙是否已打开。

任何人都可以对此有所了解吗?有没有Apple允许这样做的方法?

我完全清楚,在阅读了无数的帖子和文档之后,Apple在蓝牙(以及其他方面)方面的限制非常严格。

如果您只能通过文档链接和/或关于学习目标-c,阅读文档等的一些讽刺评论来为此问题做出贡献,那么请不要回复。

5 个答案:

答案 0 :(得分:3)

我发现这样做的唯一方法是使用私有框架(例如蓝牙管理器),这些框架仅对越狱应用程序有用......而Apple将使用私有框架拒绝任何应用程序。我相信甚至反对他们的ToS用蓝牙做任何事情,所以你在那里运气不好。

答案 1 :(得分:3)

似乎有一个答案here - 使用Core bluetooth framework

但是,该答案仅适用于iOS 5.0及更高版本。我自己没有对此进行测试,但如果我发现它有效,我会返回并添加反馈。

答案 2 :(得分:2)

不幸的是,SDK不公开蓝牙方法。

可能有一种方法可以通过使用未记录的方法来实现,但我们都知道那里的问题。

答案 3 :(得分:2)

现在可以使用iOS 7中的CBCentralManager进行检查,并使用CBCentralManagerOptionShowPowerAlertKey选项进行初始化。

CBCentralManagerOptionShowPowerAlertKey键,可以传递给CBCentralManager上的initWithDelegate:queue:options:方法,这将导致iOS启动Central Manager&没有提示用户启用蓝牙。

发表在此处:http://chrismaddern.com/determine-whether-bluetooth-is-enabled-on-ios-passively/

答案 4 :(得分:0)

对于iOS9 +,您可以查看我的回答here

#import <CoreBluetooth/CoreBluetooth.h>

@interface ShopVC () <CBCentralManagerDelegate>

@property (nonatomic, strong) CBCentralManager *bluetoothManager;

@end

@implementation ShopVC

- (void)viewDidLoad {
    [super viewDidLoad];

    if(!self.bluetoothManager)
    {
        NSDictionary *options = @{CBCentralManagerOptionShowPowerAlertKey: @NO};
        self.bluetoothManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil options:options];
    }
}

#pragma mark - CBCentralManagerDelegate

- (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];
}
相关问题