Timer (NSTimer scheduledTimerWithTimeInternval) and Bluetooth Delegate: CBPeripheralDelegate does not work with NSTimer -
i need create regular timer read rssi value of bluetooth peripheral. code has viewcontroller delegate object in turn delegate bluetooth:
in viewcontroller:
@property (weak, nonatomic) ioeble *blecontrollerofdiscoveredgateway;
in class ioeble (ioeble.h):
@protocol ioebledelegate <nsobject> @optional -(void) responsefromblecontroller:(nsstring *)sw; @required @end @interface ioeble : nsobject <cbcentralmanagerdelegate, cbperipheraldelegate> @property (nonatomic,assign) id <ioebledelegate> delegate; @property (strong, nonatomic) nsmutablearray *peripherals; @property (strong, nonatomic) cbcentralmanager *cm; @property (strong, nonatomic) cbperipheral *activeperipheral; @end
in ioeble.m implemented 1 of delegate methods cbperipheraldelegate:
- (void)peripheral:(cbperipheral *)peripheral didupdatevalueforcharacteristic:(cbcharacteristic *)characteristic error:(nserror *)error { // code here }
in viewcontroller - trying create regular timer read rssi value using timer selector, here code timer creation:
// schedules new timer, adds current run loop , waits forever. - (void) starttimer { _timer = [nstimer scheduledtimerwithtimeinterval: 10.0 target:self selector:@selector(request) userinfo:nil repeats:yes]; // [[nsrunloop mainrunloop] addtimer:_timer formode:nsdefaultrunloopmode]; [[nsrunloop currentrunloop] addtimer:_timer formode:nsdefaultrunloopmode]; // [[nsrunloop mainrunloop] rununtildate:[nsdate distantfuture]]; [[nsrunloop currentrunloop] rununtildate:[nsdate distantfuture]]; }
and invoke timer in viewdidappear:
[self starttimer]
the problem running straightforward - when setup timer above, didupdatevalueforcharacterstic
not called/invoked. comment out timer above , starts working. have confirmed data gets activeperipheral connected iphone, , connection alive response peripheral never makes because delegate method not called.
found fix. right, bad - should not have been calling [self starttimer], needed have this:
- (void) start { @autoreleasepool { [nsthread detachnewthreadselector:@selector(starttimer) totarget:self withobject:nil]; } }
and in viewdidappear:
[self start]
Comments
Post a Comment