Skip to content

Commit

Permalink
- Fixing issue with run in main thread
Browse files Browse the repository at this point in the history
- Fixing bug on try to connect to BLE withou TRANSPORT_LE type
  • Loading branch information
Douglas Nassif Roma Junior committed Apr 10, 2017
1 parent 270d1fa commit 2d71887
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
Expand Down Expand Up @@ -92,20 +93,26 @@ public void run() {
}

protected void runOnMainThread(Runnable runnable, long delayMillis) {
if (!mConfig.callListenersInMainThread || Looper.myLooper() == Looper.getMainLooper()) {
runnable.run();
} else {
if ((mConfig.callListenersInMainThread && Looper.myLooper() != Looper.getMainLooper()) || delayMillis > 0) {
if (delayMillis > 0)
handler.postDelayed(runnable, delayMillis);
else
handler.post(runnable);
} else {
runnable.run();
}
}

protected void runOnMainThread(Runnable runnable) {
runOnMainThread(runnable, 0);
}

protected void removeRunnableFromHandler(Runnable runnable) {
if (handler != null) {
handler.removeCallbacks(runnable);
}
}

public synchronized BluetoothStatus getStatus() {
return mStatus;
}
Expand Down Expand Up @@ -147,6 +154,32 @@ public static class BluetoothConfiguration {
public UUID uuid;
public UUID uuidService;
public UUID uuidCharacteristic;
/**
* @see BluetoothDevice#TRANSPORT_AUTO
* @see BluetoothDevice#TRANSPORT_BREDR
* @see BluetoothDevice#TRANSPORT_LE
*/
public int transport;
public boolean callListenersInMainThread = true;

public BluetoothConfiguration() {
setDefaultTransport();
}

private void setDefaultTransport() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
transport = BluetoothDevice.TRANSPORT_LE;
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
// From Android LOLLIPOP (21) the transport types exists, but them are hide for use,
// so is needed to use relfection to get the value
try {
transport = BluetoothDevice.class.getDeclaredField("TRANSPORT_LE").getInt(null);
} catch (Exception ex) {
Log.d(TAG, "Error on get BluetoothDevice.TRANSPORT_LE with reflection.", ex);
}
} else {
transport = -1;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@
import android.bluetooth.BluetoothManager;
import android.bluetooth.BluetoothProfile;
import android.content.Context;
import android.os.Build;
import android.support.annotation.RequiresPermission;
import android.util.Log;

import com.github.douglasjunior.bluetoothclassiclibrary.BluetoothService;
import com.github.douglasjunior.bluetoothclassiclibrary.BluetoothStatus;

import java.lang.reflect.Method;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.ArrayList;
Expand Down Expand Up @@ -231,8 +233,35 @@ public void connect(BluetoothDevice bluetoothDevice) {
if (bluetoothGatt != null) {
bluetoothGatt.disconnect();
}

updateState(BluetoothStatus.CONNECTING);
bluetoothGatt = bluetoothDevice.connectGatt(mConfig.context, false, btleGattCallback);

/*
About this issue:
https://code.google.com/p/android/issues/detail?id=92949
http://stackoverflow.com/q/27633680/2826279
*/

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
// If android verion is greather or equal to Android M (23), then call the connectGatt with TRANSPORT_LE.
bluetoothGatt = bluetoothDevice.connectGatt(mConfig.context, false, btleGattCallback, mConfig.transport);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
// From Android LOLLIPOP (21) the transport types exists, but them are hide for use,
// so is needed to use relfection to get the value
try {
Method connectGattMethod = bluetoothDevice.getClass().getDeclaredMethod("connectGatt", Context.class, boolean.class, BluetoothGattCallback.class, int.class);
connectGattMethod.setAccessible(true);
bluetoothGatt = (BluetoothGatt) connectGattMethod.invoke(bluetoothDevice, mConfig.context, false, btleGattCallback, mConfig.transport);
} catch (Exception ex) {
Log.d(TAG, "Error on call BluetoothDevice.connectGatt with reflection.", ex);
}
}

// If any try is fail, then call the connectGatt without transport
if (bluetoothGatt == null) {
bluetoothGatt = bluetoothDevice.connectGatt(mConfig.context, false, btleGattCallback);
}

}
}

Expand Down Expand Up @@ -338,9 +367,9 @@ private List<UUID> parseUUIDs(final byte[] advertisedData) {
@RequiresPermission(Manifest.permission.BLUETOOTH_ADMIN)
@Override
public void stopScan() {
runOnMainThread(mStopScanRunnable);
removeRunnableFromHandler(mStopScanRunnable);
btAdapter.stopLeScan(mLeScanCallback);

if (onScanCallback != null)
runOnMainThread(new Runnable() {
@Override
Expand Down

0 comments on commit 2d71887

Please sign in to comment.