如何获得蓝牙技术在iphone的状态(ON / OFF)编程 [英] How to get the status of bluetooth (ON/OFF) in iphone programatically

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

问题描述

我试图让iPhone / iPod的蓝牙的状态即无论是ON或OFF编程。
是否有可能使用一些苹果的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. Any suggestion would be helpful.

推荐答案

研究一点点进入萨姆的回答是我想我会分享
你可以这样做,而无需使用私有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的+,第五代iPod +,iPad的
    第三代+)

  • 简单地分配类将导致应用程序请求允许使用的蓝牙堆栈从用户(可能不期望的),如果他们拒绝,你会看到的唯一事情是CBCentralManagerStateUnauthorized

  • 蓝牙状态
  • 检索是异步,和连续。您将需要设置一个代理获得的状态变化,检查新分配的蓝牙管理器的状态将返回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>

这些测试很容易使用执行:

These tests were easy to perform using:

- (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()] autorelease];
    }
    [self centralManagerDidUpdateState:self.bluetoothManager]; // Show initial state
}

- (void)centralManagerDidUpdateState:(CBCentralManager *)central
{
    NSString *stateString = nil;
    switch(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:@"Okay" otherButtonTitleArray:nil] autorelease];
    [alert show];
}

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

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