博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Sensor传感器源码的阅读与应用开发…
阅读量:4051 次
发布时间:2019-05-25

本文共 5349 字,大约阅读时间需要 17 分钟。

由于公司最近准备推出一款MID电脑,自己想做一款传感器相关的应用内置MID,所以花了一下午时间看了下源码学习了下。其实源码中传感器内容并不多!但是却是相当强大(我阅读的是Android2.2的源码,以2.2为标准),最近推出的2.3源码还没来得及下,据说2.3的传感器也有些改动,但是不大,更多的是方便游戏开发者。废话不说,直接“上菜”,分享下~~~~

Android系统支持多种传感器。应用到各个层次,有的传感器已经在Android的框架中使用,大多数传感器由应用程序中来使用。

一.Android中支持的传感器类型:

  

  

传感器

Java
中的名称

本地接口名称

数值

   加速度

TYPE_ACCELEROMETER

SENSOR_TYPE_ACCELEROMETER

1

   磁场

TYPE_MAGNETIC_FIELD

SENSOR_TYPE_MAGNETIC_FIELD

2

   方向

TYPE_ORIENTATION

SENSOR_TYPE_ORIENTATION

3

陀螺仪

TYPE_GYROSCOPE

SENSOR_TYPE_GYROSCOPE

4

光线(亮度)

TYPE_LIGHT

SENSOR_TYPE_LIGHT

5

压力

TYPE_PRESSURE

SENSOR_TYPE_PRESSURE

6

温度

TYPE_TEMPERATURE

SENSOR_TYPE_TEMPERATURE

7

接近

TYPE_PROXIMITY

SENSOR_TYPE_PROXIMITY

8

二.Android 系统的代码分布情况:

1)传感器系统的java代码

代码路径:framework/base/core/java/android/hardware

    目录中包含了Camera Sensor两部分,Sensor部分的内容为Sensor*.java 文件。

2)传感器系统的JNI部分

代码路径: framework/base/core/jni/android_hardware_SensorManager.cpp

   本部分提供了android.hardware.Sensor.Manager 类的本质支持。

3)传感器系统硬件层实现的接口

头文件路径:hardware/libhardware/include/hardware/sensors.h

传感器系统的硬件抽象层需要各个系统根据sensors.h中定义的接口去实现

Sensor部分的内容还包含了底层部分的驱动和硬件抽象层,以及上层对Sensor的调用部

三.Android的Sensor源码解析:

  Android中的Sensor的主要文件为:

    Sensor.java              单一传感器描述文件

    SensorEvent.java         传感器系统的时间类

    SensorEventListener.java  传感器监听事件(是一个接口)

    SensorListener.java       传感器监听(接口)

    SensorManager.java        传感器的核心管理类

  Sensor.java中定义的是传感器常量的一些类型,如public static final TYPE_MAGNETIC_FIELD=2;

            等,具体参数参照传感器类型(图一)

    SensorManager.java   

          public Sensor getDefaultSensor(int type){获得默认的传感器}

          public List<Sensor> getSensorList(int type) {获得传感器列表}

          public boolean registerListener(SensorListener listener, int sensors) {

        return registerListener(listener, sensors, SENSOR_DELAY_NORMAL);
               注册监听事件

          public void unregisterListener(SensorListener listener, int sensors) {注销监听事件}

   时间关系,源码不逐一说了,大家自己有下个源码看下,如果没有源码的,给我个邮箱我给大家发这部分代码,直接上个简单的DEMO供大家认识下,好像这块的代码,在IBM的一个网站上也能找到!

四。程序代码

1)SensorActivity.java代码

package com.sensor;

import android.app.Activity;
import android.hardware.SensorEventListener;
import android.hardware.SensorListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class SensorActivity extends Activity implements SensorListener{

final String tag = "SensorActivity";
    SensorManager sm = null;

TextView xViewA = null;

TextView yViewA = null;
TextView zViewA = null;
TextView xViewO = null;
TextView yViewO = null;
TextView zViewO = null;
   
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        sm=(SensorManager)getSystemService(SENSOR_SERVICE);
        xViewA = (TextView) findViewById(R.id.xbox);
        yViewA = (TextView) findViewById(R.id.ybox);
        zViewA = (TextView) findViewById(R.id.zbox);
        xViewO = (TextView) findViewById(R.id.xboxo);
        yViewO = (TextView) findViewById(R.id.yboxo);
        zViewO = (TextView) findViewById(R.id.zboxo);
        
        
        
        
    }

