【问题标题】:After CBCentralManager initialize centralManagerDidUpdateState it's not call在 CBCentralManager 初始化 centralManagerDidUpdateState 之后它没有被调用
【发布时间】:2014-04-07 11:24:58
【问题描述】:

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

- (IBAction) Scan{

NSLog(@"scan function");

[[AppBluetoothCenter alloc]initialize];

}

在 AppBluetoothCenter.h 我已经声明了这些函数:

#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import <CoreBluetooth/CoreBluetooth.h>
#import <CoreBluetooth/CBService.h>
#import "AppDelegate.h"

@interface AppBluetoothCenter : NSObject <UIApplicationDelegate,CBCentralManagerDelegate, CBPeripheralDelegate>

@property CBCentralManager* CentralManager;

- (void) initialize;

@end

在 AppBluetoothCenter.m 中我实现了以下功能:

#import "AppBluetoothCenter.h"

@implementation AppBluetoothCenter

-(void)initialize{
NSLog(@"initialize");

_CentralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];

}

- (void)centralManagerDidUpdateState:(CBCentralManager *)central
{

if (central.state == CBCentralManagerStatePoweredOff){
    NSLog(@"BLE OFF");
}
else if (central.state == CBCentralManagerStatePoweredOn){
    NSLog(@"BLE ON");
}
else if (central.state == CBCentralManagerStateUnknown){
    NSLog(@"NOT RECOGNIZED");
}
else if(central.state == CBCentralManagerStateUnsupported){
    NSLog(@"BLE NOT SUPPORTED");
}
}
@end

在我收到的日志控制台中运行应用程序:

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

为什么没有调用centralManagerDidUpdateState?

【问题讨论】:

  • 您在哪个设备上运行您的应用程序?
  • 我在 Iphone5 和 Iphone4 上运行过,都使用 iOS7
  • 只有 iPhone4S+ 支持 BLE。检查developer.apple.com/library/ios/samplecode/TemperatureSensor/… 是否适合您。
  • 我知道,该应用程序也在 iphone5 上运行。但无论如何,如果我在 iPhone4 中运行应用程序,central.state 的结果应该是 CBCentralManagerStateUnsupported

标签: ios objective-c bluetooth-lowenergy core-bluetooth cbcentralmanager


【解决方案1】:

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

[[AppBluetoothCenter alloc]initialize];

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

系统会为你调用初始化。

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

所以把你的初始化代码 NSLog(@"初始化");

_CentralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];

在初始化方法中

我认为这就是麻烦

并使用初始化

- (IBAction) Scan{
NSLog(@"scan function");
AppBluetoothCenter* bleCenter = [[AppBluetoothCenter alloc] init];
}

这就是我的经理的样子 //H

#define DEFINE_SHARED_INSTANCE_USING_BLOCK(block) \
static dispatch_once_t pred = 0; \
__strong static id _sharedObject = nil; \
dispatch_once(&pred, ^{ \
_sharedObject = block(); \
}); \
return _sharedObject; \

@interface MWBluetoothManager : NSObject<CBCentralManagerDelegate, CBPeripheralDelegate>
{}
+ (MWBluetoothManager *)sharedInstance;
-(void) startScan;
-(void) stopScan;
//..and other methods
@end

//M

+ (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;
}

DEFINE_SHARED_INSTANCE_USING_BLOCK

这是针对单例模式的,在大多数情况下,您的应用程序中不需要另一个蓝牙管理器实例,因此每次调用 init 时都会返回相同的对象,希望对您有所帮助

【讨论】:

  • 我使用 [[AppBluetoothCenter alloc]initialize];因为我有一个接口文件AppBluetoothView,带有按钮,这样我就可以调用AppBluetoothCenter中的方法。 AppBluetoothCenter* bleCenter = [[AppBluetoothCenter alloc] init];不允许我调用方法初始化。
  • initialize,不是设置BluetoothCenter所需的方法,它是用于静态变量的。和其他类变量。不是类的例子。阅读“加载”和“初始化”方法
【解决方案2】:

也许这才是真正的原因,你可以试试

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

NSLog(@"scan function");

appBluetoothCenter=[[AppBluetoothCenter alloc]initialize];

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-25
    • 1970-01-01
    • 1970-01-01
    • 2019-07-14
    相关资源
    最近更新 更多