我希望通过编程获取iPhone/iPod蓝牙的状态,即它是否打开或关闭。 是否可以使用一些Apple API或第三方API实现。
我希望通过编程获取iPhone/iPod蓝牙的状态,即它是否打开或关闭。 是否可以使用一些Apple API或第三方API实现。
CBCentralManagerOptionShowPowerAlertKey
选项设置为NO以防止权限提示。#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];
}
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)
显然,您还需要按照上面概述的方式实现CKManagerDelegate代理方法peripheralManagerDidUpdateState:
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
。 - Awesomeness这篇答案已经从原来的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")
}
}
}
BadPirate的回答有些更新,使用iOS7版本,你可以通过给定一个拥有键为"CBCentralManagerOptionShowPowerAlertKey"且设置为0的NSDictionary来让中央管理器不显示弹出警告。
self.cbManager = [[CBCentralManager alloc] initWithDelegate:self
queue:nil
options:
[NSDictionary dictionaryWithObject:[NSNumber numberWithInt:0]
forKey:CBCentralManagerOptionShowPowerAlertKey]];
iOS 5及以上版本可以使用CoreBluetooth实现。你可以使用的类是CBCentralManager,它有一个属性"state",您可以检查它来查看蓝牙是否打开。(枚举CBCentralManagerState包含您要检查的值)。
CBCentralManager
,您可以从 delegate method 或直接使用 CBCentralManager::state
和 CBCentralManager::authorization
。请保留HTML标签。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
}
}
}
这个解决方案有点老了,在苹果引入核心蓝牙之前。
- (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
}
@property (nonatomic, strong) CBCentralManager* bluetoothManager;
并设置你的类符合协议CBCentralManagerDelegate
。 - Yevhen Dubinin