11

この単純なコードを使用すると、なぜCBCentralManagerStateUnknowniPad 2 を使用するのですか?

- (BOOL)viewDidLoad {

    bluetoothManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];

    if ([manager state] == CBCentralManagerStatePoweredOff) NSLog(@"CBCentralManagerStatePoweredOff");
    if ([manager state] == CBCentralManagerStatePoweredOn) NSLog(@"CBCentralManagerStatePoweredOn");
    if ([manager state] == CBCentralManagerStateResetting) NSLog(@"CBCentralManagerStateResetting");
    if ([manager state] == CBCentralManagerStateUnauthorized) NSLog(@"CBCentralManagerStateUnauthorized");
    if ([manager state] == CBCentralManagerStateUnknown) NSLog(@"CBCentralManagerStateUnknown");
    if ([manager state] == CBCentralManagerStateUnsupported) NSLog(@"CBCentralManagerStateUnsupported");

}

CBCentralManagerStateUnknown私は何を意味するのか理解できません。私は何をしますか?Appleのドキュメントには次のように書かれています:

状態不明、更新間近。

Bluetooth デバイスが接続されている場合、および Bluetooth がオフの場合にも、この応答が返されます。のようなものを実行しようとすると[manager retrieveConnectedPeripherals]、コンソールに次のメッセージも表示されます。

CoreBluetooth[WARNING] <CBConcreteCentralManager: ...> is not powered on
4

6 に答える 6

16

デリゲートが呼び出されない理由はわかっています。オブジェクトがメモリから削除されるためです。強力なプロパティを作成するだけです

@property (strong, nonatomic) DiscoverBluetoothDevices *btDevices;

そして初期化で

@implementation DiscoverBluetoothDevices
- (id) init
{
    self = [super init];
    if(self) {
        centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:dispatch_get_main_queue()];
        [centralManager scanForPeripheralsWithServices:nil options:@{CBCentralManagerScanOptionAllowDuplicatesKey: @YES}];

    }
    return self;
}

そして、デリゲートが適切に呼び出されるようになりました。

于 2013-03-30T16:15:55.447 に答える
3

CBCentralManagerStateUnknowniOS が BLE プロセスを開始したが、初期化が完了していないことを意味します。少し待つと、状態が変化します。

CBCentralManagerDelegate一般に、初期化呼び出しの直後に状態変化を見るのではなく、デリゲート ハンドラーで状態変化を検出することによって、「少し待って」ください。実装します

- (void) centralManagerDidUpdateState: (CBCentralManager *) central;

Apple の心拍数モニターなど、これを示す良い例がいくつかあります。

于 2012-09-20T21:26:43.137 に答える
1

実際の答え(私が知っている古い質問); 周辺機器のスキャンを開始すると、BT LE が起動し、デリゲートがコールバックされます。これを行うまで、デリゲートと状態情報は変更されませんでした。

a. b. cbcentralmanager を次のようにセットアップします。c. コードと .h ファイルに -central* デリゲートを含めます。NSLog または画面上のラベルを新しいステータスで更新します。そして… 成功。

cManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];

[cManager scanForPeripheralsWithServices:nil options:@{CBCentralManagerScanOptionAllowDuplicatesKey: @YES}];
于 2013-03-23T06:32:16.857 に答える
0

私の場合、 AppDelegate をデリゲートとして使用しました

CBCentralManagerDelegate

および間接的

 AVCaptureMetadataOutputObjectsDelegate.

一度に。

1) スレッドに注意してください。使用する

dispatch_get_main_queue() 

また

[NSThread mainThread]

BLEでの作業用。

2) 1 つのオブジェクトでこの 2 つのデリゲートを使用する場合は注意してください。ハードウェアは広告ではなく、コンテキストを保存するため

于 2015-08-10T19:24:25.550 に答える