Skip to content

Commit 456eac8

Browse files
committed
BLE bridge disconnect
1 parent ca954e5 commit 456eac8

File tree

1 file changed

+50
-6
lines changed
  • app/src/main/java/com/umarbhutta/xlightcompanion/SDK/BLE

1 file changed

+50
-6
lines changed

app/src/main/java/com/umarbhutta/xlightcompanion/SDK/BLE/BLEBridge.java

+50-6
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import com.umarbhutta.xlightcompanion.SDK.BaseBridge;
1010

1111

12+
import java.lang.reflect.Method;
13+
1214
import static android.bluetooth.BluetoothDevice.BOND_BONDED;
1315

1416
/**
@@ -20,10 +22,11 @@ public class BLEBridge extends BaseBridge {
2022
private static final String TAG = BLEBridge.class.getSimpleName();
2123

2224
public static final int STATE_DISCONNECTED = 0;
23-
public static final int STATE_SCANNING = 1;
24-
public static final int STATE_CONNECTING = 2;
25-
public static final int STATE_CONNECTED = 3;
26-
public static final int STATE_SERVICES_DISCOVERED = 4;
25+
public static final int STATE_CONNECTING = 1;
26+
public static final int STATE_CONNECTED = 2;
27+
public static final int STATE_DISCONNECTING = 3;
28+
public static final int STATE_SCANNING = 5;
29+
public static final int STATE_SERVICES_DISCOVERED = 6;
2730

2831
private boolean m_bPaired = false;
2932
private BluetoothDevice m_bleDevice;
@@ -56,6 +59,35 @@ public boolean connectController() {
5659
return false;
5760
}
5861

62+
public boolean refreshDeviceCache() {
63+
try {
64+
final Method refresh = BluetoothGatt.class.getMethod("refresh");
65+
if (refresh != null) {
66+
final boolean success = (Boolean) refresh.invoke(m_bleGatt);
67+
Log.i(TAG, "Refreshing result: " + success);
68+
return success;
69+
}
70+
} catch (Exception e) {
71+
Log.e(TAG, "An exception occured while refreshing device", e);
72+
}
73+
return false;
74+
}
75+
76+
public void closeBluetoothGatt() {
77+
if (m_bleGatt != null) {
78+
m_bleGatt.disconnect();
79+
}
80+
81+
if (m_bleGatt != null) {
82+
refreshDeviceCache();
83+
}
84+
85+
if (m_bleGatt != null) {
86+
m_bleGatt.close();
87+
m_bleGatt = null;
88+
}
89+
}
90+
5991
@Override
6092
public void setName(final String name) {
6193
super.setName(name);
@@ -92,6 +124,18 @@ public boolean isServiceDiscovered() {
92124
return connectionState == STATE_SERVICES_DISCOVERED;
93125
}
94126

127+
/**
128+
* return
129+
* {@link #STATE_DISCONNECTED}
130+
* {@link #STATE_SCANNING}
131+
* {@link #STATE_CONNECTING}
132+
* {@link #STATE_CONNECTED}
133+
* {@link #STATE_SERVICES_DISCOVERED}
134+
*/
135+
public int getConnectionState() {
136+
return connectionState;
137+
}
138+
95139
private BleGattCallback coreGattCallback = new BleGattCallback() {
96140

97141
@Override
@@ -117,15 +161,15 @@ public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState
117161
+ '\n' + "newState: " + newState
118162
+ '\n' + "thread: " + Thread.currentThread().getId());
119163

120-
if (newState == STATE_CONNECTED) {
164+
if (newState == BluetoothGatt.STATE_CONNECTED) {
121165
connectionState = STATE_CONNECTED;
122166
onConnectSuccess(gatt, status);
123167

124168
} else if (newState == BluetoothGatt.STATE_DISCONNECTED) {
125169
connectionState = STATE_DISCONNECTED;
126170
onConnectFailure(new ConnectException(gatt, status));
127171

128-
} else if (newState == STATE_CONNECTING) {
172+
} else if (newState == BluetoothGatt.STATE_CONNECTING) {
129173
connectionState = STATE_CONNECTING;
130174
}
131175
}

0 commit comments

Comments
 (0)