颤抖地在后台扫描和收听来自蓝牙设备的事件

问题描述

我想设置一个带有Flutter的移动应用程序,该应用程序也将在后台运行。此应用程序允许您扫描蓝牙设备并收听事件以启动通知和/或启动铃声。 我设法做到了所有这些,并且在Flutter_blue插件效果很好。但是我的问题是该应用程序必须继续在后台运行。

我是来这里寻求帮助的。

该应用确实执行了该应用的https://play.google.com/store/apps/details?id=com.antilost.app3&hl=fr&gl=US

解决方法

有两种方法。

  1. 您要做的就是在JAVA / Kotlin中为Android编写本机代码,为iOS的obc-c / swift编写。

演示:Here

从此开始的最佳位置是here

如果仅点击以上链接,则将能够对MethodChannel和EventChannel进行编码,这对于在Flutter和本机代码之间进行通信非常有用。因此,如果您擅长原生语言,那么对您来说就没什么了。

// 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端的代码。

  1. 第二种方法是编写自己的插件,以便从alarm_managerBackground_location插件中汲取灵感。

我希望它可以帮助您解决问题。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...