如何以编程方式获取iphone中蓝牙(ON / OFF)的状态

编程入门 行业动态 更新时间:2024-10-28 08:30:29
本文介绍了如何以编程方式获取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)的状态

本文发布于:2023-11-27 20:25:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1639483.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:蓝牙   状态   方式   iphone

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!