0

我正在开发我的第一个蓝牙应用程序。现在,我有一个 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 不被调用?

4

2 回答 2

0

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

[[AppBluetoothCenter alloc]initialize];

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

系统会为你调用初始化。

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

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

_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 时它都会返回相同的对象,希望对您有所帮助

于 2014-03-05T14:08:57.527 回答
0

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

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

NSLog(@"scan function");

appBluetoothCenter=[[AppBluetoothCenter alloc]initialize];

}
于 2014-10-18T09:39:51.380 回答