蓝牙设备的Android 8.0后台服务 [英] Android 8.0 background services for bluetooth device

查看:510
本文介绍了蓝牙设备的Android 8.0后台服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个蓝牙应用程序,假设该应用程序尚未销毁,它可以在后台跟踪温度.我注意到,在Android Oreo中,我们有后台执行限制.我需要知道我的项目中是否缺少任何内容.这是我安排项目的方式.

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.

我有一个MainActivity,它的蓝牙类BluetoothObject负责主要活动,BluetoothGattCallback()类和本地Realm数据库之间的通信.

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.

我现在在BluetoothObject中实现了Service(),它是BluetoothObject和我的Realm数据库之间的桥梁.到目前为止,一切似乎都可以进行.我使用此示例进行了所有设置.

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.

我看到的问题是,在打开应用程序且屏幕处于睡眠状态时,蓝牙连接保持连接状态.我遇到的问题是,当我的应用程序转到应用程序堆栈的后面,然后电话进入睡眠状态时.连接可能持续一个小时.我该怎么做才能保持这种连接?我需要启用任何权限吗? Android 8+中是否有引起问题的东西?

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.

如果您想不间断地运行服务,则需要通过通知来满足Foreground服务的要求.

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

这篇关于蓝牙设备的Android 8.0后台服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