際際滷

際際滷Share a Scribd company logo
Smart Robot Contest 2013 
L畉P TRNH ANDROID 
 
Bi 03: 
LLooccaattiioonn && SSeennssoorr PPrrooggrraammmmiinngg
N畛i dung bi h畛c 
 Location Programming 
 Sensor Programming
N畛i dung bi h畛c 
 Location Programming 
 Sensor Programming
Location Programming 
 Gi畛i thi畛u Android Location Service. 
 X叩c 畛nh v畛 tr鱈 hi畛n t畉i c畛a thi畉t b畛. 
 Theo d探i s畛 di chuy畛n c畛a thi畉t b畛. 
 Proximity Alerts.
Location Programming 
 Gi畛i thi畛u Android Location Service. 
 X叩c 畛nh v畛 tr鱈 hi畛n t畉i c畛a thi畉t b畛. 
 Theo d探i s畛 di chuy畛n c畛a thi畉t b畛. 
 C畉nh b叩o khi c坦 s畛 di chuy畛n 畉n g畉n.
Android Location Service 
 畛 cung c畉p th担ng tin v畛 v畛 tr鱈, Android s畛 
d畛ng location provider. C叩c lo畉i location 
provider nh動 sau: 
 GPS Provider 
 Network Provider
Android Location Service 
 GPS Provider: 
 S畛 d畛ng GPS (Global positioning system) 畛 gi炭p 
thi畉t b畛 Android x叩c 畛nh v畛 tr鱈 c畛a n坦. 
 Tuy nhi棚n, thi畉t b畛 di 畛ng r畉t kh坦 lm vi畛c tr畛c 
ti畉p v畛i GPS (th畛i gian truy畛n th担ng tin v畛 tr鱈 c叩c 
v畛 tinh 畉n thi畉t b畛). C叩c thi畉t b畛 di 畛ng hi畛n 畉i 
th動畛ng s畛 d畛ng A-GPS (th担ng tin v畛 tr鱈 c叩c v畛 
tinh truy畛n qua m畉ng vi畛n th担ng 畉n thi畉t b畛) 
ho畉c S-GPS (cho ph辿p s畛 d畛ng 畛ng th畛i GPS 
radio v cellular network radio).
Android Location Service 
 GPS Provider: 
 H畉n ch畉: 
 GPS provider h畉u nh動 kh担ng th畛 ho畉t 畛ng trong nh 
hay 畛 ngoi tr畛i nh畛ng v湛ng m b畉u tr畛i b畛 che khu畉t 
nh動 trong r畛ng r畉m ch畉ng h畉n. 
 N畉u tr棚n 動畛ng i c畛a s坦ng b畛 nhi畛u v畉t c畉n, s坦ng ph畉i 
ph畉n x畉 nhi畛u l畉n  tng 畛 di 動畛ng i  x叩c 畛nh 
v畛 tr鱈 b畛 sai (multipath error).
Android Location Service 
 Network Provider: 
 S畛 d畛ng Wireless Network Access Point. 
 S畛 d畛ng Cell ID.
Android Location Service 
 Network Provider: 
 S畛 d畛ng Wireless Network Access Point: 
 S畛 d畛ng th担ng tin Wi-Fi (MAC c畛a AP v c動畛ng 畛 t鱈n 
hi畛u nh畉n b畛i AP) 畛 truy v畉n Google location service. 
 i畛m m畉nh c畛a ph動董ng ph叩p ny l c坦 th畛 x叩c 畛nh v畛 
tr鱈 c畛a thi畉t b畛 畛 nh畛ng n董i m GPS Provider kh担ng th畛 
cung c畉p. 
 i畛m y畉u: 
 Y棚u c畉u ph畉i c坦 m畉ng Wi-Fi, AP ph畉i c畉u h狸nh SSID ho畉c 
SSID kh担ng k畉t th炭c b畉ng _nomap. 
 Thay 畛i v畛 tr鱈 c畛a AP.
Android Location Service 
 Network Provider: 
 S畛 d畛ng Cell ID: 
 S畛 d畛ng Cell ID 畛 truy v畉n Google location service v畛 
th担ng tin v畛 tr鱈 c畛a thi畉t b畛. 
 V畛 i畛m m畉nh v i畛m y畉u th狸 ph動董ng ph叩p ny kh叩 
gi畛ng v畛i ph動董ng ph叩p s畛 d畛ng Wireless Network 
Access Points. Tuy nhi棚n th狸 v畛 tr鱈 c畛a c叩c c畛t ph叩t 
s坦ng (cell tower) r畉t hi畉m thay 畛i so v畛i c叩c access 
point.
Location Programming 
 Gi畛i thi畛u Android Location Service. 
 X叩c 畛nh v畛 tr鱈 hi畛n t畉i c畛a thi畉t b畛. 
 Theo d探i s畛 di chuy畛n c畛a thi畉t b畛. 
 Promixity Alerts.
X叩c 畛nh v畛 tr鱈 hi畛n t畉i c畛a thi畉t b畛 
 C叩c thnh ph畉n c畛a Android Location API. 
 X叩c 畛nh location provider. 
 C畉p nh畉t th担ng tin location. 
 V鱈 d畛 minh ho畉.
X叩c 畛nh v畛 tr鱈 hi畛n t畉i c畛a thi畉t b畛 
 C叩c thnh ph畉n c畛a Android Location API: 
LocationManager 
LocationProvider 
Th担ng b叩o+Location 
Location 
畛c th担ng tin y棚u c畉u Criteria 
畛i v畛i LocationProvider 
LocationListener 
Cho ph辿p ho畉t 畛ng 
Sinh ra S畛 d畛ng
X叩c 畛nh v畛 tr鱈 hi畛n t畉i c畛a thi畉t b畛 
 X叩c 畛nh location provider: 
Location Provider Permission y棚u c畉u S畛 d畛ng Pin 畛 ch鱈nh x叩c 
GPS Provider android.permission. 
ACCESS_FINE_LOCATION 
ho畉c android.permission. 
ACCESS_COARSE_LOCATION 
S畛 d畛ng nhi畛u pin 
nh畉t so v畛i c叩c 
provider kh叩c 
Ch鱈nh x叩c nh畉t 
trong c叩c provider 
Network Provider android.permission. 
ACCESS_COARSE_LOCATION 
S畛 d畛ng 鱈t pin h董n 
GPS provider 
t ch鱈nh x叩c h董n 
GPS provider. 
Passive Provider android.permission. 
ACCESS_FINE_LOCATION 
Kh担ng x叩c 畛nh Kh担ng x叩c 畛nh.
X叩c 畛nh v畛 tr鱈 hi畛n t畉i c畛a thi畉t b畛 
 C畉p nh畉t th担ng tin location: C坦 hai ph動董ng 
ph叩p 
 S畛 d畛ng LocationListener v hi畛n th畛c ph動董ng th畛c 
onLocationChanged 
 S畛 d畛ng Broadcast Receiver 畛 nh畉n broadcast 
Intent khi location 動畛c c畉p nh畉t.
Hi畛n th畛c 畛ng d畛ng demo 
 Hi畛n th畛c interface LocationListener 
 L畉y handle c畛a LocationManager 
 Y棚u c畉u c畉p nh畉t th担ng tin location. 
 D畛n d畉p
Hi畛n th畛c 畛ng d畛ng demo 
 Hi畛n th畛c interface LocationListener: 
