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

穿精又带淫゛_ 提交于 2019-12-04 17:42:55

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.

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