ios - centralManager method not being called -
i android developer, moving on ios, please bear me regarding basics of ios development.
i have following code:
here .m
file:
#import "bluetoothlemanager.h" #import "constants.h" @implementation bluetoothlemanager @synthesize mbtcentralmanager; -(void)initializecbcentralmanager{ nslog(@"initializing cbcentral manager"); <--- being logged mbtcentralmanager = [[cbcentralmanager alloc] initwithdelegate:self queue:nil]; } #pragma mark - cbcentralmanagerdelegate // method called whenever have connected ble peripheral - (void)centralmanager:(cbcentralmanager *)central didconnectperipheral:(cbperipheral *)peripheral { } // cbcentralmanagerdelegate - called cbperipheral class main input parameter. contains of information there know ble peripheral. - (void)centralmanager:(cbcentralmanager *)central diddiscoverperipheral:(cbperipheral *)peripheral advertisementdata:(nsdictionary *)advertisementdata rssi:(nsnumber *)rssi { nslog(@"discovered %@ @ %@", peripheral.name, rssi); } -(void)centralmanagerdidupdatestate:(cbcentralmanager *)central{ nslog(@"start scan"); <---- not being logged. if(central.state != cbcentralmanagerstatepoweredon){ return; } if(central.state == cbcentralmanagerstatepoweredon){ nslog(@"scanning btle device"); [mbtcentralmanager scanforperipheralswithservices:@[[cbuuid uuidwithstring:device_name]] options:@{ cbcentralmanagerscanoptionallowduplicateskey : @yes }]; } } @end
here .h
file:
#import <foundation/foundation.h> @import corebluetooth; @interface bluetoothlemanager : nsobject < cbcentralmanagerdelegate, cbperipheraldelegate>{ cbcentralmanager *mbtcentralmanager; } @property (strong, retain) cbcentralmanager *mbtcentralmanager; -(void) initializecbcentralmanager; @end
when call initializecbcentralmanager
appears work, reason centralmanagerdidupdatestate
method isn't being called. can tell me i'm doing wrong?
you should clean property definition ivar getting mixed property. if declare property don't need declare ivar. don't need @synthesize
unless want specific name backing variable. if use self.
notation can sure referring property , not ivar.
your .h file should -
@import corebluetooth; @interface bluetoothlemanager : nsobject < cbcentralmanagerdelegate, cbperipheraldelegate> @property (strong, retain) cbcentralmanager *mbtcentralmanager; -(void) initializecbcentralmanager; @end
then .m file be
#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 have connected ble peripheral - (void)centralmanager:(cbcentralmanager *)central didconnectperipheral:(cbperipheral *)peripheral { } // cbcentralmanagerdelegate - called cbperipheral class main input parameter. contains of information there know ble peripheral. - (void)centralmanager:(cbcentralmanager *)central diddiscoverperipheral:(cbperipheral *)peripheral advertisementdata:(nsdictionary *)advertisementdata rssi:(nsnumber *)rssi { nslog(@"discovered %@ @ %@", peripheral.name, rssi); } -(void)centralmanagerdidupdatestate:(cbcentralmanager *)central{ nslog(@"start scan"); if(central.state == cbcentralmanagerstatepoweredon){ nslog(@"scanning btle device"); [central scanforperipheralswithservices:@[[cbuuid uuidwithstring:device_name]] options:@{ cbcentralmanagerscanoptionallowduplicateskey : @yes }]; } } @end
i have tested , works
Comments
Post a Comment