Skip to content

Commit 854672f

Browse files
author
Douglas Nassif Roma Junior
committed
- Added disconnect() method
- Fixed runOnMainThread() bug
1 parent 1b58486 commit 854672f

File tree

8 files changed

+54
-88
lines changed

8 files changed

+54
-88
lines changed

BluetoothClassicLibrary/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ android {
66
defaultConfig {
77
minSdkVersion 7
88
targetSdkVersion 7
9-
versionCode 2
10-
versionName "0.3.0"
9+
versionCode 3
10+
versionName "0.3.1"
1111
}
1212
buildTypes {
1313
release {

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

Lines changed: 20 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -51,26 +51,6 @@ protected BluetoothClassicService(BluetoothConfiguration config) {
5151
mStatus = BluetoothStatus.NONE;
5252
}
5353

54-
/**
55-
* Set the current state of the chat connection
56-
*
57-
* @param status An integer defining the current connection state
58-
*/
59-
protected synchronized void updateStatus(final BluetoothStatus status) {
60-
if (D)
61-
Log.d(TAG, "updateStatus() " + mStatus + " -> " + status);
62-
mStatus = status;
63-
64-
// Give the new state to the Handler so the UI Activity can update
65-
if (onEventCallback != null)
66-
runOnMainThread(new Runnable() {
67-
@Override
68-
public void run() {
69-
onEventCallback.onStatusChange(status);
70-
}
71-
});
72-
73-
}
7454

7555
/**
7656
* Start the ConnectThread to initiate a connection to a remote device.
@@ -82,38 +62,20 @@ public synchronized void connect(BluetoothDevice device) {
8262
if (D)
8363
Log.d(TAG, "connect to: " + device);
8464

85-
// Cancel any thread attempting to make a connection
86-
if (mStatus == BluetoothStatus.CONNECTING) {
87-
if (mConnectThread != null) {
88-
mConnectThread.cancel();
89-
mConnectThread = null;
90-
}
91-
}
92-
93-
// Cancel any thread currently running a connection
94-
if (mConnectedThread != null) {
95-
mConnectedThread.cancel();
96-
mConnectedThread = null;
97-
}
65+
disconnect();
9866

9967
// Start the thread to connect with the given device
10068
mConnectThread = new ConnectThread(device);
10169
mConnectThread.start();
10270
updateState(BluetoothStatus.CONNECTING);
10371
}
10472

105-
/**
106-
* Start the ConnectedThread to begin managing a Bluetooth connection
107-
*
108-
* @param socket The BluetoothSocket on which the connection was made
109-
* @param device The BluetoothDevice that has been connected
110-
*/
111-
@RequiresPermission(Manifest.permission.BLUETOOTH)
112-
public synchronized void connected(BluetoothSocket socket, final BluetoothDevice device) {
73+
@Override
74+
public void disconnect() {
11375
if (D)
114-
Log.d(TAG, "connected");
76+
Log.d(TAG, "disconnect");
11577

116-
// Cancel the thread that completed the connection
78+
// Cancel any thread attempting to make a connection
11779
if (mConnectThread != null) {
11880
mConnectThread.cancel();
11981
mConnectThread = null;
@@ -124,6 +86,18 @@ public synchronized void connected(BluetoothSocket socket, final BluetoothDevice
12486
mConnectedThread.cancel();
12587
mConnectedThread = null;
12688
}
89+
}
90+
91+
/**
92+
* Start the ConnectedThread to begin managing a Bluetooth connection
93+
*
94+
* @param socket The BluetoothSocket on which the connection was made
95+
* @param device The BluetoothDevice that has been connected
96+
*/
97+
@RequiresPermission(Manifest.permission.BLUETOOTH)
98+
private synchronized void connected(BluetoothSocket socket, final BluetoothDevice device) {
99+
if (D)
100+
Log.d(TAG, "connected");
127101

128102
// Start the thread to manage the connection and perform transmissions
129103
mConnectedThread = new ConnectedThread(socket);
@@ -150,16 +124,9 @@ public void run() {
150124
public synchronized void stopService() {
151125
if (D)
152126
Log.d(TAG, "stop");
153-
if (mConnectThread != null) {
154-
mConnectThread.cancel();
155-
mConnectThread = null;
156-
}
157-
if (mConnectedThread != null) {
158-
mConnectedThread.cancel();
159-
mConnectedThread = null;
160-
}
161-
if (getStatus() != BluetoothStatus.NONE)
162-
updateState(BluetoothStatus.NONE);
127+
128+
disconnect();
129+
163130
if (BluetoothService.mDefaultServiceInstance == this)
164131
BluetoothService.mDefaultServiceInstance = null;
165132
}

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

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

33
import android.bluetooth.BluetoothDevice;
44
import android.os.Handler;
5-
import android.os.Looper;
65
import android.util.Log;
76

87
import java.lang.reflect.Constructor;
@@ -31,11 +30,7 @@ public abstract class BluetoothService {
3130
protected BluetoothService(BluetoothConfiguration config) {
3231
this.mConfig = config;
3332
this.mStatus = BluetoothStatus.NONE;
34-
if (config.callListenersInMainThread) {
35-
handler = new Handler();
36-
} else {
37-
handler = null;
38-
}
33+
this.handler = new Handler();
3934
}
4035

4136
public void setOnEventCallback(OnBluetoothEventCallback onEventCallback) {
@@ -65,13 +60,20 @@ public void run() {
6560
}
6661

6762
protected void runOnMainThread(Runnable runnable, long delayMillis) {
68-
if ((mConfig.callListenersInMainThread && Looper.myLooper() != Looper.getMainLooper()) || delayMillis > 0) {
69-
if (delayMillis > 0)
63+
if (mConfig.callListenersInMainThread) {
64+
if (delayMillis > 0) {
7065
handler.postDelayed(runnable, delayMillis);
71-
else
66+
} else {
7267
handler.post(runnable);
68+
}
7369
} else {
74-
runnable.run();
70+
try {
71+
if (delayMillis > 0)
72+
Thread.sleep(delayMillis);
73+
runnable.run();
74+
} catch (InterruptedException e) {
75+
e.printStackTrace();
76+
}
7577
}
7678
}
7779

@@ -109,6 +111,11 @@ public synchronized BluetoothStatus getStatus() {
109111
*/
110112
public abstract void connect(BluetoothDevice device);
111113

114+
/**
115+
* Try to disconnect to the device and call the {@link OnBluetoothEventCallback}
116+
*/
117+
public abstract void disconnect();
118+
112119
/**
113120
* Write a array of bytes to the connected device.
114121
*/

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

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,7 @@
44
* Created by douglas on 25/11/14.
55
*/
66
public enum BluetoothStatus {
7-
CONNECTED {
8-
@Override
9-
public String toString() {
10-
return "Conectado";
11-
}
12-
},
13-
CONNECTING {
14-
@Override
15-
public String toString() {
16-
return "Conectando";
17-
}
18-
},
19-
NONE {
20-
@Override
21-
public String toString() {
22-
return "Desconectado";
23-
}
24-
}
7+
CONNECTED,
8+
CONNECTING,
9+
NONE
2510
}

BluetoothLowEnergyLibrary/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ android {
66
defaultConfig {
77
minSdkVersion 18
88
targetSdkVersion 18
9-
versionCode 2
10-
versionName "0.3.0"
9+
versionCode 3
10+
versionName "0.3.1"
1111
}
1212
buildTypes {
1313
release {

BluetoothLowEnergyLibrary/src/main/java/com/github/douglasjunior/bluetoothlowenergylibrary/BluetoothLeService.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,13 @@ public void connect(BluetoothDevice bluetoothDevice) {
266266
}
267267
}
268268

269+
@Override
270+
public void disconnect() {
271+
if (bluetoothGatt != null) {
272+
bluetoothGatt.disconnect();
273+
}
274+
}
275+
269276
@RequiresPermission(Manifest.permission.BLUETOOTH_ADMIN)
270277
final Runnable mStopScanRunnable = new Runnable() {
271278
@RequiresPermission(Manifest.permission.BLUETOOTH_ADMIN)

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@ Soon
3030
2.1. Bluetooth Classic
3131
```javascript
3232
dependencies {
33-
compile 'com.github.douglasjunior.AndroidBluetoothLibrary:BluetoothClassicLibrary:0.3.0'
33+
compile 'com.github.douglasjunior.AndroidBluetoothLibrary:BluetoothClassicLibrary:0.3.1'
3434
}
3535
```
3636

3737
2.2. Bluetooth Low Energy
3838
```javascript
3939
dependencies {
40-
compile 'com.github.douglasjunior.AndroidBluetoothLibrary:BluetoothLowEnergyLibrary:0.3.0'
40+
compile 'com.github.douglasjunior.AndroidBluetoothLibrary:BluetoothLowEnergyLibrary:0.3.1'
4141
}
4242
```
4343

Sample/src/main/java/com/github/douglasjunior/bluetoothsample/MainActivity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public class MainActivity extends AppCompatActivity implements BluetoothService.
3030
/*
3131
* Change for the UUID that you want.
3232
*/
33-
private static final UUID UUID_DEVICE = UUID.fromString("00001101-0000-1000-8000-00805f9b3411");
33+
private static final UUID UUID_DEVICE = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb");
3434
private static final UUID UUID_SERVICE = UUID.fromString("35111C00001101-0000-1000-8000-00805F9B34FB");
3535
private static final UUID UUID_CHARACTERISTIC = UUID.fromString("35111C00001101-0000-1000-8000-00805F9B34FB");
3636

0 commit comments

Comments
 (0)