8.0ble设备 android_蓝牙设备的Android 8.0后台服务

I am building a bluetooth app that tracks temperatures in the background assuming the app has not been destroyed. I noticed that in Android Oreo we have background execution limits. I need to know if I am missing anything from my project. Here is how I have the project laid out.

I have a MainActivity that has a bluetooth class BluetoothObject that takes care of communication between the main activity, BluetoothGattCallback() class, and my local Realm database.

I have now implemented a Service() in BluetoothObject, that is a bridge between the BluetoothObject and my Realm database. So far everything seems to work. I used this example to set everything up.

Since this is a bound service, do I need to call anything else to keep the service alive in the background? After I set it up, I bind the service by calling:

val intent = Intent(context, BoundService::class.java)

context.bindService(intent, myConnection, Context.BIND_AUTO_CREATE)

I also have the service set in my manifest file as instructed by the example.

The issue I am seeing is the bluetooth connection stays connected while the app is open and if the screen is asleep. I have issues is when my app goes to the back of the app stack and then the phone is asleep. The connection maybe last an hour. What can I do to keep this connection alive? Is there any permissions I need to enable? Is there something in Android 8+ that causes issues?

解决方案

There is no issue. That limitation was put specifically to kill background services that could be heavy on battery (ongoing sockets, blue tooth connections, etc.) and not directly visible to the user.

If you want your service to run unstopped, You will need to settle for Foreground service with a notification.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值