Temporizador (NSTimer scheduleTimerWithTimeInternval) y Bluetooth Delegate: CBPeripheralDelegate no funciona con NSTimer

I need to create a regular timer to read the RSSI value of the Bluetooth Peripheral. My code has a ViewController is a delegate to an object which in turn is delegate for Bluetooth:

En mi ViewController:

@property (weak, nonatomic) ioeBLE *bleControllerOfDiscoveredGateway;

In the 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 is implemented one of the delegate methods for CBPeripheralDelegate:

- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error
{

// Code Here 

}

In my ViewController - I am trying to create a regular timer to read the RSSI value by using the Timer Selector, here is the code for the timer creation:

// Schedules a new timer, adds it to the current run loop and 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 I invoke the Timer in viewDidAppear:

[self startTimer]

The problem I am running into is straightforward - when I setup the timer as above, the didUpdateValueForCharacterstic is NOT called/invoked. I comment out the timer above and it starts working. I have confirmed that data gets to the activePeripheral that is connected to the iPhone, and the connection is alive HOWEVER the response back from the Peripheral never makes it back because the delegate method does NOT get called.

preguntado el 04 de septiembre de 13 a las 02:09

1 Respuestas

Found the FIX. All right, my bad - I should NOT have been calling [self startTimer], I needed to have this:

- (void) start
{
    @autoreleasepool {
        [NSThread detachNewThreadSelector:@selector(startTimer)
                                 toTarget:self
                               withObject:nil];
    }
}

And then in viewDidAppear:

[self start]

Respondido el 04 de Septiembre de 13 a las 02:09

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.