GPS获取部分,我将经纬度的值17位double转变成long型,(乘以100000000000000)并且分为高32bit,和低32bit。然后进行传送,其实总共需要7个byte
暂时把代码贴上来吧,函数估计是不会变了,但是我还想把类进行整理下,感觉设计地不太合理。
代码:
View Code
1 package com.sensor.gps; 2 3 import android.app.Activity; 4 import android.content.Context; 5 import android.content.Intent; 6 import android.location.Criteria; 7 import android.location.Location; 8 import android.location.LocationListener; 9 import android.location.LocationManager; 10 import android.os.Bundle; 11 import android.provider.Settings; 12 import android.util.Log; 13 import android.widget.Toast; 14 15 public class GPSLocationAndroid{ 16 private static final long PREC_MOV_DIGTIPLY = (long) Math.pow(10, 14); 17 18 private static final int MOV_DIG = 32; 19 /** 20 * 更新时间 21 */ 22 private long updateTime=60000; 23 /** 24 * 调用的Activity 25 */ 26 private Activity myActivity; 27 28 /** 29 * 位置 Location 30 */ 31 private Location location = null; 32 33 34 private LocationManager locationManager; 35 /** 36 * 单一实例 37 */ 38 private static GPSLocationAndroid gpsLocation=null; 39 /** 40 * 需要有一个Activity 41 */ 42 private GPSLocationAndroid(Activity activity){ 43 myActivity=activity; 44 locationManager = (LocationManager) myActivity.getSystemService(Context.LOCATION_SERVICE); 45 checkGPSSettings(); 46 gpsInitial(); 47 } 48 49 public static GPSLocationAndroid getInstance(Activity activity){ 50 if(gpsLocation == null){ 51 gpsLocation = new GPSLocationAndroid(activity); 52 } 53 54 return gpsLocation; 55 } 56 /** 57 * 检查GPS设置 58 */ 59 private void checkGPSSettings() { 60 if (locationManager.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER)) { 61 Toast.makeText(myActivity, "GPS模块正常", Toast.LENGTH_SHORT).show(); 62 return; 63 } 64 Toast.makeText(myActivity, "请开启GPS!", Toast.LENGTH_SHORT).show(); 65 Intent intent = new Intent(Settings.ACTION_SECURITY_SETTINGS); 66 67 myActivity.startActivityForResult(intent,0); //此为设置完成后返回到获取界面 68 69 } 70 /** 71 * 初始化 72 */ 73 private void gpsInitial(){ 74 75 //查找到服务信息 76 //criteria获取精确度的方法 77 Criteria criteria=new Criteria(); 78 //位置解析的精度,高或低,参数 79 criteria.setAccuracy(Criteria.ACCURACY_FINE); 80 //是否提供海拔高度信息 81 criteria.setAltitudeRequired(true); 82 //是否提供方向信息 83 criteria.setBearingRequired(false); 84 //是否允许运营商计费 85 criteria.setCostAllowed(true); 86 //电池消耗,无,低,中,高参数。power——low为低 87 criteria.setPowerRequirement(Criteria.POWER_LOW); 88 //获取gps的信息 89 String provider=locationManager.getBestProvider(criteria, true); 90 //通过gps获取信息 91 location=locationManager.getLastKnownLocation(provider); 92 // 设置监听器,updateTime更新一次位置坐标,不考虑位置的变化。最后引用locationlistener并且实现次方法 93 locationManager.requestLocationUpdates(provider, updateTime, 0, 94 new LocationListener(){ 95 //坐标改变时触发此函数 96 @Override 97 public void onLocationChanged(Location location) { 98 GPSLocationAndroid.this.location = location; 99 } 100 //provider被disable时触发此函数,比如gps被关闭 101 @Override 102 public void onProviderDisabled(String provider) { 103 104 } 105 //provide被enable时触发此函数,比如gps被打开 106 @Override 107 public void onProviderEnabled(String provider) { 108 109 } 110 //provider的转态在:可用,暂时不可用和无服务3个状态直接切换时触发此函数 111 @Override 112 public void onStatusChanged(String provider, 113 int status, Bundle extras) { 114 115 } 116 }); 117 } 118 /** 119 * 设置更新时间 120 * @param time 是long 型的 121 */ 122 public void setUpdateTime(long time){ 123 updateTime = time; 124 } 125 /** 126 * 纬度 127 * @return 128 */ 129 public double getLatitude(){ 130 return location.getAltitude(); 131 } 132 133 /** 134 * 经度 135 */ 136 public double getLongitude(){ 137 return location.getLongitude(); 138 } 139 140 /** 141 * 纬度 高32位 142 * @return 143 */ 144 public int getLatitudeHigh(){ 145 return getHighPartOtherDig(location.getLatitude()); 146 } 147 /** 148 * 纬度低32位 149 * @return 150 */ 151 public int getLatitudeLow(){ 152 return getLowPart32Dig(location.getLatitude()); 153 } 154 /** 155 * 经度高32位 156 */ 157 public int getLongitudeHigh(){ 158 return getHighPartOtherDig(location.getLongitude()); 159 } 160 /** 161 * 经度低32位 162 */ 163 public int getLongitudeLow(){ 164 165 return getLowPart32Dig(location.getLongitude()); 166 } 167 168 /** 169 * 重新获取经纬度 170 */ 171 public static double recoverAltiOrLongi(int h,int l){ 172 long t1= h; 173 Log.e("h",""+h); 174 t1 = t1<<32; 175 Log.e("纬度移位h:",Long.toBinaryString(t1)); 176 long t2 = l; 177 Log.e("l",""+l); 178 Log.e("纬度移位l:",Long.toBinaryString(t2)); 179 long mask = 0xffffffffl; //注意得加上l 180 Log.e("纬度mask值:",""+mask+""); 181 Log.e("纬度移位mask:",Long.toBinaryString(mask)); 182 long t3 = t2 & mask; 183 Log.e("纬度移位t3:",Long.toBinaryString(t3)); 184 double r = (double)(t3+t1) /PREC_MOV_DIGTIPLY; 185 return r; 186 } 187 188 /** 189 * 返回 经度,纬度,高度 190 */ 191 @Override 192 public String toString() { 193 // TODO Auto-generated method stub 194 return "经度:"+location.getLongitude()+"\n纬度:"+location.getLatitude()+"\n高度:"+location.getAltitude(); 195 } 196 197 /** 198 * 供测试而用 199 */ 200 public void test(){ 201 Log.e("gps",toString()); 202 203 Log.e("gps","纬度高位:"+Integer.toBinaryString(getLatitudeHigh())); 204 Log.e("gps","纬度低位:"+Integer.toBinaryString(getLatitudeLow())); 205 Log.e("gps","经度高位:"+getLongitudeHigh()); 206 Log.e("gps","经度低位:"+getLongitudeLow()); 207 Toast.makeText(myActivity, toString(), Toast.LENGTH_SHORT).show(); 208 Toast.makeText(myActivity, "纬度高位:"+getLatitudeHigh(), Toast.LENGTH_SHORT).show(); 209 Toast.makeText(myActivity, "纬度低位:"+getLatitudeLow(), Toast.LENGTH_SHORT).show(); 210 Toast.makeText(myActivity, "经度高位:"+getLongitudeHigh(), Toast.LENGTH_SHORT).show(); 211 Toast.makeText(myActivity, "经度低位:"+getLongitudeLow(), Toast.LENGTH_SHORT).show(); 212 } 213 /** 214 * 获取double 的低(MOV_DIG)位 215 * @param a 216 * @return 217 */ 218 private int getLowPart32Dig(double a){ 219 long t= (long)(a*PREC_MOV_DIGTIPLY); 220 int b =(int)( t ); 221 return b; 222 } 223 /** 224 * 获取double 的高32位 225 * @param a 226 * @return 227 */ 228 private int getHighPartOtherDig(double a){ 229 230 long t= (long)(a*PREC_MOV_DIGTIPLY); 231 int b =(int)( t >> MOV_DIG); 232 return b; 233 } 234 /** 235 * 将拆分的两位数,恢复成double 236 * @param h 高 237 * @param l 低 238 * @return 239 */ 240 private double recoverFromSplit(int h,int l){ 241 long t1= h; 242 Log.e("h",""+h); 243 t1 = t1<<32; 244 Log.e("纬度移位h:",Long.toBinaryString(t1)); 245 long t2 = l; 246 Log.e("l",""+l); 247 Log.e("纬度移位l:",Long.toBinaryString(t2)); 248 long mask = 0xffffffffl; //注意得加上l 249 Log.e("纬度mask值:",""+mask+""); 250 Log.e("纬度移位mask:",Long.toBinaryString(mask)); 251 long t3 = t2 & mask; 252 Log.e("纬度移位t3:",Long.toBinaryString(t3)); 253 double r = (double)(t3+t1) /PREC_MOV_DIGTIPLY; 254 return r; 255 } 256 }
使用范例:注意在sensor_byte.xml 要添加对应的位数之类。
View Code
1 button1.setOnClickListener(new View.OnClickListener(){ 2 @Override 3 public void onClick(View v) { 4 // TODO Auto-generated method stub 5 //gpsLocation.doWork(); 6 toastShow(gpsLocation.toString()); 7 Log.e("error","what"); 8 gpsLocation.test(); 9 toastShow(""+gpsLocation.recoverAltiOrLongi(gpsLocation.getLatitudeHigh(), gpsLocation.getLatitudeLow())); 10 Log.e("recover",""+gpsLocation.recoverAltiOrLongi(gpsLocation.getLatitudeHigh(), gpsLocation.getLatitudeLow())); 11 } 12 });