• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

ios - 未调用 centralManager 方法

[复制链接]
菜鸟教程小白 发表于 2022-12-13 03:45:23 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我是一名 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/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap