问题描述
我想设置一个带有Flutter的移动应用程序,该应用程序也将在后台运行。此应用程序允许您扫描蓝牙设备并收听事件以启动通知和/或启动铃声。 我设法做到了所有这些,并且在Flutter_blue插件中效果很好。但是我的问题是该应用程序必须继续在后台运行。
我是来这里寻求帮助的。
该应用确实执行了该应用的https://play.google.com/store/apps/details?id=com.antilost.app3&hl=fr&gl=US
解决方法
有两种方法。
- 您要做的就是在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端的代码。
- 第二种方法是编写自己的插件,以便从alarm_manager或Background_location插件中汲取灵感。
我希望它可以帮助您解决问题。