检查蓝牙是否已启用?

7
我只想简单检查设备上是否启用了蓝牙。我不想从应用内部改变状态(或根本不改变状态),使用私有API,越狱设备,或做任何会导致苹果拒绝应用的事情。我只是想知道蓝牙是否已经打开。有人能给予帮助吗?是否有苹果允许的方法来实现这一点?我完全意识到,阅读无数帖子和文档后,当涉及到蓝牙(以及其他事情)时,苹果非常限制。如果您只能通过文档链接和/或关于学习Objective-C、阅读文档等的嘲讽话语来回答这个问题,请不要回复。

你看过这篇博客文章吗?http://carpe-cocoa.com/2009-07-29/detecting-when-bluetooth-is-disabled-with-gksession/ - Jesse Naugher
2
你肯定会得到很多帮助,如果你粗鲁并疏远任何可能想要帮助你的人,请继续保持。 - Jesse Naugher
@Jesse 我并不是有意冒犯或疏远你,我只是想指出你发布的链接已经过时了,因为我们已经远远超越了iOS 3.1,所以链接中的代码基本上已经被废弃。无论如何,请接受我的道歉。 - EmphaticArmPump
道歉已经被接受了。 :) 一定有办法可以做到这一点,我感觉它可能通过Bonjour实现(我在苹果文档的某个简短片段中读到过Bonjour自动通过蓝牙传输),但我从未使用过Bonjour,也没有时间去深入研究底层内容。不过,我建议你可以考虑一下这个选项。 - Jesse Naugher
可能是重复的问题:iOS - check if bluetooth is on without system alert popup to user - Deepraj Chowrasia
5个回答

3

我发现唯一的方法是使用私有框架(例如Bluetooth Manager),但这些框架只适用于越狱应用程序...而且苹果将拒绝使用私有框架的任何应用程序。我认为甚至在他们的服务条款中,使用蓝牙进行任何操作都是不允许的,所以你没有办法。


1
实际上,它不仅对Jailbreakon有用,也适用于使用企业证书签名的应用程序。 - Victor Ronin

3

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

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

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


3

这里似乎有一个答案(链接),它是使用Core Bluetooth框架的。

然而,该答案仅适用于iOS 5.0及以上版本。我自己没有测试过,如果发现有效,我会返回并添加反馈。


2

很不幸,SDK没有公开蓝牙方法。

可能可以通过使用未记录的方法来实现,但我们都知道这样做存在问题。


是的,他们会拒绝这个应用程序,因为他们是一群****。 - Ahmad Alfy

0

对于iOS9及以上版本,您可以在这里查看我的答案。

#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];
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接