9
9
import com .umarbhutta .xlightcompanion .SDK .BaseBridge ;
10
10
11
11
12
+ import java .lang .reflect .Method ;
13
+
12
14
import static android .bluetooth .BluetoothDevice .BOND_BONDED ;
13
15
14
16
/**
@@ -20,10 +22,11 @@ public class BLEBridge extends BaseBridge {
20
22
private static final String TAG = BLEBridge .class .getSimpleName ();
21
23
22
24
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 ;
27
30
28
31
private boolean m_bPaired = false ;
29
32
private BluetoothDevice m_bleDevice ;
@@ -56,6 +59,35 @@ public boolean connectController() {
56
59
return false ;
57
60
}
58
61
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
+
59
91
@ Override
60
92
public void setName (final String name ) {
61
93
super .setName (name );
@@ -92,6 +124,18 @@ public boolean isServiceDiscovered() {
92
124
return connectionState == STATE_SERVICES_DISCOVERED ;
93
125
}
94
126
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
+
95
139
private BleGattCallback coreGattCallback = new BleGattCallback () {
96
140
97
141
@ Override
@@ -117,15 +161,15 @@ public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState
117
161
+ '\n' + "newState: " + newState
118
162
+ '\n' + "thread: " + Thread .currentThread ().getId ());
119
163
120
- if (newState == STATE_CONNECTED ) {
164
+ if (newState == BluetoothGatt . STATE_CONNECTED ) {
121
165
connectionState = STATE_CONNECTED ;
122
166
onConnectSuccess (gatt , status );
123
167
124
168
} else if (newState == BluetoothGatt .STATE_DISCONNECTED ) {
125
169
connectionState = STATE_DISCONNECTED ;
126
170
onConnectFailure (new ConnectException (gatt , status ));
127
171
128
- } else if (newState == STATE_CONNECTING ) {
172
+ } else if (newState == BluetoothGatt . STATE_CONNECTING ) {
129
173
connectionState = STATE_CONNECTING ;
130
174
}
131
175
}
0 commit comments