扫描并听取背景中来自蓝牙设备的事件 [英] Scan and listen to events from Bluetooth devices in background with flutter
问题描述
我想设置一个带有颤动的移动应用程序,该应用程序也将在后台运行.此应用程序允许您扫描蓝牙设备并收听事件以启动通知和/或启动铃声.我设法做到了所有这些,并且在flutter_blue插件中效果很好.但是我的问题是该应用程序必须继续在后台运行.
I want to set up a mobile application with flutter which also runs in the background. this application allows you to scan Bluetooth devices and listen to events to launch notification and/or start a ringtone. I managed to do all this and it works very well with the flutter_blue plugin. But my problem is that the application has to keep running in the background.
我是来这里寻求帮助的.
I came here to seek help.
该应用程序的功能与该应用程序的功能完全相同 https://play.google.com/store/apps/details?id=com.antilost.app3&hl=fr&gl=US
The app does exactly what this app does https://play.google.com/store/apps/details?id=com.antilost.app3&hl=fr&gl=US
推荐答案
有2种方法.
- 您要做的就是在JAVA/Kotlin中为Android编写本机代码,为iOS的obc-c/swift编写.
演示:此处
The best place to start with this is here
如果您仅点击上面的链接,那么您将能够编写MethodChannel和EventChannel的代码,这对于在Flutter和本机代码之间进行通信非常有用.因此,如果您擅长本机操作,那么对您来说就没什么大不了了.
If you just follow the above link then you will be able to code MethodChannel and EventChannel, which will be useful to communicate between flutter and native code. So, If you are good at the native side then it won't be big deal for you.
// For example, if you want to start service in native android
class MainActivty extends Activity{
// we write
//rest of the activity code
onCreate(){
startBluetoothService();
}
startBluetoothService(){
//your code
}
}
//then, For the flutter
// Flutter side
MessageChannel msgChannel=MessageChannel("MyChannel");
msgChannel.invokeMethode("startBluetoothService");
// Native side
public class MainActivity extends FlutterActivity {
private static final String CHANNEL = "MyChannel";
@Override
public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
super.configureFlutterEngine(flutterEngine);
new MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), CHANNEL)
.setMethodCallHandler(
(call, result) -> {
if (call.method.equals("startBluetoothService")) {
int result = startBluetoothService();
//then you can return the result based on the your code execution
if (result != -1) {
result.success(batteryLevel);
} else {
result.error("UNAVAILABLE", "Battery level not available.", null);
}
} else {
result.notImplemented();
}
}
);
}
}
与上述相同,您可以编写iOS端的代码.
same as above you can write the code for the iOS side.
- 第二种方法是编写自己的插件,以便您可以从 alarm_manager 或
背景位置插件.
我希望它可以帮助您解决问题.
I hope it helps you to solve the problem.
这篇关于扫描并听取背景中来自蓝牙设备的事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!