我試圖通過編程獲取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