0

I'm working on my first bluetooth app. For now i've an AppUIview that implements a button that call a function in the AppCentralManager where the bluetooth function will be implemented:

- (IBAction) Scan{

NSLog(@"scan function");

[[AppBluetoothCenter alloc]initialize];

}

in the AppBluetoothCenter.h i've declaration those functions:

#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

in the AppBluetoothCenter.m i've implemented the functions:

#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

Running app in the log console i receive:

AppBluetooth[1273:60b] scan function
AppBluetooth[1273:60b] initialize

Why centralManagerDidUpdateState it's not be called?

CarloM_dtb
  • 21
  • 2
  • 9

2 Answers2

0

Iphone4 - doesn't support bluetooth4 modules. first device that support BLE is iPhone 4s

[[AppBluetoothCenter alloc]initialize];

i don't think it is a good idea to call initialize after alloc

system will call initialize for you.

+initialize is a class method, run before any instances of the class are created and before other class methods are run. +initialize isn't something you use most of the time, but it's handy for setting up any static variables that the class as a whole might use, or for ensuring that certain conditions are met before any instances are created.

so put your init code NSLog(@"initialize");

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

in init method

i think thats the trouble

and use init

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

this is how my manager looks like //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

this is for singleton pattern, i most cases you don't need another instance of bluetooth manager in your application, so every time you call init it will return the same object, hope that helps

Eugene_skr
  • 79
  • 3
  • I use [[AppBluetoothCenter alloc]initialize]; because i've an interface file AppBluetoothView, with the button, and in this way i can call the method in AppBluetoothCenter. AppBluetoothCenter* bleCenter = [[AppBluetoothCenter alloc] init]; don't permitt me to call the method initialize. – CarloM_dtb Mar 05 '14 at 14:56
  • initialize, is not the method you need for setup BluetoothCenter, it is for static var. and other class var. not for instance of class. read about "load" and "initialize" methods – Eugene_skr Mar 06 '14 at 11:50
0

Perhaps this is the real reason, you can try

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

NSLog(@"scan function");

appBluetoothCenter=[[AppBluetoothCenter alloc]initialize];

}
J. Steen
  • 15,470
  • 15
  • 56
  • 63
zks
  • 69
  • 3