本章将会介绍:

传感器的API

加速器编程,设备的方向,近场检测

网络编程

蓝牙编程

上述的技术的应用场景很多,比如:

1.检测当前的网络是否可用,并提醒用户,检测当前的网络类型,比如Wifi、3G、EDGE网络。

2.加速器可以用来提供随机种子,比如摇晃设备时随机访问数据源中的数据。

3.用户水平和垂直放置设备时应用提供不同的视图。

传感器

android设备常见的硬件有:加速器,罗盘,麦克风,陀螺仪等。

Android的Sensor类抽象了所有的传感器设备。实际的传感器有厂商,名称,精确度,范围和类型的属性。

通过SensorManager可以获得传感器的引用:

String service_name = Context.SensorService;
sensManager = (SensorManager)GetSystemService(service_name);

Android.Hardware.SensorType枚举定义了常见的传感器类型:

Accelerometer 可获得x、y、z轴的加速度,单位为米每二次方秒.
Gyroscope 可获得x、y、z轴的角度.
Light可用来指定屏幕的亮度,以lux为单位,对比周围可见光.
MagneticField 获得设备所在点的磁场强度.
Orientation 可获得三轴的角度,以度为单位.
Pressure 获得设备周围的压强.
Proximity 获得设备与目标之间的距离,以厘米为单位。通常是用来检测设备与用户的耳朵之间的距离的。靠近人耳时无需输入则屏幕关闭.
Temperature 获得设备的温度,以摄氏温度为单位.
All 获得平台上所有传感器.

有两种方式获得Sensor类:

defSensor = sensManager.GetDefaultSensor(SensorType.Accelerometer);//可获取默认的该类的传感器

IList<Sensor> accSensors = sensorManager.GetSensorList(SensorType.Accelerometer); //可获取该类的所有传感器

使用传感器分三步:

1. 注册:

sensManager.RegisterListener(this, defSensor, SensorDelay.Ui); //第一个参数是处理值的类,第二个参数是传感器实例,第三个参数是传感器参数的采样率

采样率由枚举类SensorDelay来决定:

Fastest最快.
Game适合游戏.
Normal 默认的采样率.
Ui 更新UI所用的采样率.(最低)

2.处理: 实现接口类 ISensorEventListener来处理传感器的值,如果一个Activity要处理传感器的值,可以定义如下,实现OnSensorChanged 和 OnAccuracyChanged接口方法。OnAccuracyChanged用来处理精度的变化,由Android.Hardware.SensorStatus.Accuracy枚举来决定精度:

AccuracyLow 低精度,需要校正.
AccuracyMedium 中等精度.
AccuracyHigh 高精度.
Unreliable 不可靠的精度.

public class Activity1 : Activity, Android.Hardware.ISensorEventListener

{

public void ISensorEventListener.OnSensorChanged(SensorEvent e)
{
    if ( e.Sensor.Type == SensorType.Accelerometer){
    var calibrationValue = SensorManager.StandardGravity;
    var mVals = e.Values;
    var x = mVals[0];
    var y = mVals[1];
    var z = mVals[2];
    var SumOfSq = Math.Pow(x, 2) + Math.Pow(y, 2) + Math.Pow(z, 2);
        var mag = Math.Pow(SumOfSq, .5) - calibrationValue;
        RunOnUiThread(() =>
            tv.Text = "Acceleration (g): " + mag.ToString()
        );
    }
}
public void ISensorEventListener.OnAccuracyChanged(Sensor sensor, int accuracy)
{
    if (sensor.Type == Android.Hardware.SensorType.Accelerometer)
    {
        if (Android.Hardware.SensorStatus.AccuracyHigh ==
(Android.Hardware.SensorStatus)accuracy)
        {

}
    }
}

}

3.注销

sensManager.UnregisterListener(this, defSensor);

下面详细介绍各种传感器返回的参数:

Accelerometer: value[0]: Lateral (x direction)
value[1]: Longitudinal (z direction)
value[2]: Vertical (y direction)

Gyroscope: The gyroscope returns three values — device orientation in degrees along three axes:
value[0]: Azimuth
value[1]: Pitch
value[2]: Roll

Light: The light sensor returns the measurement of illumination. Only one value is returned. It is obtained by value[0]

Magnetic Field: The magnetic field that is returned is measured in three directions, and the values are in microteslas:

value[0] : Lateral (x direction)
value[1] : Longitudinal (z direction)
value[2] : Vertical (y direction)

Orientation: The device's orientation is returned in degrees along three axes based on the following values:

value[0]: Azimuth
value[1]: Roll
value[2]: Pitch
Pressure: The pressure is returned in value[0]. This would be useful in an engineering scenario. The pressure is measured in hPa.
Proximity: The proximity is measured in centimeters and is returned in value[0].
Temperature: The temperature is measured in Celsius and is returned in value[0].

下面就是几个使用传感器的例子了:

public void ISensorEventListener.OnSensorChanged(SensorEvent e)
{
    if ( e.Sensor.Type == SensorType.Accelerometer){
        var mVals = e.Values;
        var x = mVals[0];
        var y = mVals[1];
        var z = mVals[2];
        var SumOfSq = Math.Pow(x, 2) + Math.Pow(y, 2) + Math.Pow(z, 2);
        var mag = Math.Pow(SumOfSq, .5) - SensorManager.StandardGravity
        if (MaxAccel == 0.0)
        {
            MaxAccel = mag;
        }
        if (mag > MaxAccel)
        {
            MaxAccel = mag;
        }
        RunOnUiThread(() =>
            tv.Text = "Acceleration (m/sˆ2): " + MaxAccel.ToString()
        );
    }
}

