Android Manager之SensorManager---距离传感器

一、获取距离传感器实例:

SensorManager sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
Sensor proximitySensor = sensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);

二、注册传感器事件监听器:

sensorManager.registerListener(this, proximitySensor, SensorManager.SENSOR_DELAY_NORMAL);

对registerListener方法的三个参数说明如下:

第一个参数 Listener:一个SensorEventListener,这里我们的实例类实现了SensorEventListener,所以我们传递了this。

第二个参数 Sensor:一个传感器,这里我们传递我们的距离传感器实例proximitySensor。

第三个参数 samplingPeriodUs(采样周期):这里我们传递SensorManager.SENSOR_DELAY_NORMAL

 

三、重写onSensorChanged和onAccuracyChanged方法:

@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
@Override
public void onSensorChanged(SensorEvent event) {
    if (isHeadset) {
        return;
    }
    float[] its = event.values;
    System.out.println("its[0]:" + its[0]);
    if (its[0] == 0.0) {  //贴近手机
        switchSpeaker(false);
        tv_on_speakerphone.setTextColor(Color.parseColor("#62696C"));
        tv_on_speakerphone.setCompoundDrawablesRelativeWithIntrinsicBounds(0, R.drawable.on_speakerphone, 0, 0);
        tv_on_speakerphone.setTag(false);
    } else {  //远离手机
        switchSpeaker(true);
        tv_on_speakerphone.setTextColor(Color.parseColor("#D6E3F4"));
        tv_on_speakerphone.setCompoundDrawablesRelativeWithIntrinsicBounds(0, R.drawable.on_speakerphone_highlight, 0, 0);
        tv_on_speakerphone.setTag(true);
    }
}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {}

备注:我们使用距离传感器实现的功能是:

当没有插耳机时,通过传感器判断距离,从而自动打开/关闭扬声器。

四、当我们不使用的时候,取消注册:

sensorManager.unregisterListener(this);

备注:其他传感器的时候,基本上也是按照这四步走。

题外:我们在使用传感器的时候,是使用了SensorManager。我们可以发现Android设计了各种Manager,比如我们进行网络处理时候用到的ConnectivityManager,我们进行wifi相关操作时用到的WifiManager,我们进行音频相关操作时用到的AudioManager。那么Android中为什么要设计各种manager呢?为什么要有这样的架构呢?

我们拿使用SensorManager举例,我们在使用传感器的时候,我们是先获取SensorManager,然后对这个SensorManager进行操作,对我们APP而言,只有manager是可见的。

由于服务进程与应用是运行在不同的进程中,应用必须和服务建立进程间通信,这就为应用开发

增加了许多繁琐的细节,为此,引进Manager机制达到了暴露接口同时又隐藏共享服务的目的。

这样的做法有助于application层的开发人员更加方便地进行开发。 

### HID Sensor Accel 3D Information HID (Human Interface Device) sensors provide a standardized way to interact with various types of input devices including accelerometers that can measure three-dimensional acceleration. For the specific case of 3D acceleration sensors, these are often used in applications requiring motion sensing such as gaming controllers, fitness trackers, and more. The Linux kernel supports HID-based sensors through dedicated drivers which allow for interfacing with hardware components like 3D accelerometers. The `hid-sensor-accel-3d` module is responsible for handling data from triaxial accelerometer sensors connected via USB or other interfaces supported by HID standards[^1]. #### Implementation Details To implement support for a 3D acceleration sensor within an operating system using the HID protocol: - **Driver Registration**: A device driver registers itself with the HID subsystem indicating its capability to manage certain classes of devices. - **Data Acquisition**: Once registered, this driver receives raw data packets sent periodically over the communication channel established between host and peripheral. - **Processing & Calibration**: Raw values undergo processing steps involving calibration routines before being exposed to user space applications. Here’s how one might load the necessary modules into the running kernel: ```bash sudo modprobe hid_sensor_accel_3d ``` For development purposes, developers may also wish to explore source code related to HID sensor implementations found under `/drivers/hid/`. This includes files defining structures, function prototypes, and actual logic required to interpret incoming reports according to specifications outlined in relevant documentation provided by manufacturers or standard bodies governing HID protocols. #### Driver Configuration Options When configuring the kernel build environment, options pertaining specifically to HID sensor support should be enabled if not already present. These settings ensure proper compilation of associated binaries alongside core functionalities offered by the platform. Options include enabling general HID sensor framework along with individual entries corresponding to each type of physical quantity measured—such as temperature, humidity, pressure—and movement-related metrics captured by gyroscopes and magnetometers besides linear displacement gauged by accelerometers. ```makefile CONFIG_HID_SENSOR_ACCEL_3D=y ``` This configuration directive instructs the compiler during assembly phase to incorporate object files pertinent to managing interactions with compatible peripherals offering multi-axis velocity change detection capabilities.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值