@Override

public void onAccuracyChanged(int sensor, int accuracy) {
  // TODO Auto-generated method stub
  Log.d(tag,"onAccuracyChanged: " + sensor + ", accuracy: " + accuracy);
}

@Override

public void onSensorChanged(int sensor, float[] values) {
  // TODO Auto-generated method stub
    synchronized (this) {
             Log.d(tag, "onSensorChanged: " + sensor + ", x: " + values[0] + ", y: " + values[1] + ", z: " + values[2]);
             if (sensor == SensorManager.SENSOR_ORIENTATION) {
              xViewO.setText("Orientation X: " + values[0]);
              yViewO.setText("Orientation Y: " + values[1]);
              zViewO.setText("Orientation Z: " + values[2]);
             }
             if (sensor == SensorManager.SENSOR_ACCELEROMETER) {
              xViewA.setText("Accel X: " + values[0]);
              yViewA.setText("Accel Y: " + values[1]);
              zViewA.setText("Accel Z: " + values[2]);
                       
         }
  
}
  @Override
     protected void onResume() {
         super.onResume();
         sm.registerListener(this,
                 SensorManager.SENSOR_ORIENTATION |
           SensorManager.SENSOR_ACCELEROMETER,
                 SensorManager.SENSOR_DELAY_NORMAL);
     }
     
     @Override
     protected void onStop() {
         sm.unregisterListener(this);
         super.onStop();
       
     
}

   

 

2)main.xml  布局文件(简单的放些TextView)

   <?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    androidrientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    />
<TextView  
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Accelerometer"
    />
<TextView  
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="X Value"
    android:id="@+id/xbox"
    />
<TextView  
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Y Value"
    android:id="@+id/ybox"
    />
<TextView  
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Z Value"
    android:id="@+id/zbox"
    />   

<TextView  
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Orientation"
    />
<TextView  
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="X Value"
    android:id="@+id/xboxo"
    />
<TextView  
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Y Value"
    android:id="@+id/yboxo"
    />
<TextView  
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Z Value"
    android:id="@+id/zboxo"
    />   
</LinearLayout>

   五:在模拟器开发测试Sensor要注意,必须要装个传感器插件,才能看到效果,可能有部分手机硬件驱动是不支持Sensor的,不过市面上流行的品牌手机一般都支持!

        抽空首次整理做的教程,有不好的地方,不吝指正!

转载地址:http://tesci.baihongyu.com/

你可能感兴趣的文章
环境分支-git版本管理
查看>>
uni-app 全局变量
查看>>
instanceof 的原理是什么
查看>>
js判断空对象的几种方法
查看>>
var/let/const区别
查看>>
函数式柯里化的理解?
查看>>
时间戳转化为年月日时分秒
查看>>
配置ssh公钥
查看>>
git clone拉代码的时候出现permission denied 没有权限的问题解决
查看>>
前端-vue-文件上传(图片、word,ppt,pdf,excel,txt等文件流)
查看>>
word,PDF,excel、ppt等文件上传,视频上传查看等
查看>>
java 不用递归写tree
查看>>
springboot2 集成Hibernate JPA 用 声明式事物
查看>>
fhs-framework jetcache 缓存维护之自动清除缓存
查看>>
SpringBoot 动态编译 JAVA class 解决 jar in jar 的依赖问题
查看>>
fhs_framework springcloud使用统一的控制器来接收rpc调用请求教程,无需每个rpc接口都写控制器
查看>>
fhs-framework springboot mybatis 解决表关联查询问题的关键方案-翻译服务
查看>>
Springboot + easyui + mybatis 高级搜索功能实现
查看>>
k8s 踩坑笔记
查看>>
SpringCloud Seata Nacos 整合教程和坑
查看>>