OStack程序员社区-中国程序员成长平台

标题: ios - 未调用 centralManager 方法 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 03:45
标题: ios - 未调用 centralManager 方法

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

我有以下代码:

这是 .m 文件:

#import "BlueToothLEManager.h"
#import "Constants.h"

@implementation BlueToothLEManager

@synthesize mBTCentralManager;


-(void)initializeCBCentralManager{
    NSLog(@"initializing CBCentral Manager"); <--- This is being logged
    mBTCentralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];

}

#pragma mark - CBCentralManagerDelegate

// method called whenever you have successfully connected to the BLE peripheral
- (void)centralManagerCBCentralManager *)central didConnectPeripheralCBPeripheral *)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)centralManagerCBCentralManager *)central didDiscoverPeripheralCBPeripheral *)peripheral advertisementDataNSDictionary *)advertisementData RSSINSNumber *)RSSI
{
    NSLog(@"Discovered %@ at %@", peripheral.name, RSSI); 
}

-(void)centralManagerDidUpdateStateCBCentralManager *)central{
    NSLog(@"Start scan"); <---- This is  NOT being logged. 
    if(central.state != CBCentralManagerStatePoweredOn){
        return;
    }
    if(central.state == CBCentralManagerStatePoweredOn){
        NSLog(@"Scanning for BTLE device"); 
        [mBTCentralManager scanForPeripheralsWithServices[[CBUUID UUIDWithStringEVICE_NAME]] options{ CBCentralManagerScanOptionAllowDuplicatesKey : @YES }];
    }
}
@end

这是 .h 文件:

#import <Foundation/Foundation.h>

@import CoreBluetooth;

@interface BlueToothLEManager : NSObject < CBCentralManagerDelegate, CBPeripheralDelegate>{
    CBCentralManager *mBTCentralManager; 
}

@property (strong, retain) CBCentralManager *mBTCentralManager;

-(void) initializeCBCentralManager;


@end

当我调用 initializeCBCentralManager 时,一切似乎都正常工作,但由于某种原因,没有调用 centralManagerDidUpdateState 方法。有人可以告诉我我做错了什么吗?



Best Answer-推荐答案


您应该清理您的属性定义,因为您的 iVar 与您的属性混淆了。如果您声明一个属性,则不需要声明 iVar。您也不需要 @synthesize ,除非您想要支持变量的特定名称。如果您使用 self. 表示法,那么您可以确定您指的是属性而不是 iVar。

您的 .h 文件应该是 -

@import CoreBluetooth;

@interface BlueToothLEManager : NSObject < CBCentralManagerDelegate, CBPeripheralDelegate>

@property (strong, retain) CBCentralManager *mBTCentralManager;

-(void) initializeCBCentralManager;

@end

那么你的 .m 文件将是

#import "BlueToothLEManager.h"
#import "Constants.h"

@implementation BlueToothLEManager


-(void)initializeCBCentralManager{
    NSLog(@"initializing CBCentral Manager"); 
    self.mBTCentralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];

}

#pragma mark - CBCentralManagerDelegate

// method called whenever you have successfully connected to the BLE peripheral
- (void)centralManagerCBCentralManager *)central didConnectPeripheralCBPeripheral *)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)centralManagerCBCentralManager *)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"); 
        [central scanForPeripheralsWithServices[[CBUUID UUIDWithStringEVICE_NAME]] options{ CBCentralManagerScanOptionAllowDuplicatesKey : @YES }];
    }
}
@end

我已经测试过了,它可以工作

关于ios - 未调用 centralManager 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27324220/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4