Ph動董ng th畛c M担 t畉 
onLocationChanged Th担ng b叩o th担ng tin location 達 
c畉p nh畉t. 
onProviderDisabled & 
onProviderEnabled 
Ng動畛i d湛ng disable/enable 
location provider t畛 setting menu. 
onStatusChanged Location provider on/off. 
public void onLocationChanged(Location arg0) { 
txtViDo.setText(String.valueOf(arg0.getLatitude())); 
txtKinhDo.setText(String.valueOf(arg0.getLongitude())); 
txtProvider.setText(String.valueOf(arg0.getProvider())); 
long timeToFix = SystemClock.uptimeMillis() - uptimeAtResume; 
txtTTF.setText(String.valueOf(timeToFix / 1000)); 
lblDCXDonVi.setVisibility(View.VISIBLE); 
lblTTFDonVi.setVisibility(View.VISIBLE); 
}
Hi畛n th畛c 畛ng d畛ng demo 
 L畉y handle c畛a LocationManager: 
private LocationManager lm; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.activity_main); 
lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
txtViDo = (TextView)findViewById(R.id.txtViDo); 
txtKinhDo = (TextView)findViewById(R.id.txtKinhDo); 
txtDoChinhXac = (TextView)findViewById(R.id.txtDoChinhXac); 
txtTTF = (TextView)findViewById(R.id.txtTTF); 
txtProvider = (TextView)findViewById(R.id.txtProvider); 
txtEnabledProvider = (TextView)findViewById(R.id.txtEnabledProvider); 
lblDCXDonVi = (TextView)findViewById(R.id.lblDCXDonVi); 
lblTTFDonVi = (TextView)findViewById(R.id.lblTTFDonVi); 
}
Hi畛n th畛c 畛ng d畛ng demo 
 Y棚u c畉u c畉p nh畉t th担ng tin location: 
 Ch炭ng ta kh担ng th畛 y棚u c畉u Android cung c畉p 
th担ng tin ny ngay l畉p t畛c m ch炭ng ta ch畛 y棚u 
c畉u v nh畉n 動畛c th担ng tin khi no n坦 c坦 th担ng 
qua ph動董ng th畛c 
LocationManager#requestSingleUpdate
Hi畛n th畛c 畛ng d畛ng demo 
protected void onResume() { 
super.onResume(); 
StringBuffer stBuffer = new StringBuffer(); 
Criteria criteria = new Criteria(); 
criteria.setAccuracy(Criteria.ACCURACY_COARSE); 
enabledProviders = lm.getProviders(criteria, true); 
if (enabledProviders.isEmpty()){ 
txtEnabledProvider.setText(""); 
}else{ 
for (String enabledProvider : enabledProviders) 
{ 
stBuffer.append(enabledProvider).append(" "); 
lm.requestSingleUpdate(enabledProvider,this,null); 
} txtEnabledProvider.setText(stBuffer); 
} 
uptimeAtResume = SystemClock.uptimeMillis(); 
txtViDo.setText(""); 
txtKinhDo.setText(""); 
txtProvider.setText(""); 
txtDoChinhXac.setText(""); 
txtTTF.setText(""); 
lblDCXDonVi.setVisibility(View.GONE); 
lblTTFDonVi.setVisibility(View.GONE); 
}
Hi畛n th畛c 畛ng d畛ng demo 
 D畛n d畉p : 
@Override 
protected void onPause() { 
super.onPause(); 
lm.removeUpdates(this); 
}
Location Programming 
 Gi畛i thi畛u Android Location Service. 
 X叩c 畛nh v畛 tr鱈 hi畛n t畉i c畛a thi畉t b畛. 
 Theo d探i s畛 di chuy畛n c畛a thi畉t b畛. 
 Promixity Alerts.
Theo d探i s畛 di chuy畛n c畛a thi畉t b畛 
 Thu th畉p d畛 li畛u location. 
 Tr狸nh by d畛 li畛u location.
Theo d探i s畛 di chuy畛n c畛a thi畉t b畛 
 Thu th畉p d畛 li畛u location. 
 Tr狸nh by d畛 li畛u location.
Theo d探i s畛 di chuy畛n c畛a thi畉t b畛 
 Thu th畉p d畛 li畛u location: 
 S畛 d畛ng BrodcastReceiver. 
 S畛 d畛ng Service.
Theo d探i s畛 di chuy畛n c畛a thi畉t b畛 
 Thu th畉p d畛 li畛u location: 
 S畛 d畛ng BroadcastReceiver: 
 T畉o broadcast Intent v畛i action 達 畛nh ngh挑a tr動畛c 
 T畉o PendingIntent t畛 getBroadcast. 
 S畛 d畛ng ph動董ng th畛c 
LocationManager#requestLocationUpdate 畛 ng k箪 
PendingIntent v畛i LocationManager. 
 Hi畛n th畛c BroadcastReceiver 畛 nh畉n broadcast Intent 
動畛c g畛i m畛i khi location 動畛c c畉p nh畉t.
Theo d探i s畛 di chuy畛n c畛a thi畉t b畛 
 Thu th畉p d畛 li畛u location: 
 S畛 d畛ng Service: 
 Hi畛n th畛c Service v LocationListener. 
 Trong ph動董ng th畛c onCreate l畉y handle c畛a 
LocationManager. 
 Trong ph動董ng th畛c onStartCommand s畛 d畛ng ph動董ng 
th畛c requestOnLocationUpdate 畛 ng k箪 listener cho 
LocationManger. 
 Trong onLocationChanged ch炭ng ta l動u l畉i location. 
 Trong ph動董ng th畛c onDestroy ch炭ng ta d湛ng ph動董ng 
th畛c removeUpdate 畛 xo叩 ng k箪.
Theo d探i s畛 di chuy畛n c畛a thi畉t b畛 
 Thu th畉p d畛 li畛u location. 
 Tr狸nh by d畛 li畛u location.
Theo d探i s畛 di chuy畛n c畛a thi畉t b畛 
 Tr狸nh by d畛 li畛u location: 
 Th動畛ng s畛 d畛ng Google Map 畛 tr狸nh by d畛 li畛u 
location thu th畉p 動畛c. 但y l ch畛 畛 t畛 t狸m hi畛u.
Location Programming 
 Gi畛i thi畛u Android Location Service. 
 X叩c 畛nh v畛 tr鱈 hi畛n t畉i c畛a thi畉t b畛. 
 Theo d探i s畛 di chuy畛n c畛a thi畉t b畛. 
 Promixity Alerts.
Promixity Alerts 
 Geocoding 
 Thi畉t l畉p Promixity Alert. 
 畛i 畛ng v畛i Promixity Alert 
O R
Promixity Alerts 
 Geocoding 
 Thi畉t l畉p Promixity Alert. 
 畛i 畛ng v畛i Promixity Alert
Promixity Alerts 
 Geocoding: 
 Chuy畛n t棚n v畛 tr鱈 sang to畉 畛 (kinh 畛, v挑 畛). 
 Class android.location.Geocoder cho ph辿p ch炭ng ta 
th畛c hi畛n geocoding v ng動畛c l畉i. 
 isPresent  ki畛m tra b畉n Android hi畛n t畉i c坦 h畛 tr畛 c叩c 
ph動董ng th畛c geocoding v reverse geocoding hay 
kh担ng. 
 GetLocationFromName tr畉 v畛 danh s叩ch c叩c 畛a i畛m 
ph湛 h畛p v畛i t棚n 動畛c cung c畉p.
Promixity Alerts 
 Geocoding 
 Thi畉t l畉p Promixity Alert. 
 畛i 畛ng v畛i Promixity Alert
Promixity Alerts 
 Thi畉t l畉p Promixity Alert: 
 S畛 d畛ng ph動董ng th畛c 
LocationManager#addPromixityAlert 畛 thi畉t l畉p 
proximity alert: 
 Kinh 畛, v挑 畛. 
 B叩n k鱈nh v湛ng xung quanh. 
 Gi畛i h畉n v畛 th畛i gian. 
 PendingIntent: intent s畉 動畛c send broadcast khi 
proximity alert 動畛c k鱈ch ho畉t 
 S畛 d畛ng ph動董ng th畛c 
LocationManager#removeProximityAlert 畛 hu畛 
ng k箪 1 proximity alert.
Promixity Alerts 
 Geocoding. 
 Thi畉t l畉p Promixity Alert. 
 畛i 畛ng v畛i Promixity Alert.
Promixity Alerts 
 畛i 畛ng v畛i Promixity Alert: Khi proximity alert b畛 
k鱈ch ho畉t th狸 intent trong PendingIntent s畉 動畛c g畛i 
broadcast  Hi畛n th畛c BroadcastReceiver 畛 nh畉n 
intent ny. 
 S畛 d畛ng key 
LocationManager.KEY_PROXIMITY_ENTERING 畛 
filter v nh畉n th担ng tin t畛 Intent.
N畛i dung bi h畛c 
 Location Programming 
 Sensor Programming
Sensor Programming 
 Gi畛i thi畛u 
 Android Sensor Programming API 
 V鱈 d畛 minh ho畉
Sensor Programming 
 Gi畛i thi畛u 
 Android Sensor Programming API 
 V鱈 d畛 minh ho畉
Sensor Programming 
 Gi畛i thi畛u 
 MEMS (Microelectromechanical sensors) l nh畛ng c畉m bi畉n 
c坦 k鱈ch th動畛c c畛c k畛 nh畛 動畛c s畉n xu畉t d畛a tr棚n k畛 thu畉t 
s畉n xu畉t chip m叩y t鱈nh. 
 C叩c lo畉i c畉m bi畉n: 
 Raw sensor cung c畉p d畛 li畛u th担 t畛 thi畉t b畛 v畉t l箪 
 Sensor.TYPE_LIGHT (C畉m bi畉n 叩nh s叩ng) 
 Sensor.TYPE_PROXIMITY (C畉m bi畉n ti畛m c畉n) 
 Sensor.TYPE_PRESSURE (C畉m bi畉n 叩p su畉t) 
 Sensor.TYPE_TEMPERATURE (C畉m bi畉n nhi畛t 畛 thi畉t b畛) 
 Sensor.TYPE_ACCELEROMETER (C畉m bi畉n gia t畛c) 
 Sensor.TYPE_GYROSCOPE (C畉m bi畉n con quay) 
 Sensor.TYPE_MAGNETIC_FIELD (C畉m bi畉n t畛 tr動畛ng) 
 Sensor.TYPE_RELATIVE_HUMIDITY (C畉m bi畉n 畛 畉m) 
 Sensor.TYPE_AMBIENT_TEMPERATURE (CB nhi畛t 畛 m担i tr動畛ng)
Sensor Programming 
 Gi畛i thi畛u 
 C叩c lo畉i c畉m bi畉n: 
 Synthetic sensor (composite, virtual): abstract layer 
gi畛a code v thi畉t b畛 v畉t l箪 
 Sensor.TYPE_ROTATION_VECTOR (CB v辿c t董 quay) 
 Sensor.TYPE_LINEAR_ACCELERATION 
(CB gia t畛c kh担ng tr畛ng l畛c) 
 Sensor.TYPE_GRAVITY (CB tr畛ng l畛c) 
 Sensor.TYPE_ORIENTATION (CB ph動董ng h動畛ng)
Sensor Programming 
 Gi畛i thi畛u 
 Android chia c畉m bi畉n thnh 3 lo畉i: 
 Nh坦m c畉m bi畉n v畛 chuy畛n 畛ng: C畉m bi畉n gia t畛c, 
c畉m bi畉n tr畛ng l畛c, c畉m bi畉n con quay, c畉m bi畉n gia 
t畛c kh担ng tr畛ng l畛c, c畉m bi畉n v辿c t董 quay. 
 Nh坦m c畉m bi畉n v畛 v畛 tr鱈:C畉m bi畉n t畛 tr動畛ng, c畉m bi畉n 
ph動董ng h動畛ng, c畉m bi畉n ti畛m c畉n. 
 Nh坦m c畉m bi畉n v畛 m担i tr動畛ng: C畉m bi畉n nhi畛t 畛 m担i 
tr動畛ng, c畉m bi畉n 叩nh s叩ng, c畉m bi畉n 叩p su畉t, c畉m 
bi畉n 畛 畉m, c畉m bi畉n nhi畛t 畛 thi畉t b畛.
Sensor Programming 
 Gi畛i thi畛u 
 Android Sensor Programming API 
 V鱈 d畛 minh ho畉
Sensor Programming 
 Android Sensor Programming API: 
 Package: android.hardware 
 Class & Interface: 
 SensorManager  Android sensor service 
 Sensor  sensor c畛 th畛 
 SensorEvent  s畛 ki畛n tr棚n sensor 
 SensorEventListener  th担ng b叩o khi c坦 s畛 ki畛n x畉y ra 
tr棚n sensor c畛 th畛.
Sensor Programming 
 Android Sensor Programming API: 
 X叩c 畛nh t畉t c畉 c叩c sensor: 
private SensorManager mSensorManager; 
... 
mSensorManager = (SensorManager) 
getSystemService(Context.SENSOR_SERVICE); 
List<Sensor> sensorsList = 
mSensorManager.getSensorList(Sensor.TYPE_ALL);
Sensor Programming 
 Android Sensor Programming API: 
 SensorManager class: 
 L畛p ny cho ph辿p ch炭ng ta truy xu畉t 畉n c叩c c畉m bi畉n c畛a 
thi畉t b畛. 
 畛 truy xu畉t 畉n instance c畛a l畛p ny ch炭ng ta s畛 d畛ng 
ph動董ng th畛c Context#getSystemService v畛i tham s畛 l 
Context.SENSOR_SERVICE 
 C叩c ph動董ng th畛c th動畛ng d湛ng: 
 List<Sensor> getSensorList(int type) 
 static float[ ] getOrientation(float[ ] R, float[ ] values) 
 boolean registerListener(SensorEventListener listener,Sensor 
sensor, int rate) 
 rate: Ch畛 ra th畛i gian hay t畉n su畉t o 畛 chuy畛n cho listener. 但y ch畛 l gi叩 tr畛 畛 ngh畛, h畛 
th畛ng c坦 th畛 tr畉 v畛 nhanh h董n ho畉c ch畉m h董n. C叩c gi叩 tr畛 畛nh ngh挑a s畉n: 
SENSOR_DELAY_FASTEST, SENSOR_DELAY_GAME, SENSOR_DELAY_UI, 
SENSOR_DELAY_NORMAL.
Sensor Programming 
 Android Sensor Programming API: 
 Sensor class: 
 L畛p ny 畉i di畛n cho m畛t c畉m bi畉n. 
 C叩c th担ng tin l畛p ny cung c畉p v畛 m畛t c畉m bi畉n: 
 Maximum range  畛 o l畛n nh畉t (theo 董n v畛 c畛a c畉m bi畉n). 
 Minimum delay  Kho畉ng th畛i gian nh畛 nh畉t gi畛a 2 s畛 ki畛n x畉y ra. 
 Name  T棚n c畉m bi畉n. 
 Power  C動畛ng 畛 d嘆ng i畛n d湛ng b畛i c畉m bi畉n. 
 Resolution  畛 ph但n gi畉i c畛a c畉m bi畉n (theo 董n v畛 c畛a c畉m bi畉n). 
 Type  Lo畉i c畉m bi畉n. 
 Vendor  T棚n c畛a nh s畉n xu畉t. 
 Version  Phi棚n b畉n. 
 C叩c th担ng tin c畛a c畉m bi畉n c坦 th畛 truy xu畉t th担ng qua c叩c 
hm getter t動董ng 畛ng.
Sensor Programming 
 Android Sensor Programming API: 
 SensorEvent class: 
 L畛p ny 畉i di畛n cho m畛t s畛 ki畛n c畛a c畉m bi畉n. 
 C叩c th担ng tin l畛p ny cung c畉p v畛 m畛t s畛 ki畛n c畛a c畉m 
bi畉n: 
 畛 ch鱈nh x叩c c畛a s畛 ki畛n. 
 C畉m bi畉n sinh ra s畛 ki畛n. 
 Th畛i i畛m ph叩t sinh s畛 ki畛n. 
 M畛t chu畛i d畛 li畛u (畛 di v thnh ph畉n ph畛 thu畛c vo lo畉i c畉m bi畉n).
Sensor Programming 
 Android Sensor Programming API: 
 SensorEventListener class: 
 畛 giao ti畉p v畛i c畉m bi畉n th狸 畛ng d畛ng c畉n ng k箪 v畛i 
SensorManager c畉m bi畉n m 畛ng d畛ng quan t但m c湛ng 
v畛i b畉n hi畛n th畛c SensorEventListener. M畛i khi c坦 m畛t 
s畛 ki畛n x畉y ra tr棚n c畉m bi畉n th狸 SensorManager s畉 th担ng 
b叩o cho 畛ng d畛ng qua c叩c hm callback c畛a b畉n hi畛n 
th畛c SensorEventListener m 畛ng d畛ng 達 ng k箪. 
 Hai ph動董ng th畛c b畉t bu畛c ph畉i hi畛n th畛c: 
 void onAccuracyChanged(Sensor sensor, int accuracy) 
 g畛i khi 畛 ch鱈nh x叩c c畛a ph辿p o c畛a c畉m bi畉n 
thay 畛i 
 void onSensorChanged(SensorEvent event)  g畛i khi 
c坦 m畛t s畛 ki畛n x畉y ra tr棚n c畉m bi畉n.
Sensor Programming 
 Gi畛i thi畛u 
 Android Sensor Programming API 
 V鱈 d畛 minh ho畉
NNGGUUYY畛畛NN HH畛畛UU PPHH働働畛畛CC, MEng. 
Website:http://phuocnh.nghelong.com

More Related Content

Viewers also liked (19)

Android N但ng cao-Bi 9-Debug in Android Application Development
Android N但ng cao-Bi 9-Debug in Android Application Development Android N但ng cao-Bi 9-Debug in Android Application Development
Android N但ng cao-Bi 9-Debug in Android Application Development
Phuoc Nguyen
Android N但ng cao-Bi 4: Content Provider
Android N但ng cao-Bi 4: Content ProviderAndroid N但ng cao-Bi 4: Content Provider
Android N但ng cao-Bi 4: Content Provider
Phuoc Nguyen
Android chapter03-life-cycle
Android chapter03-life-cycleAndroid chapter03-life-cycle
Android chapter03-life-cycle
Love Ga Nhok Mitom
Android presentation
Android presentationAndroid presentation
Android presentation
Nguyen Duong
IT120-1. Gi畛i thi畛u v畛 Android SDK
IT120-1. Gi畛i thi畛u v畛 Android SDKIT120-1. Gi畛i thi畛u v畛 Android SDK
IT120-1. Gi畛i thi畛u v畛 Android SDK
MultiUni
Google Android Security (Basic2Advanced)
Google Android Security (Basic2Advanced)Google Android Security (Basic2Advanced)
Google Android Security (Basic2Advanced)
Giap Le Van
Android chapter03-life-cycle
Android chapter03-life-cycleAndroid chapter03-life-cycle
Android chapter03-life-cycle
Vu Dang
Basic Sqlite in Android
Basic Sqlite in AndroidBasic Sqlite in Android
Basic Sqlite in Android
yuchi_1k91 Pit
際際滷 h畛i th畉o Google Android BKHN 26-10
際際滷 h畛i th畉o Google Android BKHN 26-10際際滷 h畛i th畉o Google Android BKHN 26-10
際際滷 h畛i th畉o Google Android BKHN 26-10
Giap Le Van
Android OS
Android OSAndroid OS
Android OS
Phanxico L棚 C担ng Vi棚n
C叩ch t畛i 動u h坦a m担i tr動畛ng l畉p tr狸nh 畛ng d畛ng cho Android - Tng t畛c m叩y 畉o A...
C叩ch t畛i 動u h坦a m担i tr動畛ng l畉p tr狸nh 畛ng d畛ng cho Android - Tng t畛c m叩y 畉o A...C叩ch t畛i 動u h坦a m担i tr動畛ng l畉p tr狸nh 畛ng d畛ng cho Android - Tng t畛c m叩y 畉o A...
C叩ch t畛i 動u h坦a m担i tr動畛ng l畉p tr狸nh 畛ng d畛ng cho Android - Tng t畛c m叩y 畉o A...
Ngo Trung
Lap trinh android kiem tien ngay trong khi hoc
Lap trinh android  kiem tien ngay trong khi hocLap trinh android  kiem tien ngay trong khi hoc
Lap trinh android kiem tien ngay trong khi hoc
H畛c vi畛n o t畉o CNTT NIIT iNET
Tim hi棚u v棚 h棚味 i棚u hanh android
Tim hi棚u v棚 h棚味 i棚u hanh androidTim hi棚u v棚 h棚味 i棚u hanh android
Tim hi棚u v棚 h棚味 i棚u hanh android
Tc 畛 Xun
Bi 1: Gi畛i thi畛u Android
Bi 1: Gi畛i thi畛u AndroidBi 1: Gi畛i thi畛u Android
Bi 1: Gi畛i thi畛u Android
hoccungdoanhnghiep
56123159 android
56123159 android56123159 android
56123159 android
Hieu Pham
Christopher hills-victor-beasely--your-electro vibratory-body
Christopher hills-victor-beasely--your-electro vibratory-bodyChristopher hills-victor-beasely--your-electro vibratory-body
Christopher hills-victor-beasely--your-electro vibratory-body
ganesha1963
L & a part 6
L & a part 6L & a part 6
L & a part 6
debpearcy
Agile Wake Up #3 : La transformation Agile de Kisio DigitalAgile Wake Up #3 : La transformation Agile de Kisio Digital
Agile Wake Up #3 : La transformation Agile de Kisio Digital
Zenika
Android N但ng cao-Bi 9-Debug in Android Application Development
Android N但ng cao-Bi 9-Debug in Android Application Development Android N但ng cao-Bi 9-Debug in Android Application Development
Android N但ng cao-Bi 9-Debug in Android Application Development
Phuoc Nguyen
Android N但ng cao-Bi 4: Content Provider
Android N但ng cao-Bi 4: Content ProviderAndroid N但ng cao-Bi 4: Content Provider
Android N但ng cao-Bi 4: Content Provider
Phuoc Nguyen
Android chapter03-life-cycle
Android chapter03-life-cycleAndroid chapter03-life-cycle
Android chapter03-life-cycle
Love Ga Nhok Mitom
Android presentation
Android presentationAndroid presentation
Android presentation
Nguyen Duong
IT120-1. Gi畛i thi畛u v畛 Android SDK
IT120-1. Gi畛i thi畛u v畛 Android SDKIT120-1. Gi畛i thi畛u v畛 Android SDK
IT120-1. Gi畛i thi畛u v畛 Android SDK
MultiUni
Google Android Security (Basic2Advanced)
Google Android Security (Basic2Advanced)Google Android Security (Basic2Advanced)
Google Android Security (Basic2Advanced)
Giap Le Van
Android chapter03-life-cycle
Android chapter03-life-cycleAndroid chapter03-life-cycle
Android chapter03-life-cycle
Vu Dang
Basic Sqlite in Android
Basic Sqlite in AndroidBasic Sqlite in Android
Basic Sqlite in Android
yuchi_1k91 Pit
際際滷 h畛i th畉o Google Android BKHN 26-10
際際滷 h畛i th畉o Google Android BKHN 26-10際際滷 h畛i th畉o Google Android BKHN 26-10
際際滷 h畛i th畉o Google Android BKHN 26-10
Giap Le Van
C叩ch t畛i 動u h坦a m担i tr動畛ng l畉p tr狸nh 畛ng d畛ng cho Android - Tng t畛c m叩y 畉o A...
C叩ch t畛i 動u h坦a m担i tr動畛ng l畉p tr狸nh 畛ng d畛ng cho Android - Tng t畛c m叩y 畉o A...C叩ch t畛i 動u h坦a m担i tr動畛ng l畉p tr狸nh 畛ng d畛ng cho Android - Tng t畛c m叩y 畉o A...
C叩ch t畛i 動u h坦a m担i tr動畛ng l畉p tr狸nh 畛ng d畛ng cho Android - Tng t畛c m叩y 畉o A...
Ngo Trung
Tim hi棚u v棚 h棚味 i棚u hanh android
Tim hi棚u v棚 h棚味 i棚u hanh androidTim hi棚u v棚 h棚味 i棚u hanh android
Tim hi棚u v棚 h棚味 i棚u hanh android
Tc 畛 Xun
Bi 1: Gi畛i thi畛u Android
Bi 1: Gi畛i thi畛u AndroidBi 1: Gi畛i thi畛u Android
Bi 1: Gi畛i thi畛u Android
hoccungdoanhnghiep
56123159 android
56123159 android56123159 android
56123159 android
Hieu Pham
Christopher hills-victor-beasely--your-electro vibratory-body
Christopher hills-victor-beasely--your-electro vibratory-bodyChristopher hills-victor-beasely--your-electro vibratory-body
Christopher hills-victor-beasely--your-electro vibratory-body
ganesha1963
L & a part 6
L & a part 6L & a part 6
L & a part 6
debpearcy
Agile Wake Up #3 : La transformation Agile de Kisio DigitalAgile Wake Up #3 : La transformation Agile de Kisio Digital
Agile Wake Up #3 : La transformation Agile de Kisio Digital
Zenika

More from Phuoc Nguyen (7)

Lanh dao va TPP
Lanh dao va TPPLanh dao va TPP
Lanh dao va TPP
Phuoc Nguyen
Hiberbate Framework
Hiberbate FrameworkHiberbate Framework
Hiberbate Framework
Phuoc Nguyen
Introduction to Hibernate Framework
Introduction to Hibernate FrameworkIntroduction to Hibernate Framework
Introduction to Hibernate Framework
Phuoc Nguyen
Webservice performance testing with SoapUI
Webservice performance testing with SoapUIWebservice performance testing with SoapUI
Webservice performance testing with SoapUI
Phuoc Nguyen
Web application security test tools
Web application security test toolsWeb application security test tools
Web application security test tools
Phuoc Nguyen
A successful project sharing
A successful project sharingA successful project sharing
A successful project sharing
Phuoc Nguyen
Bu畛n vui ngh畛 IT (Pros & cons of IT Career)
Bu畛n vui ngh畛 IT (Pros & cons of IT Career)Bu畛n vui ngh畛 IT (Pros & cons of IT Career)
Bu畛n vui ngh畛 IT (Pros & cons of IT Career)
Phuoc Nguyen
Lanh dao va TPP
Lanh dao va TPPLanh dao va TPP
Lanh dao va TPP
Phuoc Nguyen
Hiberbate Framework
Hiberbate FrameworkHiberbate Framework
Hiberbate Framework
Phuoc Nguyen
Introduction to Hibernate Framework
Introduction to Hibernate FrameworkIntroduction to Hibernate Framework
Introduction to Hibernate Framework
Phuoc Nguyen
Webservice performance testing with SoapUI
Webservice performance testing with SoapUIWebservice performance testing with SoapUI
Webservice performance testing with SoapUI
Phuoc Nguyen
Web application security test tools
Web application security test toolsWeb application security test tools
Web application security test tools
Phuoc Nguyen
A successful project sharing
A successful project sharingA successful project sharing
A successful project sharing
Phuoc Nguyen
Bu畛n vui ngh畛 IT (Pros & cons of IT Career)
Bu畛n vui ngh畛 IT (Pros & cons of IT Career)Bu畛n vui ngh畛 IT (Pros & cons of IT Career)
Bu畛n vui ngh畛 IT (Pros & cons of IT Career)
Phuoc Nguyen

Android location sensor programming

  • 1. Smart Robot Contest 2013 L畉P TRNH ANDROID Bi 03: LLooccaattiioonn && SSeennssoorr PPrrooggrraammmmiinngg
  • 2. N畛i dung bi h畛c Location Programming Sensor Programming
  • 3. N畛i dung bi h畛c Location Programming Sensor Programming
  • 4. Location Programming Gi畛i thi畛u Android Location Service. X叩c 畛nh v畛 tr鱈 hi畛n t畉i c畛a thi畉t b畛. Theo d探i s畛 di chuy畛n c畛a thi畉t b畛. Proximity Alerts.
  • 5. Location Programming Gi畛i thi畛u Android Location Service. X叩c 畛nh v畛 tr鱈 hi畛n t畉i c畛a thi畉t b畛. Theo d探i s畛 di chuy畛n c畛a thi畉t b畛. C畉nh b叩o khi c坦 s畛 di chuy畛n 畉n g畉n.
  • 6. Android Location Service 畛 cung c畉p th担ng tin v畛 v畛 tr鱈, Android s畛 d畛ng location provider. C叩c lo畉i location provider nh動 sau: GPS Provider Network Provider
  • 7. Android Location Service GPS Provider: S畛 d畛ng GPS (Global positioning system) 畛 gi炭p thi畉t b畛 Android x叩c 畛nh v畛 tr鱈 c畛a n坦. Tuy nhi棚n, thi畉t b畛 di 畛ng r畉t kh坦 lm vi畛c tr畛c ti畉p v畛i GPS (th畛i gian truy畛n th担ng tin v畛 tr鱈 c叩c v畛 tinh 畉n thi畉t b畛). C叩c thi畉t b畛 di 畛ng hi畛n 畉i th動畛ng s畛 d畛ng A-GPS (th担ng tin v畛 tr鱈 c叩c v畛 tinh truy畛n qua m畉ng vi畛n th担ng 畉n thi畉t b畛) ho畉c S-GPS (cho ph辿p s畛 d畛ng 畛ng th畛i GPS radio v cellular network radio).
  • 8. Android Location Service GPS Provider: H畉n ch畉: GPS provider h畉u nh動 kh担ng th畛 ho畉t 畛ng trong nh hay 畛 ngoi tr畛i nh畛ng v湛ng m b畉u tr畛i b畛 che khu畉t nh動 trong r畛ng r畉m ch畉ng h畉n. N畉u tr棚n 動畛ng i c畛a s坦ng b畛 nhi畛u v畉t c畉n, s坦ng ph畉i ph畉n x畉 nhi畛u l畉n tng 畛 di 動畛ng i x叩c 畛nh v畛 tr鱈 b畛 sai (multipath error).
  • 9. Android Location Service Network Provider: S畛 d畛ng Wireless Network Access Point. S畛 d畛ng Cell ID.
  • 10. Android Location Service Network Provider: S畛 d畛ng Wireless Network Access Point: S畛 d畛ng th担ng tin Wi-Fi (MAC c畛a AP v c動畛ng 畛 t鱈n hi畛u nh畉n b畛i AP) 畛 truy v畉n Google location service. i畛m m畉nh c畛a ph動董ng ph叩p ny l c坦 th畛 x叩c 畛nh v畛 tr鱈 c畛a thi畉t b畛 畛 nh畛ng n董i m GPS Provider kh担ng th畛 cung c畉p. i畛m y畉u: Y棚u c畉u ph畉i c坦 m畉ng Wi-Fi, AP ph畉i c畉u h狸nh SSID ho畉c SSID kh担ng k畉t th炭c b畉ng _nomap. Thay 畛i v畛 tr鱈 c畛a AP.
  • 11. Android Location Service Network Provider: S畛 d畛ng Cell ID: S畛 d畛ng Cell ID 畛 truy v畉n Google location service v畛 th担ng tin v畛 tr鱈 c畛a thi畉t b畛. V畛 i畛m m畉nh v i畛m y畉u th狸 ph動董ng ph叩p ny kh叩 gi畛ng v畛i ph動董ng ph叩p s畛 d畛ng Wireless Network Access Points. Tuy nhi棚n th狸 v畛 tr鱈 c畛a c叩c c畛t ph叩t s坦ng (cell tower) r畉t hi畉m thay 畛i so v畛i c叩c access point.
  • 12. Location Programming Gi畛i thi畛u Android Location Service. X叩c 畛nh v畛 tr鱈 hi畛n t畉i c畛a thi畉t b畛. Theo d探i s畛 di chuy畛n c畛a thi畉t b畛. Promixity Alerts.
  • 13. X叩c 畛nh v畛 tr鱈 hi畛n t畉i c畛a thi畉t b畛 C叩c thnh ph畉n c畛a Android Location API. X叩c 畛nh location provider. C畉p nh畉t th担ng tin location. V鱈 d畛 minh ho畉.
  • 14. X叩c 畛nh v畛 tr鱈 hi畛n t畉i c畛a thi畉t b畛 C叩c thnh ph畉n c畛a Android Location API: LocationManager LocationProvider Th担ng b叩o+Location Location 畛c th担ng tin y棚u c畉u Criteria 畛i v畛i LocationProvider LocationListener Cho ph辿p ho畉t 畛ng Sinh ra S畛 d畛ng
  • 15. X叩c 畛nh v畛 tr鱈 hi畛n t畉i c畛a thi畉t b畛 X叩c 畛nh location provider: Location Provider Permission y棚u c畉u S畛 d畛ng Pin 畛 ch鱈nh x叩c GPS Provider android.permission. ACCESS_FINE_LOCATION ho畉c android.permission. ACCESS_COARSE_LOCATION S畛 d畛ng nhi畛u pin nh畉t so v畛i c叩c provider kh叩c Ch鱈nh x叩c nh畉t trong c叩c provider Network Provider android.permission. ACCESS_COARSE_LOCATION S畛 d畛ng 鱈t pin h董n GPS provider t ch鱈nh x叩c h董n GPS provider. Passive Provider android.permission. ACCESS_FINE_LOCATION Kh担ng x叩c 畛nh Kh担ng x叩c 畛nh.
  • 16. X叩c 畛nh v畛 tr鱈 hi畛n t畉i c畛a thi畉t b畛 C畉p nh畉t th担ng tin location: C坦 hai ph動董ng ph叩p S畛 d畛ng LocationListener v hi畛n th畛c ph動董ng th畛c onLocationChanged S畛 d畛ng Broadcast Receiver 畛 nh畉n broadcast Intent khi location 動畛c c畉p nh畉t.
  • 17. Hi畛n th畛c 畛ng d畛ng demo Hi畛n th畛c interface LocationListener L畉y handle c畛a LocationManager Y棚u c畉u c畉p nh畉t th担ng tin location. D畛n d畉p
  • 18. Hi畛n th畛c 畛ng d畛ng demo Hi畛n th畛c interface LocationListener: Ph動董ng th畛c M担 t畉 onLocationChanged Th担ng b叩o th担ng tin location 達 c畉p nh畉t. onProviderDisabled & onProviderEnabled Ng動畛i d湛ng disable/enable location provider t畛 setting menu. onStatusChanged Location provider on/off. public void onLocationChanged(Location arg0) { txtViDo.setText(String.valueOf(arg0.getLatitude())); txtKinhDo.setText(String.valueOf(arg0.getLongitude())); txtProvider.setText(String.valueOf(arg0.getProvider())); long timeToFix = SystemClock.uptimeMillis() - uptimeAtResume; txtTTF.setText(String.valueOf(timeToFix / 1000)); lblDCXDonVi.setVisibility(View.VISIBLE); lblTTFDonVi.setVisibility(View.VISIBLE); }
  • 19. Hi畛n th畛c 畛ng d畛ng demo L畉y handle c畛a LocationManager: private LocationManager lm; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE); txtViDo = (TextView)findViewById(R.id.txtViDo); txtKinhDo = (TextView)findViewById(R.id.txtKinhDo); txtDoChinhXac = (TextView)findViewById(R.id.txtDoChinhXac); txtTTF = (TextView)findViewById(R.id.txtTTF); txtProvider = (TextView)findViewById(R.id.txtProvider); txtEnabledProvider = (TextView)findViewById(R.id.txtEnabledProvider); lblDCXDonVi = (TextView)findViewById(R.id.lblDCXDonVi); lblTTFDonVi = (TextView)findViewById(R.id.lblTTFDonVi); }
  • 20. Hi畛n th畛c 畛ng d畛ng demo Y棚u c畉u c畉p nh畉t th担ng tin location: Ch炭ng ta kh担ng th畛 y棚u c畉u Android cung c畉p th担ng tin ny ngay l畉p t畛c m ch炭ng ta ch畛 y棚u c畉u v nh畉n 動畛c th担ng tin khi no n坦 c坦 th担ng qua ph動董ng th畛c LocationManager#requestSingleUpdate
  • 21. Hi畛n th畛c 畛ng d畛ng demo protected void onResume() { super.onResume(); StringBuffer stBuffer = new StringBuffer(); Criteria criteria = new Criteria(); criteria.setAccuracy(Criteria.ACCURACY_COARSE); enabledProviders = lm.getProviders(criteria, true); if (enabledProviders.isEmpty()){ txtEnabledProvider.setText(""); }else{ for (String enabledProvider : enabledProviders) { stBuffer.append(enabledProvider).append(" "); lm.requestSingleUpdate(enabledProvider,this,null); } txtEnabledProvider.setText(stBuffer); } uptimeAtResume = SystemClock.uptimeMillis(); txtViDo.setText(""); txtKinhDo.setText(""); txtProvider.setText(""); txtDoChinhXac.setText(""); txtTTF.setText(""); lblDCXDonVi.setVisibility(View.GONE); lblTTFDonVi.setVisibility(View.GONE); }
  • 22. Hi畛n th畛c 畛ng d畛ng demo D畛n d畉p : @Override protected void onPause() { super.onPause(); lm.removeUpdates(this); }
  • 23. Location Programming Gi畛i thi畛u Android Location Service. X叩c 畛nh v畛 tr鱈 hi畛n t畉i c畛a thi畉t b畛. Theo d探i s畛 di chuy畛n c畛a thi畉t b畛. Promixity Alerts.
  • 24. Theo d探i s畛 di chuy畛n c畛a thi畉t b畛 Thu th畉p d畛 li畛u location. Tr狸nh by d畛 li畛u location.
  • 25. Theo d探i s畛 di chuy畛n c畛a thi畉t b畛 Thu th畉p d畛 li畛u location. Tr狸nh by d畛 li畛u location.
  • 26. Theo d探i s畛 di chuy畛n c畛a thi畉t b畛 Thu th畉p d畛 li畛u location: S畛 d畛ng BrodcastReceiver. S畛 d畛ng Service.
  • 27. Theo d探i s畛 di chuy畛n c畛a thi畉t b畛 Thu th畉p d畛 li畛u location: S畛 d畛ng BroadcastReceiver: T畉o broadcast Intent v畛i action 達 畛nh ngh挑a tr動畛c T畉o PendingIntent t畛 getBroadcast. S畛 d畛ng ph動董ng th畛c LocationManager#requestLocationUpdate 畛 ng k箪 PendingIntent v畛i LocationManager. Hi畛n th畛c BroadcastReceiver 畛 nh畉n broadcast Intent 動畛c g畛i m畛i khi location 動畛c c畉p nh畉t.
  • 28. Theo d探i s畛 di chuy畛n c畛a thi畉t b畛 Thu th畉p d畛 li畛u location: S畛 d畛ng Service: Hi畛n th畛c Service v LocationListener. Trong ph動董ng th畛c onCreate l畉y handle c畛a LocationManager. Trong ph動董ng th畛c onStartCommand s畛 d畛ng ph動董ng th畛c requestOnLocationUpdate 畛 ng k箪 listener cho LocationManger. Trong onLocationChanged ch炭ng ta l動u l畉i location. Trong ph動董ng th畛c onDestroy ch炭ng ta d湛ng ph動董ng th畛c removeUpdate 畛 xo叩 ng k箪.
  • 29. Theo d探i s畛 di chuy畛n c畛a thi畉t b畛 Thu th畉p d畛 li畛u location. Tr狸nh by d畛 li畛u location.
  • 30. Theo d探i s畛 di chuy畛n c畛a thi畉t b畛 Tr狸nh by d畛 li畛u location: Th動畛ng s畛 d畛ng Google Map 畛 tr狸nh by d畛 li畛u location thu th畉p 動畛c. 但y l ch畛 畛 t畛 t狸m hi畛u.
  • 31. Location Programming Gi畛i thi畛u Android Location Service. X叩c 畛nh v畛 tr鱈 hi畛n t畉i c畛a thi畉t b畛. Theo d探i s畛 di chuy畛n c畛a thi畉t b畛. Promixity Alerts.
  • 32. Promixity Alerts Geocoding Thi畉t l畉p Promixity Alert. 畛i 畛ng v畛i Promixity Alert O R
  • 33. Promixity Alerts Geocoding Thi畉t l畉p Promixity Alert. 畛i 畛ng v畛i Promixity Alert
  • 34. Promixity Alerts Geocoding: Chuy畛n t棚n v畛 tr鱈 sang to畉 畛 (kinh 畛, v挑 畛). Class android.location.Geocoder cho ph辿p ch炭ng ta th畛c hi畛n geocoding v ng動畛c l畉i. isPresent ki畛m tra b畉n Android hi畛n t畉i c坦 h畛 tr畛 c叩c ph動董ng th畛c geocoding v reverse geocoding hay kh担ng. GetLocationFromName tr畉 v畛 danh s叩ch c叩c 畛a i畛m ph湛 h畛p v畛i t棚n 動畛c cung c畉p.
  • 35. Promixity Alerts Geocoding Thi畉t l畉p Promixity Alert. 畛i 畛ng v畛i Promixity Alert
  • 36. Promixity Alerts Thi畉t l畉p Promixity Alert: S畛 d畛ng ph動董ng th畛c LocationManager#addPromixityAlert 畛 thi畉t l畉p proximity alert: Kinh 畛, v挑 畛. B叩n k鱈nh v湛ng xung quanh. Gi畛i h畉n v畛 th畛i gian. PendingIntent: intent s畉 動畛c send broadcast khi proximity alert 動畛c k鱈ch ho畉t S畛 d畛ng ph動董ng th畛c LocationManager#removeProximityAlert 畛 hu畛 ng k箪 1 proximity alert.
  • 37. Promixity Alerts Geocoding. Thi畉t l畉p Promixity Alert. 畛i 畛ng v畛i Promixity Alert.
  • 38. Promixity Alerts 畛i 畛ng v畛i Promixity Alert: Khi proximity alert b畛 k鱈ch ho畉t th狸 intent trong PendingIntent s畉 動畛c g畛i broadcast Hi畛n th畛c BroadcastReceiver 畛 nh畉n intent ny. S畛 d畛ng key LocationManager.KEY_PROXIMITY_ENTERING 畛 filter v nh畉n th担ng tin t畛 Intent.
  • 39. N畛i dung bi h畛c Location Programming Sensor Programming
  • 40. Sensor Programming Gi畛i thi畛u Android Sensor Programming API V鱈 d畛 minh ho畉
  • 41. Sensor Programming Gi畛i thi畛u Android Sensor Programming API V鱈 d畛 minh ho畉
  • 42. Sensor Programming Gi畛i thi畛u MEMS (Microelectromechanical sensors) l nh畛ng c畉m bi畉n c坦 k鱈ch th動畛c c畛c k畛 nh畛 動畛c s畉n xu畉t d畛a tr棚n k畛 thu畉t s畉n xu畉t chip m叩y t鱈nh. C叩c lo畉i c畉m bi畉n: Raw sensor cung c畉p d畛 li畛u th担 t畛 thi畉t b畛 v畉t l箪 Sensor.TYPE_LIGHT (C畉m bi畉n 叩nh s叩ng) Sensor.TYPE_PROXIMITY (C畉m bi畉n ti畛m c畉n) Sensor.TYPE_PRESSURE (C畉m bi畉n 叩p su畉t) Sensor.TYPE_TEMPERATURE (C畉m bi畉n nhi畛t 畛 thi畉t b畛) Sensor.TYPE_ACCELEROMETER (C畉m bi畉n gia t畛c) Sensor.TYPE_GYROSCOPE (C畉m bi畉n con quay) Sensor.TYPE_MAGNETIC_FIELD (C畉m bi畉n t畛 tr動畛ng) Sensor.TYPE_RELATIVE_HUMIDITY (C畉m bi畉n 畛 畉m) Sensor.TYPE_AMBIENT_TEMPERATURE (CB nhi畛t 畛 m担i tr動畛ng)
  • 43. Sensor Programming Gi畛i thi畛u C叩c lo畉i c畉m bi畉n: Synthetic sensor (composite, virtual): abstract layer gi畛a code v thi畉t b畛 v畉t l箪 Sensor.TYPE_ROTATION_VECTOR (CB v辿c t董 quay) Sensor.TYPE_LINEAR_ACCELERATION (CB gia t畛c kh担ng tr畛ng l畛c) Sensor.TYPE_GRAVITY (CB tr畛ng l畛c) Sensor.TYPE_ORIENTATION (CB ph動董ng h動畛ng)
  • 44. Sensor Programming Gi畛i thi畛u Android chia c畉m bi畉n thnh 3 lo畉i: Nh坦m c畉m bi畉n v畛 chuy畛n 畛ng: C畉m bi畉n gia t畛c, c畉m bi畉n tr畛ng l畛c, c畉m bi畉n con quay, c畉m bi畉n gia t畛c kh担ng tr畛ng l畛c, c畉m bi畉n v辿c t董 quay. Nh坦m c畉m bi畉n v畛 v畛 tr鱈:C畉m bi畉n t畛 tr動畛ng, c畉m bi畉n ph動董ng h動畛ng, c畉m bi畉n ti畛m c畉n. Nh坦m c畉m bi畉n v畛 m担i tr動畛ng: C畉m bi畉n nhi畛t 畛 m担i tr動畛ng, c畉m bi畉n 叩nh s叩ng, c畉m bi畉n 叩p su畉t, c畉m bi畉n 畛 畉m, c畉m bi畉n nhi畛t 畛 thi畉t b畛.
  • 45. Sensor Programming Gi畛i thi畛u Android Sensor Programming API V鱈 d畛 minh ho畉
  • 46. Sensor Programming Android Sensor Programming API: Package: android.hardware Class & Interface: SensorManager Android sensor service Sensor sensor c畛 th畛 SensorEvent s畛 ki畛n tr棚n sensor SensorEventListener th担ng b叩o khi c坦 s畛 ki畛n x畉y ra tr棚n sensor c畛 th畛.
  • 47. Sensor Programming Android Sensor Programming API: X叩c 畛nh t畉t c畉 c叩c sensor: private SensorManager mSensorManager; ... mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); List<Sensor> sensorsList = mSensorManager.getSensorList(Sensor.TYPE_ALL);
  • 48. Sensor Programming Android Sensor Programming API: SensorManager class: L畛p ny cho ph辿p ch炭ng ta truy xu畉t 畉n c叩c c畉m bi畉n c畛a thi畉t b畛. 畛 truy xu畉t 畉n instance c畛a l畛p ny ch炭ng ta s畛 d畛ng ph動董ng th畛c Context#getSystemService v畛i tham s畛 l Context.SENSOR_SERVICE C叩c ph動董ng th畛c th動畛ng d湛ng: List<Sensor> getSensorList(int type) static float[ ] getOrientation(float[ ] R, float[ ] values) boolean registerListener(SensorEventListener listener,Sensor sensor, int rate) rate: Ch畛 ra th畛i gian hay t畉n su畉t o 畛 chuy畛n cho listener. 但y ch畛 l gi叩 tr畛 畛 ngh畛, h畛 th畛ng c坦 th畛 tr畉 v畛 nhanh h董n ho畉c ch畉m h董n. C叩c gi叩 tr畛 畛nh ngh挑a s畉n: SENSOR_DELAY_FASTEST, SENSOR_DELAY_GAME, SENSOR_DELAY_UI, SENSOR_DELAY_NORMAL.
  • 49. Sensor Programming Android Sensor Programming API: Sensor class: L畛p ny 畉i di畛n cho m畛t c畉m bi畉n. C叩c th担ng tin l畛p ny cung c畉p v畛 m畛t c畉m bi畉n: Maximum range 畛 o l畛n nh畉t (theo 董n v畛 c畛a c畉m bi畉n). Minimum delay Kho畉ng th畛i gian nh畛 nh畉t gi畛a 2 s畛 ki畛n x畉y ra. Name T棚n c畉m bi畉n. Power C動畛ng 畛 d嘆ng i畛n d湛ng b畛i c畉m bi畉n. Resolution 畛 ph但n gi畉i c畛a c畉m bi畉n (theo 董n v畛 c畛a c畉m bi畉n). Type Lo畉i c畉m bi畉n. Vendor T棚n c畛a nh s畉n xu畉t. Version Phi棚n b畉n. C叩c th担ng tin c畛a c畉m bi畉n c坦 th畛 truy xu畉t th担ng qua c叩c hm getter t動董ng 畛ng.
  • 50. Sensor Programming Android Sensor Programming API: SensorEvent class: L畛p ny 畉i di畛n cho m畛t s畛 ki畛n c畛a c畉m bi畉n. C叩c th担ng tin l畛p ny cung c畉p v畛 m畛t s畛 ki畛n c畛a c畉m bi畉n: 畛 ch鱈nh x叩c c畛a s畛 ki畛n. C畉m bi畉n sinh ra s畛 ki畛n. Th畛i i畛m ph叩t sinh s畛 ki畛n. M畛t chu畛i d畛 li畛u (畛 di v thnh ph畉n ph畛 thu畛c vo lo畉i c畉m bi畉n).
  • 51. Sensor Programming Android Sensor Programming API: SensorEventListener class: 畛 giao ti畉p v畛i c畉m bi畉n th狸 畛ng d畛ng c畉n ng k箪 v畛i SensorManager c畉m bi畉n m 畛ng d畛ng quan t但m c湛ng v畛i b畉n hi畛n th畛c SensorEventListener. M畛i khi c坦 m畛t s畛 ki畛n x畉y ra tr棚n c畉m bi畉n th狸 SensorManager s畉 th担ng b叩o cho 畛ng d畛ng qua c叩c hm callback c畛a b畉n hi畛n th畛c SensorEventListener m 畛ng d畛ng 達 ng k箪. Hai ph動董ng th畛c b畉t bu畛c ph畉i hi畛n th畛c: void onAccuracyChanged(Sensor sensor, int accuracy) g畛i khi 畛 ch鱈nh x叩c c畛a ph辿p o c畛a c畉m bi畉n thay 畛i void onSensorChanged(SensorEvent event) g畛i khi c坦 m畛t s畛 ki畛n x畉y ra tr棚n c畉m bi畉n.
  • 52. Sensor Programming Gi畛i thi畛u Android Sensor Programming API V鱈 d畛 minh ho畉
  • 53. NNGGUUYY畛畛NN HH畛畛UU PPHH働働畛畛CC, MEng. Website:http://phuocnh.nghelong.com