本章将会介绍:
传感器的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: