Skip to content

Commit bf5e94b

Browse files
committed
Added option: config.callListenersInMainThread
1 parent 90c386b commit bf5e94b

File tree

10 files changed

+46
-353
lines changed

10 files changed

+46
-353
lines changed

.gitignore

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
*.iml
12
.gradle
23
/local.properties
3-
/.idea/workspace.xml
4-
/.idea/libraries
4+
/.idea
55
.DS_Store
66
**/build
7-
.idea/
7+
/captures

BluetoothClassicLibrary/BluetoothClassicLibrary.iml

Lines changed: 0 additions & 100 deletions
This file was deleted.

BluetoothClassicLibrary/src/main/java/com/github/douglasjunior/bluetoothclassiclibrary/BluetoothClassicService.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -417,14 +417,6 @@ public void cancel() {
417417
}
418418
}
419419

420-
private void runOnMainThread(Runnable runnable) {
421-
if (Looper.myLooper() == Looper.getMainLooper()) {
422-
runnable.run();
423-
} else {
424-
handler.post(runnable);
425-
}
426-
}
427-
428420
// The BroadcastReceiver that listens for discovered devices and
429421
// changes the title when discovery is finished
430422
private final BroadcastReceiver mScanReceiver = new BroadcastReceiver() {

BluetoothClassicLibrary/src/main/java/com/github/douglasjunior/bluetoothclassiclibrary/BluetoothService.java

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@
33
import android.bluetooth.BluetoothDevice;
44
import android.content.Context;
55
import android.os.Handler;
6+
import android.os.Looper;
67
import android.util.Log;
78

89
import java.lang.reflect.Constructor;
910
import java.lang.reflect.InvocationTargetException;
1011
import java.util.UUID;
1112

13+
import static android.R.attr.delay;
14+
1215
/**
1316
* Created by douglas on 23/03/15.
1417
*/
@@ -21,7 +24,7 @@ public abstract class BluetoothService {
2124
protected BluetoothConfiguration mConfig;
2225
protected BluetoothStatus mStatus;
2326

24-
protected final Handler handler = new Handler();
27+
private final Handler handler;
2528

2629
protected OnBluetoothEventCallback onEventCallback;
2730

@@ -30,8 +33,13 @@ public abstract class BluetoothService {
3033
private static BluetoothConfiguration mDefaultConfiguration;
3134

3235
protected BluetoothService(BluetoothConfiguration config) {
33-
mConfig = config;
36+
this.mConfig = config;
3437
this.mStatus = BluetoothStatus.NONE;
38+
if (config.callListenersInMainThread) {
39+
handler = new Handler();
40+
} else {
41+
handler = null;
42+
}
3543
}
3644

3745
public static void setDefaultConfiguration(BluetoothConfiguration config) {
@@ -77,14 +85,29 @@ protected synchronized void updateState(final BluetoothStatus status) {
7785

7886
// Give the new state to the Handler so the UI Activity can update
7987
if (onEventCallback != null)
80-
handler.post(new Runnable() {
88+
runOnMainThread(new Runnable() {
8189
@Override
8290
public void run() {
8391
onEventCallback.onStatusChange(status);
8492
}
8593
});
8694
}
8795

96+
protected void runOnMainThread(Runnable runnable, long delayMillis) {
97+
if (!mConfig.callListenersInMainThread || Looper.myLooper() == Looper.getMainLooper()) {
98+
runnable.run();
99+
} else {
100+
if (delay > 0)
101+
handler.postDelayed(runnable, delay);
102+
else
103+
handler.post(runnable);
104+
}
105+
}
106+
107+
protected void runOnMainThread(Runnable runnable) {
108+
runOnMainThread(runnable, 0);
109+
}
110+
88111
public synchronized BluetoothStatus getStatus() {
89112
return mStatus;
90113
}
@@ -126,5 +149,6 @@ public static class BluetoothConfiguration {
126149
public UUID uuid;
127150
public UUID uuidService;
128151
public UUID uuidCharacteristic;
152+
public boolean callListenersInMainThread = true;
129153
}
130154
}

BluetoothLowEnergyLibrary/BluetoothLowEnergyLibrary.iml

Lines changed: 0 additions & 101 deletions
This file was deleted.

0 commit comments

Comments
 (0)