震动

Mono for Android has the class Android.OS.Vibrator, 使用震动需要两步:

1. 在AndroidManifest.xml 文件中设置权限允许应用震动设备:
<uses-permission android:name="android.permission.VIBRATE" />

2. 获取震动类并调用震动方法:
Vibrator vibrator = (Vibrator)GetSystemService(Context.VibratorService);
vibrator.Vibrate(pattern, -1);

Note two interesting things in this code:

mono for android读书笔记之硬件编程(转)的更多相关文章

  1. mono for android读书笔记之真机调试(转)

    调试环境: 1.软件:monodevelop v3.0.3.5 2.硬件:华为C8650s手机一部,数据线一根,thinkpad e420笔记本电脑一台 调试的应用程序有一个Activity,Acti ...

  2. Java并发编程的艺术读书笔记(1)-并发编程的挑战

    title: Java并发编程的艺术读书笔记(1)-并发编程的挑战 date: 2017-05-03 23:28:45 tags: ['多线程','并发'] categories: 读书笔记 --- ...

  3. Java并发编程的艺术读书笔记(2)-并发编程模型

    title: Java并发编程的艺术读书笔记(2)-并发编程模型 date: 2017-05-05 23:37:20 tags: ['多线程','并发'] categories: 读书笔记 --- 1 ...

  4. 《Essential C++》读书笔记 之 C++编程基础

    <Essential C++>读书笔记 之 C++编程基础 2014-07-03 1.1 如何撰写C++程序 头文件 命名空间 1.2 对象的定义与初始化 1.3 撰写表达式 运算符的优先 ...

  5. Android读书笔记一

    通过本章的学习真实体会到“移植”的概念:为特定设备定制Android的过程,但是移植的过程中开发最多的就是支持各种硬件设备的Linux驱动程序,本章对Android和Linux做了总体介绍.接着介绍了 ...

  6. 读书笔记 |Google C++编程风格指南

    Google C++编程风格指南 ## 0. 背景 每一个C++程序员都知道,C++具有很多强大的语言特性,但这种强大不可避免的导致它的复杂,这种复杂会使得代码更易于出现bug.难于阅读和维护. 本指 ...

  7. 读书笔记_python网络编程3_(1)

    0.前言 代码目录: https://github.com/brandon-rhodes/fopnp/tree/m/py3 0.1.网络实验环境:理解客户端与服务器是如何通过网络进行通信的 每台机器通 ...

  8. SpringBoot实战派读书笔记---响应式编程

    1.什么是WebFlux? WebFlux不需要Servlet API,在完全异步且无阻塞,并通过Reactor项目实现了Reactor Streams规范. WebFlux可以在资源有限的情况下提高 ...

  9. 【读书笔记】《编程珠玑》第一章之位向量&位图

    此书的叙述模式是借由一个具体问题来引出的一系列算法,数据结构等等方面的技巧性策略.共分三篇,基础,性能,应用.每篇涵盖数章,章内案例都非常切实棘手,解说也生动有趣. 自个呢也是头一次接触编程技巧类的书 ...

随机推荐

  1. s16 计算机网络基础

    交换机设备说明 1)交换机设备说明 交换机概念:解决多台主机在一个网络里面通讯的需求 主机身份标识信息:称为叫做mac地址 交换机通讯的网络范围:称为叫做一个局域网 交换机传输数据问题: 01.会有广 ...

  2. Freedom DownTime

    Storyline Computer hackers are being portrayed as the newest brand of terrorists. This is a story of ...

  3. PHP(八)数组

  4. Hello_Area_Description 任务三:Project Tango采集区域描述数据

    Permission Dialogs for Users in Java在Java中用户使用的权限对话框 Tango works by using visual cues from the devic ...

  5. 目前主流编译器对C++11特性的支持情况

    目前主流编译器对C++11特性的支持情况 1. GCC编译器(从编译器GCC4.8.X的版本完全支持) (1)目前C++11特性,之前成为C++0X特性,从GCC4.3的后续版本中逐步对C++11进行 ...

  6. HDU 5974 A Simple Math Problem(数论+结论)

    Problem Description Given two positive integers a and b,find suitable X and Y to meet the conditions ...

  7. WinExec打开exe文件

    1,WinExec():   WinExec主要运行EXE文件,不能运行其他类型的文件.不用引用特别单元.   原型:UINT WinExec(exePath,ShowCmd)   示例,我想要用记事 ...

  8. Android下拉刷新控件android-Ultra-Pull-To-Refresh 使用

    一.gitHub地址及介绍 https://github.com/liaohuqiu/android-Ultra-Pull-To-Refresh android-Ultra-Pull-To-Refre ...

  9. SQL Server触发器的基本使用

    sqlserver_SQL触发器的使用及语法(转自:百度文库) 定义: 何为触发器?在SQL Server里面也就是对某一个表的一定的操作,触发某种条件,从而执行的一段程序.触发器是一个特殊的存储过程 ...

  10. 点滴笔记(二):利用JS对象把值传到后台

    记得以前刚写asp.net 从前台往后台传值 都是var data=A,B,C,D,E; 循环添加用逗号隔开 后台还要被测试测出只输入,就错了 哈哈..后来用✈◆类似的符号隔开 不是长久之计... 现 ...