Skip to content

Commit 67e3cf8

Browse files
committed
Changed AsyncTask to Executor task
Added GetBtMacAddress method to retrieve the Bluetooth Mac Adrress of the device
1 parent 0d86dbc commit 67e3cf8

File tree

4 files changed

+133
-5
lines changed

4 files changed

+133
-5
lines changed

DeviceIdentifiersWrapper/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ android {
88
defaultConfig {
99
minSdkVersion 19
1010
targetSdkVersion 33
11-
versionCode 12
12-
versionName "0.9.3"
11+
versionCode 100
12+
versionName "0.10.0"
1313

1414
testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
1515

DeviceIdentifiersWrapper/src/main/java/com/zebra/deviceidentifierswrapper/DIHelper.java

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import android.Manifest.permission;
44
import android.annotation.SuppressLint;
5+
import android.bluetooth.BluetoothAdapter;
56
import android.content.Context;
67
import android.content.pm.PackageInfo;
78
import android.content.pm.PackageManager;
@@ -27,6 +28,8 @@ public class DIHelper {
2728
protected static String sIMEI = null;
2829
protected static String sSerialNumber = null;
2930

31+
protected static String sBtMacAddress = null;
32+
3033
public static final long SEC_IN_MS = 1000;
3134
public static final long MIN_IN_MS = SEC_IN_MS * 60;
3235
public static long MAX_EMDK_TIMEOUT_IN_MS = 10 * MIN_IN_MS; // 10 minutes
@@ -92,7 +95,7 @@ public void onDebugStatus(String message) {
9295
};
9396

9497
new RetrieveOEMInfoTask()
95-
.execute(context, Uri.parse("content://oem_info/oem.zebra.secure/build_serial"),
98+
.executeAsync(context, Uri.parse("content://oem_info/oem.zebra.secure/build_serial"),
9699
tempCallbackInterface);
97100
}
98101

@@ -159,7 +162,57 @@ public void onDebugStatus(String message) {
159162
}
160163
};
161164

162-
new RetrieveOEMInfoTask().execute(context, Uri.parse("content://oem_info/wan/imei"),
165+
new RetrieveOEMInfoTask().executeAsync(context, Uri.parse("content://oem_info/wan/imei"),
163166
tempCallbackInterface);
164167
}
168+
169+
public static void getBtMacAddress(Context context, IDIResultCallbacks callbackInterface)
170+
{
171+
if(sBtMacAddress != null)
172+
{
173+
if(callbackInterface != null)
174+
{
175+
callbackInterface.onDebugStatus("BT Mac address already in cache.");
176+
}
177+
callbackInterface.onSuccess(sBtMacAddress);
178+
return;
179+
}
180+
if (android.os.Build.VERSION.SDK_INT < 23) {
181+
returnBtMacAddressUsingAndroidAPIs(context, callbackInterface);
182+
} else {
183+
returnBtMacAddressUsingZebraAPIs(context, callbackInterface);
184+
}
185+
}
186+
187+
private static void returnBtMacAddressUsingZebraAPIs(Context context, IDIResultCallbacks callbackInterface) {
188+
IDIResultCallbacks tempCallbackInterface = new IDIResultCallbacks() {
189+
@Override
190+
public void onSuccess(String message) {
191+
sBtMacAddress = message;
192+
callbackInterface.onSuccess(message);
193+
}
194+
195+
@Override
196+
public void onError(String message) {
197+
callbackInterface.onError(message);
198+
}
199+
200+
@Override
201+
public void onDebugStatus(String message) {
202+
callbackInterface.onDebugStatus(message);
203+
}
204+
};
205+
206+
new RetrieveOEMInfoTask().executeAsync(context, Uri.parse("content://oem_info/oem.zebra.secure/bt_mac"),
207+
tempCallbackInterface);
208+
}
209+
210+
private static void returnBtMacAddressUsingAndroidAPIs(Context context, IDIResultCallbacks callbackInterface) {
211+
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
212+
String macAddress = mBluetoothAdapter.getAddress();
213+
if(callbackInterface != null)
214+
{
215+
callbackInterface.onSuccess(macAddress);
216+
}
217+
}
165218
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package com.zebra.deviceidentifierswrapper;
2+
3+
// Inspiration from Vitality answer : https://stackoverflow.com/a/68395429
4+
5+
import android.os.Handler;
6+
import android.os.Looper;
7+
import android.util.Log;
8+
9+
import java.util.concurrent.Executor;
10+
import java.util.concurrent.LinkedBlockingQueue;
11+
import java.util.concurrent.ThreadPoolExecutor;
12+
import java.util.concurrent.TimeUnit;
13+
14+
public abstract class ExecutorTask<Params, Progress, Result> {
15+
public static final String TAG = "ExecutorTask";
16+
17+
private static final Executor THREAD_POOL_EXECUTOR =
18+
new ThreadPoolExecutor(5, 128, 1,
19+
TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>());
20+
21+
private final Handler mHandler = new Handler(Looper.getMainLooper());
22+
private boolean mIsInterrupted = false;
23+
24+
protected void onPreExecute(){}
25+
protected abstract Result doInBackground(Params... params);
26+
protected void onPostExecute(Result result){}
27+
28+
protected void onProgressUpdate(Progress... values) {
29+
}
30+
protected void onCancelled() {}
31+
32+
@SafeVarargs
33+
public final void executeAsync(Params... params) {
34+
THREAD_POOL_EXECUTOR.execute(() -> {
35+
try {
36+
checkInterrupted();
37+
mHandler.post(this::onPreExecute);
38+
39+
checkInterrupted();
40+
final Result results = doInBackground(params);
41+
42+
checkInterrupted();
43+
mHandler.post(new Runnable() {
44+
@Override
45+
public void run() {
46+
onPostExecute(results);
47+
}
48+
});
49+
} catch (InterruptedException ex) {
50+
mHandler.post(this::onCancelled);
51+
} catch (Exception ex) {
52+
Log.e(TAG, "executeAsync: " + ex.getMessage() + "\n" + ex.getStackTrace());
53+
54+
}
55+
});
56+
}
57+
58+
private void checkInterrupted() throws InterruptedException {
59+
if (isInterrupted()){
60+
throw new InterruptedException();
61+
}
62+
}
63+
64+
public void cancel(boolean mayInterruptIfRunning){
65+
setInterrupted(mayInterruptIfRunning);
66+
}
67+
68+
public boolean isInterrupted() {
69+
return mIsInterrupted;
70+
}
71+
72+
public void setInterrupted(boolean interrupted) {
73+
mIsInterrupted = interrupted;
74+
}
75+
}

DeviceIdentifiersWrapper/src/main/java/com/zebra/deviceidentifierswrapper/RetrieveOEMInfoTask.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
* (c) Zebra 2020
2626
*/
2727

28-
class RetrieveOEMInfoTask extends AsyncTask<Object, Void, Boolean> {
28+
class RetrieveOEMInfoTask extends ExecutorTask<Object, Void, Boolean> {
2929

3030
@Override
3131
protected Boolean doInBackground(Object... objects) {

0 commit comments

Comments
 (0)