i schedule a Timer to read RSSI using readRSSI function ,but the timer stops when my app go into background

旧城冷巷雨未停 提交于 2020-01-01 18:12:34

问题


I schedule a Timer to read RSSI using readRSSI function ,but the timer stops when my app go into background. Maybe there is a good method to get RSSI value from peripheral's notification when app comes into background . But I don't know how to do. Could anybody help me? Anyone has a better method?


回答1:


I have got the same problem, and I have two idea.

1.

First, you should have some service that got corebluetooth delegate periodly. F or my example, I have a battery service that I can got delegate with didUpdateValueForCharacteristic periodly.

Second, set readRSSI function in didUpdateValueForCharacteristic. And don't forget to set background mode with Corebluetooth.So now you can got the RSSI updating when update battery value.

This works fine for me, but I got another idea from some sample code.

2.

Use the code below whenever you want to start the RSSI reader:

NSTimer *rssiTimer; 
[rssiTimer invalidate];    
rssiTimer = [NSTimer timerWithTimeInterval:1.0 target:peripheral selector:@selector(readRSSI) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop]addTimer:rssiTimer forMode:NSRunLoopCommonModes];

It works fine for me too.



来源:https://stackoverflow.com/questions/15651940/i-schedule-a-timer-to-read-rssi-using-readrssi-function-but-the-timer-stops-whe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!