Ios 如何以编程方式获取iphone中蓝牙(开/关)的状态
我正在尝试获取iPhone/iPod Bluetooth的状态,以确定它是否以编程方式打开或关闭。Ios 如何以编程方式获取iphone中蓝牙(开/关)的状态,ios,iphone,bluetooth,iobluetooth,Ios,Iphone,Bluetooth,Iobluetooth,我正在尝试获取iPhone/iPod Bluetooth的状态,以确定它是否以编程方式打开或关闭。 是否可以使用一些苹果API或第三方API。在苹果推出核心蓝牙之前,此解决方案有点过时 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customizatio
是否可以使用一些苹果API或第三方API。在苹果推出核心蓝牙之前,此解决方案有点过时
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
Class BluetoothManager = objc_getClass( "BluetoothManager" ) ;
id btCont = [BluetoothManager sharedInstance] ;
[self performSelector:@selector(status:) withObject:btCont afterDelay:1.0f] ;
return YES ;
}
- (void)status:(id)btCont
{
BOOL currentState = [btCont enabled] ;
//check the value of currentState
}
在iOS 5及以上版本上,有一种使用CoreBooth的方法。您可以使用的类是CBCentralManager。它有一个属性“状态”,您可以检查蓝牙是否开启。(enum CBCentralManagerState具有您要检查的值)。我想与大家分享一些研究成果 您可以在不使用私有API的情况下执行此操作,但需要注意以下几点:
- 它只能在iOS 5.0+上工作
- 它只能在以下设备上工作: 支持蓝牙LE规范(iPhone 4S+、第五代iPod+、iPad 第三代+)
- 简单地分配类将导致您的应用程序向用户请求使用蓝牙堆栈的权限(可能不需要),如果他们拒绝,您将看到的唯一内容是CBCentralManagerStateUnauthorized iOS7+修订版:现在可以防止前面提到的罢工,请参阅下面的注释,该注释解释了您可以将CoreBluetooth的
选项设置为否以防止权限提示李>CBCentralManagementShowPowerAlertKey
- 蓝牙状态的检索是异步的、连续的。您需要设置一个代理以获取状态更改,因为检查新分配的蓝牙管理器的状态将返回CBCentralManagerStateUnknown
#import <CoreBluetooth/CoreBluetooth.h>
BadBirpirate回答的一些更新,使用iOS7,您可以设置中央管理器,使其在分配管理器对象时不显示警报,方法是为其提供一个NSDictionary,该NSDictionary的键“CBCentralManagementShowPowerAlertKey”设置为0
self.cbManager = [[CBCentralManager alloc] initWithDelegate:self
queue:nil
options:
[NSDictionary dictionaryWithObject:[NSNumber numberWithInt:0]
forKey:CBCentralManagerOptionShowPowerAlertKey]];
该答案已从原来的Objective-C更新为Swift 4.0 假设您已经创建了蓝牙管理器,并将代理分配给
ViewController
类
import CoreBluetooth
extension ViewController : CBCentralManagerDelegate {
func centralManagerDidUpdateState(_ central: CBCentralManager) {
switch central.state {
case .poweredOn:
print("powered on")
case .poweredOff:
print("powered off")
case .resetting:
print("resetting")
case .unauthorized:
print("unauthorized")
case .unsupported:
print("unsupported")
case .unknown:
print("unknown")
}
}
}
要禁用默认警报消息,在实例化CBPeripheralManager时只需通过选项字典: SWIFT在iOS8上测试+
import CoreBluetooth
//Define class variable in your VC/AppDelegate
var bluetoothPeripheralManager: CBPeripheralManager?
//On viewDidLoad/didFinishLaunchingWithOptions
let options = [CBCentralManagerOptionShowPowerAlertKey:0] //<-this is the magic bit!
bluetoothPeripheralManager = CBPeripheralManager(delegate: self, queue: nil, options: options)
一旦设置了
CBCentralManager
设置,您就可以从或直接使用CBCentralManager::state
和CBCentralManager::authorization
import CoreBluetooth
class Manager {
let centralManager = CBCentralManager(delegate: self, queue: nil)
var isBTTurnedOn: Bool {
return centralManager.state == .poweredOn
}
var isAuthorized: Bool {
if #available(iOS 13.0, *) {
return centralManager.authorization == .allowedAlways
} else {
return true
}
}
}
这只适用于带有BT LE的设备,即您暗指的iPhone 4S+、iPad 3+,但为了一致性:声明属性
@property(非原子、强)CBCentralManager*bluetoothManager代码>并设置您的类符合协议CBCentralManagerDelegate
这对我不起作用。有人为他们工作吗?iOS7+您可以使用CBCentralManager上指定的初始值设定项来避免警报消息,请查看以下答案:苹果是否接受此方法?或者他们会拒绝使用它的应用程序?@Signo-苹果没有问题,尽管有些报告说它不再工作。如何在swift中添加属性CBPeripheralManager。它说找不到类型“CBPeripheralManager”的初始值设定项@Aravind.A在源文件顶部添加import corebooth
。这段代码非常适合对蓝牙状态(开/关等)进行无声检查。这是在后台工作,还是仅当应用程序在屏幕上时才工作?
func peripheralManagerDidUpdateState(peripheral: CBPeripheralManager!) {
var statusMessage = ""
switch peripheral.state {
case .poweredOn:
statusMessage = "Bluetooth Status: Turned On"
case .poweredOff:
statusMessage = "Bluetooth Status: Turned Off"
case .resetting:
statusMessage = "Bluetooth Status: Resetting"
case .unauthorized:
statusMessage = "Bluetooth Status: Not Authorized"
case .unsupported:
statusMessage = "Bluetooth Status: Not Supported"
case .unknown:
statusMessage = "Bluetooth Status: Unknown"
}
print(statusMessage)
if peripheral.state == .poweredOff {
//TODO: Update this property in an App Manager class
}
}
import CoreBluetooth
class Manager {
let centralManager = CBCentralManager(delegate: self, queue: nil)
var isBTTurnedOn: Bool {
return centralManager.state == .poweredOn
}
var isAuthorized: Bool {
if #available(iOS 13.0, *) {
return centralManager.authorization == .allowedAlways
} else {
return true
}
}
}