|
11 | 11 | import android.bluetooth.BluetoothManager; |
12 | 12 | import android.bluetooth.BluetoothProfile; |
13 | 13 | import android.content.Context; |
| 14 | +import android.os.Build; |
14 | 15 | import android.support.annotation.RequiresPermission; |
15 | 16 | import android.util.Log; |
16 | 17 |
|
17 | 18 | import com.github.douglasjunior.bluetoothclassiclibrary.BluetoothService; |
18 | 19 | import com.github.douglasjunior.bluetoothclassiclibrary.BluetoothStatus; |
19 | 20 |
|
| 21 | +import java.lang.reflect.Method; |
20 | 22 | import java.nio.ByteBuffer; |
21 | 23 | import java.nio.ByteOrder; |
22 | 24 | import java.util.ArrayList; |
@@ -231,8 +233,35 @@ public void connect(BluetoothDevice bluetoothDevice) { |
231 | 233 | if (bluetoothGatt != null) { |
232 | 234 | bluetoothGatt.disconnect(); |
233 | 235 | } |
| 236 | + |
234 | 237 | updateState(BluetoothStatus.CONNECTING); |
235 | | - bluetoothGatt = bluetoothDevice.connectGatt(mConfig.context, false, btleGattCallback); |
| 238 | + |
| 239 | + /* |
| 240 | + About this issue: |
| 241 | + https://code.google.com/p/android/issues/detail?id=92949 |
| 242 | + http://stackoverflow.com/q/27633680/2826279 |
| 243 | + */ |
| 244 | + |
| 245 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { |
| 246 | + // If android verion is greather or equal to Android M (23), then call the connectGatt with TRANSPORT_LE. |
| 247 | + bluetoothGatt = bluetoothDevice.connectGatt(mConfig.context, false, btleGattCallback, mConfig.transport); |
| 248 | + } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { |
| 249 | + // From Android LOLLIPOP (21) the transport types exists, but them are hide for use, |
| 250 | + // so is needed to use relfection to get the value |
| 251 | + try { |
| 252 | + Method connectGattMethod = bluetoothDevice.getClass().getDeclaredMethod("connectGatt", Context.class, boolean.class, BluetoothGattCallback.class, int.class); |
| 253 | + connectGattMethod.setAccessible(true); |
| 254 | + bluetoothGatt = (BluetoothGatt) connectGattMethod.invoke(bluetoothDevice, mConfig.context, false, btleGattCallback, mConfig.transport); |
| 255 | + } catch (Exception ex) { |
| 256 | + Log.d(TAG, "Error on call BluetoothDevice.connectGatt with reflection.", ex); |
| 257 | + } |
| 258 | + } |
| 259 | + |
| 260 | + // If any try is fail, then call the connectGatt without transport |
| 261 | + if (bluetoothGatt == null) { |
| 262 | + bluetoothGatt = bluetoothDevice.connectGatt(mConfig.context, false, btleGattCallback); |
| 263 | + } |
| 264 | + |
236 | 265 | } |
237 | 266 | } |
238 | 267 |
|
@@ -338,9 +367,9 @@ private List<UUID> parseUUIDs(final byte[] advertisedData) { |
338 | 367 | @RequiresPermission(Manifest.permission.BLUETOOTH_ADMIN) |
339 | 368 | @Override |
340 | 369 | public void stopScan() { |
341 | | - runOnMainThread(mStopScanRunnable); |
| 370 | + removeRunnableFromHandler(mStopScanRunnable); |
342 | 371 | btAdapter.stopLeScan(mLeScanCallback); |
343 | | - |
| 372 | + |
344 | 373 | if (onScanCallback != null) |
345 | 374 | runOnMainThread(new Runnable() { |
346 | 375 | @Override |
|
0 commit comments