Ios CBCentralManager初始化CentralManagerDipDate后,将其声明为';这不是电话

Ios CBCentralManager初始化CentralManagerDipDate后,将其声明为';这不是电话,ios,objective-c,bluetooth-lowenergy,core-bluetooth,cbcentralmanager,Ios,Objective C,Bluetooth Lowenergy,Core Bluetooth,Cbcentralmanager,我正在开发我的第一个蓝牙应用程序。目前,我有一个AppUIview,它实现了一个按钮,该按钮在AppCentralManager中调用一个函数,其中将实现蓝牙功能: -(iAction)扫描{ NSLog(“扫描功能”); [[AppBluetoothCenter alloc]初始化]; } 在AppBluetoothCenter.h中,我声明了以下函数: #导入 #进口 #进口 #进口 #导入“AppDelegate.h” @接口AppBluetoothCenter:NSObject @物业

我正在开发我的第一个蓝牙应用程序。目前,我有一个AppUIview,它实现了一个按钮,该按钮在AppCentralManager中调用一个函数,其中将实现蓝牙功能:

-(iAction)扫描{
NSLog(“扫描功能”);
[[AppBluetoothCenter alloc]初始化];
}
在AppBluetoothCenter.h中,我声明了以下函数:

#导入
#进口
#进口
#进口
#导入“AppDelegate.h”
@接口AppBluetoothCenter:NSObject
@物业CBCentralManager*CentralManager;
-(作废)初始化;
@结束
在AppBluetoothCenter.m中,我实现了以下功能:

#导入“AppBluetoothCenter.h”
@蓝牙中心的实现
-(无效)初始化{
NSLog(@“初始化”);
_CentralManager=[[CBCentralManager alloc]initWithDelegate:自队列:nil];
}
-(void)CentralManagerDipDateState:(CBCentralManager*)central
{
if(central.state==CBCentralManagerStatePoweredOff){
NSLog(@“BLE OFF”);
}
else if(central.state==CBCentralManagerStatePoweredOn){
NSLog(@“BLE ON”);
}
else if(central.state==CBCentralManagerStateUnknown){
NSLog(@“未识别”);
}
else if(central.state==cbcentralmanagerstate不受支持){
NSLog(@“不支持BLE”);
}
}
@结束
在我收到的日志控制台中运行应用程序:

AppBluetooth[1273:60b]扫描功能
AppBluetooth[1273:60b]初始化


为什么不调用CentralManagerDipDateState?

Iphone4-不支持bluetooth4模块。 第一款支持BLE的设备是iPhone 4s

[[AppBluetoothCenter alloc]initialize];
我认为在alloc之后调用initialize不是一个好主意

系统将为您调用初始化

+initialize是一个类方法,在创建该类的任何实例之前以及在运行其他类方法之前运行+初始化不是 一些你大部分时间都在使用的东西,但它对于设置任何 类作为一个整体可能使用的静态变量,或用于确保 在创建任何实例之前满足某些条件

所以把你的初始化代码 NSLog(@“初始化”)

_CentralManager=[[CBCentralManager alloc]initWithDelegate:自队列:nil]

在init方法中

我想这就是问题所在

并使用init

- (IBAction) Scan{
NSLog(@"scan function");
AppBluetoothCenter* bleCenter = [[AppBluetoothCenter alloc] init];
}
这就是我的经理的样子 //H

使用\u块定义\u共享\u实例\u


这是针对单例模式的,在大多数情况下,应用程序中不需要另一个bluetooth manager实例,因此每次调用init时,它都会返回相同的对象,希望对您有所帮助。也许这是真正的原因,您可以尝试一下

@property (strong,nonatomic) AppBluetoothCenter *appBluetoothCenter;
…………………………
- (IBAction) Scan{

NSLog(@"scan function");

appBluetoothCenter=[[AppBluetoothCenter alloc]initialize];

}

您在哪个设备上运行应用程序?我在Iphone5和Iphone4上运行过,这两款手机都使用iOS7,只有iPhone4S+支持BLE。检查是否适合你。我知道,该应用程序也在iphone5上运行。但无论如何,如果我在iPhone4上运行应用程序,central.state的结果应该是CBCentralManagerStateUnsupportedI use[[AppBluetoothCenter alloc]initialize];因为我有一个带有按钮的接口文件AppBluetoothView,这样我就可以在AppBluetoothCenter中调用该方法。AppBluetoothCenter*bleCenter=[[AppBluetoothCenter alloc]init];不要允许我调用initialize.initialize方法,这不是安装BluetoothCenter所需的方法,它用于静态变量和其他类变量,而不是类的实例。阅读“加载”和“初始化”方法
+ (MWBluetoothManager *)sharedInstance {
    DEFINE_SHARED_INSTANCE_USING_BLOCK(^{
        return [[self alloc] init];
    });
}

- (id)init
{
    self = [super init];
    if (self)
    {
        _deviceList = [[NSMutableArray alloc] init];
        _metaDataForDevices = [[NSMutableDictionary alloc] init];
//        _vendor_name = {0};
        _libver = 0;
        _sendBuffer = [[NSMutableData alloc] init];        
    }
    return self;
}

-(CBCentralManager *)centralManager
{
    if (!_centralManager)
        _centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
    return _centralManager;
}
@property (strong,nonatomic) AppBluetoothCenter *appBluetoothCenter;
…………………………
- (IBAction) Scan{

NSLog(@"scan function");

appBluetoothCenter=[[AppBluetoothCenter alloc]initialize];

}