本文介绍了当应用程序在后台运行且没有前台服务时如何检测蓝牙变化(连接状态和特性)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个设备,它是带有按钮的简单BLE设备,并且我需要检测用户何时长时间按下该设备(通过特性更改),即使该应用程序在后台.

I have a device that is a simple BLE device with a button, and I need to detect when the user long presses the device (via characteristic change) even when the app is in the background.

此点击将触发服务器的远程呼叫.

This click will trigger a remote call the server.

对此我有两个问题:

#1我可以在没有前台服务的情况下实现此目标吗?

#1 Can I have this achieved without a foreground service?

#2如果我有运行BLE GATT回调的前台服务,如何检测设备何时再次处于范围内?

#2 If I have a foreground service running with the BLE GATT callbacks how can I detect when the device is in range again?

我当前的解决方案是让前台服务始终在后台运行,但与此同时,用户将需要始终显示该应用程序的通知.

My current solution is to have a foreground service always running in the background but with that, the user will need an always showing notification of the app.

推荐答案

1:是的,您可以使用 JobIntentService 来实现此功能而无需使用前台服务.

1:Yes you can achieve this without using a foreground service, by using a JobIntentService.

这种类型的服务不需要向您的用户显示通知.

This type of service does not require to display a notification to your user.

要知道单击设备按钮的时间,您的JobIntentService可以扫描BLE设备,并且可以在设备广告的BLE数据中包括最后单击的标志或时间戳.

To know when the device button has been clicked, your JobIntentService could scan for BLE devices, and you could include in the BLE data advertised by your device a flag or timestamp of the last click.

为了避免耗尽电池电量,建议您不要连续扫描,而应每分钟扫描几秒钟.

In order to avoid draining the battery, I suggest you don't scan continuously, but only for a few seconds every minute.

2:如果您的BLE扫描仪接收到该设备发布的BLE数据,则表明该设备在范围内.

2:If your BLE scanner receives BLE data advertised by the device, it means the device is within range.

这篇关于当应用程序在后台运行且没有前台服务时如何检测蓝牙变化(连接状态和特性)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 12:16