我试图通过编程获取iPhone/iPod蓝牙的状态,无论它是开启还是关闭。 是否有可能使用某些Apple API或第三方API。如何通过程序在iphone中获得蓝牙(开/关)的状态
回答
该解决方案是有点陈旧,苹果导入纤芯蓝牙
- (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及以上使用CoreBluetooth的方式之前。您可以使用的课程是CBCentralManager。它有一个属性“状态”,您可以检查蓝牙是否打开。 (枚举CBCentralManagerState具有要检查的值)。
的一点点研究为Sam's answer,我想我会分享 你可以这样做,不使用私人API,但有几个注意事项:
- 它只会在iOS 5.0+ 工作
- 它只会在 支持蓝牙LE规范(iPhone 4S +,第5代iPod +,iPad的 第三代+)
- 简单地分配班会导致应用程序请求允许使用的蓝牙堆栈从用户设备上工作(可能没有如果他们拒绝,唯一会看到的是CBCentralManagerStateUnauthorized
- 蓝牙状态的检索是异步的,并且是连续的。您将需要设置一个代理获得状态的改变,如检查一个新分配的蓝牙管理器的状态将返回CBCentralManagerStateUnknown
话虽这么说,这个方法似乎提供蓝牙协议栈状态的实时更新。
包括CoreBluetooth框架后,
#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];
}
您提到了这一点,但为了保持一致性:声明属性@property(非原子,强大)CBCentralManager * bluetoothManager;`并将您的类设置为符合协议`CBCentralManagerDelegate` – 2014-01-14 17:19:46
这不适合我。有人得到这个为他们工作? iOS7 + – achi 2014-09-24 05:56:14
一些更新的BadPirate的答案,与iOS7可以设置中央管理器不显示通过为其分配管理对象的NSDictionary具有密钥“CBCentralManagerOptionShowPowerAlertKey”设置为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)
很明显,你还需要实现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
}
}
- 1. 如何通过iphone程序中的蓝牙发送图像?
- 2. 如何在J2ME中检查蓝牙开/关状态?
- 3. 如何获得在iOS6的蓝牙隐私设置状态
- 4. 如何通过蓝牙将蓝牙配件连接至iPhone?
- 5. iphone开关接通/断开蓝牙编程
- 6. Android蓝牙状态
- 7. 在配对状态下捕获蓝牙
- 8. 如何通过蓝牙读取手机状态
- 9. 如何开始蓝牙配对过程?
- 10. iPhone中的蓝牙编程
- 11. 在我的应用程序中打开蓝牙设备iphone
- 12. 如何在蓝牙打开时在5.1.1版本的状态栏上获取蓝牙图标?
- 13. 状态栏上的蓝牙状态
- 14. 通过蓝牙
- 15. 通过蓝牙
- 16. 通过蓝牙
- 17. 如何通过蓝牙从PC获得移动MAC地址?
- 18. 配对和iPhone应用程序通过蓝牙/ Blutooth LE连接
- 19. 从iPhone应用程序通过蓝牙传输文件
- 20. 通过通知按钮打开蓝牙
- 21. 如何通过Android应用程序过滤蓝牙标签
- 22. iphone中的蓝牙
- 23. 检查蓝牙状态
- 24. 蓝牙连接状态
- 25. 蓝牙错误状态133
- 26. 蓝牙连接状态
- 27. 收听蓝牙状态
- 28. 我可以从iOS获得有关蓝牙耳机连接状态的信息
- 29. 如何通过蓝牙将通知从arduino发送到iPhone?
- 30. 如何在PHP中开发基于蓝牙的应用程序?
这只适用于BT LE的设备,例如iPhone 4S +,iPad 3+ – domsom 2012-10-30 14:37:17