菜鸟教程小白 发表于 2022-12-13 03:45:23

ios - 未调用 centralManager 方法


                                            <p><p>我是一名 Android 开发人员,正在转向 iOS,所以请多多了解 iOS 开发的基础知识。 </p>

<p>我有以下代码:</p>

<p>这是 <code>.m</code> 文件:</p>

<pre><code>#import &#34;BlueToothLEManager.h&#34;
#import &#34;Constants.h&#34;

@implementation BlueToothLEManager

@synthesize mBTCentralManager;


-(void)initializeCBCentralManager{
    NSLog(@&#34;initializing CBCentral Manager&#34;); &lt;--- This is being logged
    mBTCentralManager = [ initWithDelegate:self queue:nil];

}

#pragma mark - CBCentralManagerDelegate

// method called whenever you have successfully connected to the BLE peripheral
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral
{
}

// CBCentralManagerDelegate - This is called with the CBPeripheral class as its main input parameter. This contains most of the information there is to know about a BLE peripheral.
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
{
    NSLog(@&#34;Discovered %@ at %@&#34;, peripheral.name, RSSI);
}

-(void)centralManagerDidUpdateState:(CBCentralManager *)central{
    NSLog(@&#34;Start scan&#34;); &lt;---- This isNOT being logged.
    if(central.state != CBCentralManagerStatePoweredOn){
      return;
    }
    if(central.state == CBCentralManagerStatePoweredOn){
      NSLog(@&#34;Scanning for BTLE device&#34;);
      ] options:@{ CBCentralManagerScanOptionAllowDuplicatesKey : @YES }];
    }
}
@end
</code></pre>

<p>这是 <code>.h</code> 文件:</p>

<pre><code>#import &lt;Foundation/Foundation.h&gt;

@import CoreBluetooth;

@interface BlueToothLEManager : NSObject &lt; CBCentralManagerDelegate, CBPeripheralDelegate&gt;{
    CBCentralManager *mBTCentralManager;
}

@property (strong, retain) CBCentralManager *mBTCentralManager;

-(void) initializeCBCentralManager;


@end
</code></pre>

<p>当我调用 <code>initializeCBCentralManager</code> 时,一切似乎都正常工作,但由于某种原因,没有调用 <code>centralManagerDidUpdateState</code> 方法。有人可以告诉我我做错了什么吗? </p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>您应该清理您的属性定义,因为您的 iVar 与您的属性混淆了。如果您声明一个属性,则不需要声明 iVar。您也不需要 <code>@synthesize</code> ,除非您想要支持变量的特定名称。如果您使用 <code>self.</code> 表示法,那么您可以确定您指的是属性而不是 iVar。</p>

<p>您的 .h 文件应该是 -</p>

<pre><code>@import CoreBluetooth;

@interface BlueToothLEManager : NSObject &lt; CBCentralManagerDelegate, CBPeripheralDelegate&gt;

@property (strong, retain) CBCentralManager *mBTCentralManager;

-(void) initializeCBCentralManager;

@end
</code></pre>

<p>那么你的 .m 文件将是</p>

<pre><code>#import &#34;BlueToothLEManager.h&#34;
#import &#34;Constants.h&#34;

@implementation BlueToothLEManager


-(void)initializeCBCentralManager{
    NSLog(@&#34;initializing CBCentral Manager&#34;);
    self.mBTCentralManager = [ initWithDelegate:self queue:nil];

}

#pragma mark - CBCentralManagerDelegate

// method called whenever you have successfully connected to the BLE peripheral
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral
{
}

// CBCentralManagerDelegate - This is called with the CBPeripheral class as its main input parameter. This contains most of the information there is to know about a BLE peripheral.
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
{
    NSLog(@&#34;Discovered %@ at %@&#34;, peripheral.name, RSSI);
}

-(void)centralManagerDidUpdateState:(CBCentralManager *)central{
    NSLog(@&#34;Start scan&#34;);

    if(central.state == CBCentralManagerStatePoweredOn){
      NSLog(@&#34;Scanning for BTLE device&#34;);
      ] options:@{ CBCentralManagerScanOptionAllowDuplicatesKey : @YES }];
    }
}
@end
</code></pre>

<p>我已经测试过了,它可以工作</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 未调用 centralManager 方法,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/27324220/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/27324220/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 未调用 centralManager 方法