14

このコードにより、現在の Bluetooth ステータスを判別できます。

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


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

しかし、[[CBCentralManager alloc] init...]が発生すると、Bluetooth がオフの場合、システムはユーザーに警告を表示します。

ユーザーの邪魔をせずに Bluetooth の状態を確認する方法はありますか?

4

6 に答える 6

22

Apple 開発者から次のような回答がありました。 iOS7 では、このCBCentralManagerOptionShowPowerAlertKeyオプションを使用すると、このアラートを無効にできます。

初期化時にCBCentralManagerがある場合は、メソッドを使用できますinitWithDelegate:queue:options

例:

私の.hファイルにはCBCentralManager * manager

.m ファイル内:

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

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

[_manager scanForPeripheralsWithServices:nil options:nil];

このコードを使用すると、警告が表示されなくなります。

于 2013-10-21T20:45:13.217 に答える
9

迅速に、func 内のアプリ デリゲートに次の 2 行を記述できます。

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

bCentralManger は次のように宣言する必要があります。

プライベート変数 bCentralManger: CBCentralManager!

于 2016-02-29T12:22:21.890 に答える
3

BadPirateAnas の回答を組み合わせることで、システム アラートを表示せずに Bluetooth の状態を取得できます。

#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];
}
于 2017-01-19T02:42:06.787 に答える
3

以下のコードを使用して、iOS 8 以降のバージョンのアラートを無効にしました

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

[self.bluetoothManager scanForPeripheralsWithServices:nil options:nil];
于 2015-11-26T06:45:55.627 に答える
2

私はこれを iOS 9 でしかテストしていないので、誰かがこの 1 つの古い OS デバイスをテストできるかもしれません。

CBCentralManagerデリゲートを設定する代わりに、viewDidLoad必要な瞬間までこれを残します。以下の例では、WKWebView読み込みが完了したらこれを呼び出します。Web ビューの各ページで使用が必要になる可能性があるためです。 Bluetoothの に入れましたWKWebView didFinishNavigation

迅速

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;
    }
}

デリゲートが設定された瞬間にbluetoothStatus()、状態が変化します。

Bluetooth のみをオンにする通知は、アプリの初期ロード時にのみ呼び出されるようです。このようにすると、centralManagerDidUpdateState

于 2016-02-24T13:30:57.207 に答える
1

Bluetooth LE をサポートし、Bluetooth が無効になっている iOS デバイスでアプリケーションが実行されている場合、現在、このアラートを無効にする方法はありません。アラートを無効にする手段を提供することは、拡張要求です。したがって、Apple がこの拡張機能について多くのリクエストを受け取るほど、より良い結果が得られます。

于 2012-10-19T00:26:21.677 に答える