ios - 未调用 centralManager 方法
<p><p>我是一名 Android 开发人员,正在转向 iOS,所以请多多了解 iOS 开发的基础知识。 </p>
<p>我有以下代码:</p>
<p>这是 <code>.m</code> 文件:</p>
<pre><code>#import "BlueToothLEManager.h"
#import "Constants.h"
@implementation BlueToothLEManager
@synthesize mBTCentralManager;
-(void)initializeCBCentralManager{
NSLog(@"initializing CBCentral Manager"); <--- 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(@"Discovered %@ at %@", peripheral.name, RSSI);
}
-(void)centralManagerDidUpdateState:(CBCentralManager *)central{
NSLog(@"Start scan"); <---- This isNOT being logged.
if(central.state != CBCentralManagerStatePoweredOn){
return;
}
if(central.state == CBCentralManagerStatePoweredOn){
NSLog(@"Scanning for BTLE device");
] options:@{ CBCentralManagerScanOptionAllowDuplicatesKey : @YES }];
}
}
@end
</code></pre>
<p>这是 <code>.h</code> 文件:</p>
<pre><code>#import <Foundation/Foundation.h>
@import CoreBluetooth;
@interface BlueToothLEManager : NSObject < CBCentralManagerDelegate, CBPeripheralDelegate>{
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 < CBCentralManagerDelegate, CBPeripheralDelegate>
@property (strong, retain) CBCentralManager *mBTCentralManager;
-(void) initializeCBCentralManager;
@end
</code></pre>
<p>那么你的 .m 文件将是</p>
<pre><code>#import "BlueToothLEManager.h"
#import "Constants.h"
@implementation BlueToothLEManager
-(void)initializeCBCentralManager{
NSLog(@"initializing CBCentral Manager");
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(@"Discovered %@ at %@", peripheral.name, RSSI);
}
-(void)centralManagerDidUpdateState:(CBCentralManager *)central{
NSLog(@"Start scan");
if(central.state == CBCentralManagerStatePoweredOn){
NSLog(@"Scanning for BTLE device");
] 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]