简体   繁体   English

连接到 ble 设备时是否可以启动服务/活动?

[英]Is it possible to start service/activity when connect to ble device?

I am develop a android application with BLE.我正在使用 BLE 开发 android 应用程序。

My bluetooth device can be searched by bluetooth scanning of android.我的蓝牙设备可以通过android的蓝牙扫描搜索到。

And my application can connect with the bluetooth device by connectGatt()我的应用程序可以通过connectGatt()与蓝牙设备连接

When my application is running(even there's only foreground service running), the connection is continue.当我的应用程序正在运行时(即使只有前台服务正在运行),连接也会继续。 (I use autoConnect=true ) (我使用autoConnect=true

But even if my application is not running, I hope Android OS will try to connect if it finds my Bluetooth device and start foreground service or activity of my application.但即使我的应用程序没有运行,我希望 Android 操作系统在找到我的蓝牙设备并启动我的应用程序的前台服务或活动时会尝试连接。

is that possible?那可能吗?

In the window of bluetooth device list found by Android OS, I can find my bluetooth device.在 Android OS 找到的蓝牙设备列表 window 中,我可以找到我的蓝牙设备。 But if i touch it, they say "an app is needed to use this device" right now.但是,如果我触摸它,他们现在会说“需要一个应用程序才能使用此设备”。

Please tell me how to let Android OS try to connect with my bluetooth device even if my application is running or not.请告诉我如何让 Android 操作系统尝试连接我的蓝牙设备,即使我的应用程序是否正在运行。

No, Android will not start an application (a new process) due to a BLE connection has been established.不,Android 不会启动应用程序(新进程),因为已经建立了 BLE 连接。 A connectGatt attempt is only valid as long as the app process runs.只有在应用进程运行时,connectGatt 尝试才有效。

You can however scan for devices without the app running, see https://developer.android.com/reference/android/bluetooth/le/BluetoothLeScanner#startScan(java.util.List%3Candroid.bluetooth.le.ScanFilter%3E,%20android.bluetooth.le.ScanSettings,%20android.app.PendingIntent) .但是,您可以在没有运行应用程序的情况下扫描设备,请参阅https://developer.android.com/reference/android/bluetooth/le/BluetoothLeScanner#startScan(java.util.List%3Candroid.bluetooth.le.ScanFilter%3E, %20android.bluetooth.le.ScanSettings,%20android.app.PendingIntent)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM