2012-09-21 24 views
12

Mã này cho phép để xác định tình trạng bluetooth hiện tại:iOS - kiểm tra xem bluetooth là không có hệ thống cửa sổ bật lên cảnh báo cho người sử dụng

CBCentralManager* testBluetooth = [[CBCentralManager alloc] initWithDelegate:nil queue: nil]; 


switch ([testBluetooth state]) {....} 

Tuy nhiên, khi [[CBCentralManager alloc] init ...] xảy ra, hệ thống cửa sổ bật lên cảnh báo cho người dùng, nếu bluetooth tắt.

Có cách nào để kiểm tra trạng thái bluetooth mà không làm phiền người dùng của tôi không?

Trả lời

2

Hiện tại không có cách nào để vô hiệu hóa cảnh báo này khi ứng dụng của bạn chạy trên thiết bị iOS hỗ trợ Bluetooth LE và nơi Bluetooth bị tắt. Nó sẽ là một yêu cầu nâng cao để cung cấp một phương tiện để vô hiệu hóa cảnh báo. Vì vậy, càng có nhiều yêu cầu của Apple về việc tăng cường này, thì càng tốt.

20

Tôi nhận được phản hồi sau từ nhà phát triển Apple: Trong iOS7, tùy chọn CBCentralManagerOptionShowPowerAlertKey cho phép bạn tắt cảnh báo này.

Nếu bạn havea một CBCentralManager khi bạn khởi nó, bạn có thể sử dụng phương pháp initWithDelegate:queue:options

Ví dụ:

Trong file .h của tôi tôi có một CBCentralManager * manager

Trong file .m:

NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:NO], CBCentralManagerOptionShowPowerAlertKey, nil]; 

_manager = [[CBCentralManager alloc] initWithDelegate:self queue:nil options:options]; 

[_manager scanForPeripheralsWithServices:nil options:nil]; 

Với mã này cảnh báo không còn xuất hiện nữa, tôi hy vọng điều đó sẽ hữu ích!

+0

Tùy chọn quét ngoại vi là: CBCentralManagerScanOptionAllowDuplicatesKey; CBCentralManagerScanOptionSolicitedServiceUUIDsKey; [liên kết] (https://developer.apple.com/library/ios/documentation/CoreBluetooth/Reference/CBCentralManager_Class/translated_content/CBCentralManager.html#//apple_ref/doc/constant_group/Peripheral_Scanning_Options) Vì vậy, hãy gửi từ điển tùy chọn bạn đang gửi không tạo sự khác biệt trong scanForPeripheralWithServices. Chỉ cần nó trong khởi tạo – shim

+0

Nó không hoạt động trong iOS 5 – Salim

+0

Không hoạt động trong iOS 8+ –

2

Tôi đã sử dụng mã dưới đây để vô hiệu hóa cảnh báo cho iOS 8 trở lên phiên bản

self.bluetoothManager = [[CBCentralManager alloc] 
             initWithDelegate:self 
             queue:dispatch_get_main_queue() 
             options:@{CBCentralManagerOptionShowPowerAlertKey: @(NO)}]; 

[self.bluetoothManager scanForPeripheralsWithServices:nil options:nil]; 
1

tôi đã chỉ được thử nghiệm này trên iOS 9 như vậy có lẽ ai đó có thể kiểm tra điều này một thiết bị chạy HĐH cũ.

Chúng tôi làm tất cả mọi thứ bình thường, ngoại trừ một điều, thay vì các thiết lập CBCentralManager đại biểu trong viewDidLoad chúng tôi rời khỏi đây cho đến thời điểm chúng ta cần nó, trong trường hợp ví dụ dưới đây tôi gọi đây là một lần tải của tôi WKWebView đã hoàn tất, và vì mỗi trang chế độ xem web của tôi có khả năng yêu cầu sử dụng Bluetooth tôi đặt số này vào WKWebView didFinishNavigation.

Swift

var managerBLE: CBCentralManager? 

func bluetoothStatus() { 
    managerBLE = CBCentralManager(delegate: self, queue: nil, options: nil) 
} 

func webView(webView: WKWebView, didFinishNavigation navigation: WKNavigation!) { 
    bluetoothStatus() 
} 

func centralManagerDidUpdateState(central: CBCentralManager) { 
    switch managerBLE!.state 
    { 
    case CBCentralManagerState.PoweredOff: 
     print("Powered Off") 
    case CBCentralManagerState.PoweredOn: 
     print("Powered On") 
    case CBCentralManagerState.Unsupported: 
     print("Unsupported") 
    case CBCentralManagerState.Resetting: 
     print("Resetting") 
     fallthrough 
    case CBCentralManagerState.Unauthorized: 
     print("Unauthorized") 
    case CBCentralManagerState.Unknown: 
     print("Unknown") 
    default: 
     break; 
    } 
} 

Khoảnh khắc đại biểu được thiết lập trong vòng bluetoothStatus() bạn sẽ thấy ngọn lửa thay đổi trạng thái.

Thông báo bật Bluetooth dường như chỉ muốn được gọi là ngay tại tải ban đầu của ứng dụng của bạn, làm nó theo cách này có nghĩa là bạn chỉ nhận được những gì bạn muốn từ centralManagerDidUpdateState

+0

Ngoài ra, tôi biết rằng phương pháp này có thể mang về tiêu thụ năng lượng bởi vì chúng tôi đang thiết lập và thiết lập lại các đại biểu để nó có thể có giá trị suy nghĩ về việc loại bỏ các CBCentralManager Delegate trước khi chúng tôi thiết lập lại nó mỗi khi chúng ta gọi hàm? –

3

Trong nhanh chóng bạn có thể làm ghi hai dòng sau trong đại biểu ứng dụng của bạn bên trong func: didFinishLaunchingWithOptions launchOptions

self.bCentralManger = CBCentralManager(delegate: self, queue: dispatch_get_main_queue(), options: [CBCentralManagerOptionShowPowerAlertKey: false]) 
    self.bCentralManger.scanForPeripheralsWithServices(nil, options: nil) 

nơi bCentralManger của bạn nên được khai báo là:

tin var bCentralManger: CBCentralManager !

1

Bằng cách kết hợp trên BadPirate'sAnas' câu trả lời, bạn có thể nhận trạng thái bluetooth mà không hiển thị cảnh báo hệ thống.

#import <CoreBluetooth/CoreBluetooth.h> 

@interface ShopVC() <CBCentralManagerDelegate> 

@property (nonatomic, strong) CBCentralManager *bluetoothManager; 

@end 

@implementation ShopVC 

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    if(!self.bluetoothManager) 
    { 
     NSDictionary *options = @{CBCentralManagerOptionShowPowerAlertKey: @NO}; 
     self.bluetoothManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil options:options]; 
    } 
} 

#pragma mark - CBCentralManagerDelegate 

- (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]; 
} 
Các vấn đề liên quan