简体   繁体   English

编程新手,正在使用 Dart。 如何使用接近传感器启动功能?

[英]New to programming, am working with Dart. How do I use the proximity sensor to initiate a function?

Essentially, I would like to use a hand waving in from the proximity sensor to initiate a function.本质上,我想用一只手从接近传感器中挥手来启动一个功能。 I'm not sure where to begin, so I thought this was the appropriate channel to pose the question and gain some insight.我不确定从哪里开始,所以我认为这是提出问题并获得一些见解的合适渠道。 Thank you.谢谢。

Flutter has a proximity_plugin which actually listens to the proximity events. Flutter 有一个proximity_plugin ,它实际上侦听邻近事件。

Example:示例:

// Modify the pubspec to add proximity_plugin
dependencies:
  proximity_plugin: "^1.0.0"

then那么

// First import the plugin
import 'package:proximity_plugin/proximity_plugin.dart';

// Add a listener to the events
proximityEvents.listen((ProximityEvent event) {
    // do something based on proximity change events
});

Hope that helped!希望有所帮助!

Base on your application I hope you might need to retrieve all sensor data from the device.根据您的应用程序,我希望您可能需要从设备中检索所有传感器数据。 It is better to have to implement your code to get accelerometer, gyroscope, and proximity data.最好必须实现您的代码以获取加速度计、陀螺仪和接近度数据。 So, from one package you cover your all sensor requirements to the application.因此,从一个包开始,您就可以满足您对应用的所有传感器要求。 Maintaining is easy.维护很容易。 The code is more readable so far...到目前为止,代码更具可读性......

Flutter has a solution to that also, Flutter 也有一个解决方案,

Before that, don't forget to add the hardware permission for WAVE_LOCK by,在此之前,不要忘记为 WAVE_LOCK 添加硬件权限,

<uses-permission android:name="android.permission.WAKE_LOCK" />

After that, You have to do is, add the library via dependency之后,您要做的是,通过依赖项添加库

//add to this in you pubspec.yaml file
dependencies:
  all_sensors: ^0.2.3

This is the latest package provided by flutter sometimes it may upgrade from time to time.这是flutter提供的最新包,有时会不时升级。 make sure to upgrade it with your application.确保使用您的应用程序升级它。

Then add it to your application by importing it.然后通过导入将其添加到您的应用程序中。

// in your dart file 
import 'package:all_sensors/all_sensors.dart';

Now implement your app the way you want.现在以您想要的方式实现您的应用程序。

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

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