简体   繁体   English

当应用程序在后台运行且没有前台服务时如何检测蓝牙变化(连接状态和特性)

[英]How to detect bluetooth changes (connection state and characteristic) when app is in background without a foreground service

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

This click will trigger a remote call the server. 单击此按钮将触发服务器的远程呼叫。

I have two questions regarding this: 我对此有两个问题:

#1 Can I have this achieved without a foreground service? #1在没有前台服务的情况下可以实现吗?

#2 If I have a foreground service running with the BLE GATT callbacks how can I detect when the device is in range again? #2如果我有运行BLE GATT回调的前台服务,如何检测设备何时再次处于范围内?

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: Yes you can achieve this without using a foreground service, by using a JobIntentService . 1:是的,您可以通过使用JobIntentService来实现此功能而无需使用前台服务。

This type of service does not require to display a notification to your user. 此类服务不需要向您的用户显示通知。

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

In order to avoid draining the battery, I suggest you don't scan continuously, but only for a few seconds every minute. 为了避免耗尽电池,建议您不要连续扫描,而应每分钟扫描几秒钟。

2: If your BLE scanner receives BLE data advertised by the device, it means the device is within range. 2:如果您的BLE扫描仪收到设备发布的BLE数据,则表明设备在范围内。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 运行前台服务时如何检测当前的前台应用程序 - How to detect current foreground app when foreground service is running 前台服务中的蓝牙连接 - Bluetooth connection in foreground service 如何检测Android应用程序何时进入后台并返回前台 - How to detect when an Android app goes to the background and come back to the foreground 如何检测 android 应用程序何时在后台和前台处理一些代码 - how to detect when android app in background and foreground to handle some code Android 用于蓝牙连接的前台服务 - Android foreground service for Bluetooth Connection 当蓝牙连接状态改变时防止 onDestroy - Prevent onDestroy when Bluetooth connection state changes 当应用程序处于前台时,检测Android 7.0 Nougat上的连接更改 - Detect connectivity changes on Android 7.0 Nougat when app is in foreground android MVVM结构中app state从后台变成前台时如何调用API - How to call API when app state become foreground from background in android MVVM structure 一个Android应用程序如何检测没有蓝牙权限的蓝牙耳机? - How can an android app detect a bluetooth headset without bluetooth permissions? Android中的后台即服务中的蓝牙连接? - Bluetooth connection in Background as a Service in Android?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM