Commit 41d92f29f85f6970ff621e0a63858793b6c28179

Authored by florian staine
1 parent afe23868

Add MAC address

Plus hide map and marker before positioning
app/src/main/AndroidManifest.xml
... ... @@ -3,6 +3,8 @@
3 3 package="fr.utbm.lo53.p2017.positionningapp">
4 4  
5 5 <uses-permission android:name="android.permission.INTERNET" />
  6 + <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
  7 +
6 8 <application
7 9 android:allowBackup="true"
8 10 android:icon="@drawable/map_marker"
... ...
app/src/main/java/fr/utbm/lo53/p2017/positionningapp/PositioningActivity.java
1 1 package fr.utbm.lo53.p2017.positionningapp;
2 2  
  3 +import android.content.Context;
3 4 import android.graphics.Bitmap;
  5 +import android.net.wifi.WifiInfo;
  6 +import android.net.wifi.WifiManager;
  7 +import android.os.Build;
4 8 import android.os.Bundle;
5 9 import android.os.Handler;
6 10 import android.support.constraint.ConstraintLayout;
... ... @@ -26,6 +30,10 @@ import com.android.volley.toolbox.Volley;
26 30 import org.json.JSONException;
27 31 import org.json.JSONObject;
28 32  
  33 +import java.net.NetworkInterface;
  34 +import java.net.SocketException;
  35 +import java.util.Collections;
  36 +import java.util.List;
29 37 import java.util.Random;
30 38  
31 39 public class PositioningActivity extends BaseActivity {
... ... @@ -51,7 +59,7 @@ public class PositioningActivity extends BaseActivity {
51 59 public void run() {
52 60 // Create request
53 61 JsonObjectRequest locateRequest = new JsonObjectRequest(Request.Method.GET,
54   - getURLSolver().locateURL(),
  62 + getURLSolver().locateURL(getMacAddress()),
55 63 null,
56 64 new Response.Listener<JSONObject>() {
57 65 @Override
... ... @@ -128,6 +136,7 @@ public class PositioningActivity extends BaseActivity {
128 136 }
129 137  
130 138 public void startLocating(View v) {
  139 + Log.i(TAG, v.toString());
131 140 if (isLocating) {
132 141 return;
133 142 }
... ... @@ -162,6 +171,7 @@ public class PositioningActivity extends BaseActivity {
162 171 mapMarker.setX(mapView.getX() + x_on_map - mapMarker.getWidth()/2);
163 172 mapMarker.setY(mapView.getY() + y_on_map - mapMarker.getHeight());
164 173 mapMarker.setVisibility(View.VISIBLE);
  174 + mapView.setVisibility(mapView.VISIBLE);
165 175 }
166 176  
167 177 private boolean hasCorrectMap(int mapId) {
... ... @@ -175,6 +185,7 @@ public class PositioningActivity extends BaseActivity {
175 185 @Override
176 186 public void onResponse(Bitmap bitmap) {
177 187 mapView.setImageBitmap(bitmap);
  188 + mapView.setVisibility(mapView.VISIBLE);
178 189 }
179 190 }, 0, 0, null,
180 191 new Response.ErrorListener() {
... ... @@ -185,4 +196,40 @@ public class PositioningActivity extends BaseActivity {
185 196 });
186 197 queue.add(mapRequest);
187 198 }
  199 +
  200 + protected String getMacAddress() {
  201 + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
  202 + // Get MAC address prior to android 6.0
  203 + WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
  204 + WifiInfo wInfo = wifiManager.getConnectionInfo();
  205 + return wInfo.getMacAddress();
  206 + } else {
  207 + // Removed in Android 6.0 -> manually get MAC address
  208 + try {
  209 + List<NetworkInterface> all = Collections.list(NetworkInterface.getNetworkInterfaces());
  210 + for (NetworkInterface nif : all) {
  211 + if (!nif.getName().equalsIgnoreCase("wlan0")) continue;
  212 +
  213 + byte[] macBytes = nif.getHardwareAddress();
  214 + if (macBytes == null) {
  215 + return "";
  216 + }
  217 +
  218 + StringBuilder res1 = new StringBuilder();
  219 + for (byte b : macBytes) {
  220 + //res1.append(Integer.toHexString(b & 0xFF) + ":");
  221 + res1.append(String.format("%02X:", b));
  222 + }
  223 +
  224 + if (res1.length() > 0) {
  225 + res1.deleteCharAt(res1.length() - 1);
  226 + }
  227 + return res1.toString();
  228 + }
  229 + } catch (SocketException e) {
  230 + e.printStackTrace();
  231 + }
  232 + return "02:00:00:00:00:00";
  233 + }
  234 + }
188 235 }
... ...
app/src/main/java/fr/utbm/lo53/p2017/positionningapp/network/URLSolver.java
... ... @@ -49,11 +49,11 @@ public class URLSolver implements SharedPreferences.OnSharedPreferenceChangeList
49 49 return builder.scheme("http").encodedAuthority(hostname + ":" + port);
50 50 }
51 51  
52   - public String locateURL(/* TODO: mac ... */) {
  52 + public String locateURL(String mac) {
53 53 Uri.Builder b = baseURL();
54 54 b.appendPath("positioning")
55 55 .appendPath("request")
56   - .appendQueryParameter("mac", "TODO"); // TODO
  56 + .appendQueryParameter("mac", mac);
57 57 return b.build().toString();
58 58 }
59 59  
... ...
app/src/main/res/layout/content_positioning.xml
... ... @@ -19,10 +19,11 @@
19 19 android:layout_marginTop="8dp"
20 20 android:adjustViewBounds="true"
21 21 android:scaleType="fitXY"
  22 + android:visibility="invisible"
22 23 app:layout_constraintLeft_toLeftOf="parent"
23 24 app:layout_constraintRight_toRightOf="parent"
24 25 app:layout_constraintTop_toTopOf="parent"
25   - app:srcCompat="@drawable/map2" />
  26 + app:srcCompat="@drawable/map" />
26 27  
27 28 <ImageView
28 29 android:id="@+id/map_marker"
... ... @@ -36,6 +37,7 @@
36 37 android:layout_marginTop="0dp"
37 38 android:adjustViewBounds="false"
38 39 android:cropToPadding="false"
  40 + android:visibility="invisible"
39 41 app:layout_constraintBottom_toBottomOf="@+id/Map"
40 42 app:layout_constraintHorizontal_bias="0.0"
41 43 app:layout_constraintLeft_toLeftOf="parent"
... ...