Skip to content

Commit

Permalink
now you can operate in child thread
Browse files Browse the repository at this point in the history
  • Loading branch information
Jasonchenlijian committed Jan 5, 2018
1 parent 3461f1b commit a690320
Show file tree
Hide file tree
Showing 14 changed files with 261 additions and 179 deletions.
4 changes: 2 additions & 2 deletions FastBleLib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ android {
defaultConfig {
minSdkVersion 14
targetSdkVersion 22
versionCode 217
versionName "2.1.7"
versionCode 220
versionName "2.2.0"
}
buildTypes {
release {
Expand Down
9 changes: 7 additions & 2 deletions FastBleLib/src/main/java/com/clj/fastble/BleManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Looper;

import com.clj.fastble.bluetooth.BleBluetooth;
import com.clj.fastble.bluetooth.MultipleBluetoothController;
Expand Down Expand Up @@ -253,10 +254,14 @@ public BluetoothGatt connect(BleDevice bleDevice, BleGattCallback bleGattCallbac
}

if (!isBlueEnable()) {
handleException(new OtherException("BlueTooth not enable!"));
handleException(new OtherException("BlueTooth is not enabled!"));
return null;
}

if (Looper.myLooper() == null || Looper.myLooper() != Looper.getMainLooper()) {
BleLog.w("Be careful: currentThread is not MainThread!");
}

if (bleDevice == null || bleDevice.getDevice() == null) {
bleGattCallback.onConnectFail(new NotFoundDeviceException());
} else {
Expand Down Expand Up @@ -400,7 +405,7 @@ public void write(BleDevice bleDevice,
}

if (data.length > 20) {
BleLog.w("data's length beyond 20!");
BleLog.w("Be careful: data's length beyond 20! Ensure MTU higher than 23.");
}

BleBluetooth bleBluetooth = multipleBluetoothController.getBleBluetooth(bleDevice);
Expand Down
193 changes: 126 additions & 67 deletions FastBleLib/src/main/java/com/clj/fastble/bluetooth/BleBluetooth.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import com.clj.fastble.exception.GattException;
import com.clj.fastble.exception.OtherException;
import com.clj.fastble.exception.TimeoutException;
import com.clj.fastble.utils.BleLog;

import java.util.UUID;

Expand Down Expand Up @@ -216,7 +215,6 @@ public void handleMessage(Message msg) {
}

BleConnector(BleBluetooth bleBluetooth) {
BleLog.w("currentThread: " + Thread.currentThread().getId());
Looper looper = Looper.myLooper();
if (looper == null) {
Looper.prepare();
Expand All @@ -227,7 +225,12 @@ public void handleMessage(Message msg) {
}

private void run() {
Looper.loop();
if (Looper.myLooper() != Looper.getMainLooper())
Looper.loop();
}

public void destroy() {
handler.removeCallbacksAndMessages(null);
}

private BleConnector withUUID(UUID serviceUUID, UUID characteristicUUID) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.clj.fastble.data;


import android.bluetooth.BluetoothGatt;

import com.clj.fastble.callback.BleGattCallback;

public class BleConnectStateParameter {

private BleGattCallback callback;
private BluetoothGatt gatt;
private int status;
private boolean isAcitive;
private BleDevice bleDevice;


public BleConnectStateParameter(BleGattCallback callback, BluetoothGatt gatt, int status) {
this.callback = callback;
this.gatt = gatt;
this.status = status;
}

public BleGattCallback getCallback() {
return callback;
}

public void setCallback(BleGattCallback callback) {
this.callback = callback;
}

public BluetoothGatt getGatt() {
return gatt;
}

public void setGatt(BluetoothGatt gatt) {
this.gatt = gatt;
}

public int getStatus() {
return status;
}

public void setStatus(int status) {
this.status = status;
}

public boolean isAcitive() {
return isAcitive;
}

public void setAcitive(boolean acitive) {
isAcitive = acitive;
}

public BleDevice getBleDevice() {
return bleDevice;
}

public void setBleDevice(BleDevice bleDevice) {
this.bleDevice = bleDevice;
}
}
7 changes: 6 additions & 1 deletion FastBleLib/src/main/java/com/clj/fastble/data/BleMsg.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@

public class BleMsg {

// Connect
public static final int MSG_CONNECT_FAIL = 0x01;
public static final int MSG_DISCONNECTED = 0x02;
public static final int MSG_CONNECT_SUCCESS = 0x03;

// Notify
public static final int MSG_CHA_NOTIFY_START = 0x11;
public static final int MSG_CHA_NOTIFY_RESULT = 0x12;
Expand All @@ -29,7 +34,7 @@ public class BleMsg {
public static final String KEY_READ_BUNDLE_STATUS = "read_status";
public static final String KEY_READ_BUNDLE_VALUE = "read_value";

// Read Rssi
// Rssi
public static final int MSG_READ_RSSI_START = 0x51;
public static final int MSG_READ_RSSI_RESULT = 0x52;
public static final String KEY_READ_RSSI_BUNDLE_STATUS = "rssi_status";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {

private void next(BleDevice bleDevice) {
if (mNeedConnect) {
BleLog.i("devices detected --------"
BleLog.i("devices detected ------"
+ " name:" + bleDevice.getName()
+ " mac:" + bleDevice.getMac()
+ " Rssi:" + bleDevice.getRssi()
Expand All @@ -100,7 +100,7 @@ private void next(BleDevice bleDevice) {
+ " name: " + bleDevice.getName()
+ " mac: " + bleDevice.getMac()
+ " Rssi: " + bleDevice.getRssi()
+ " scanRecord: " + HexUtil.formatHexString(bleDevice.getScanRecord()));
+ " scanRecord: " + HexUtil.formatHexString(bleDevice.getScanRecord(), true));

mBleDeviceList.add(bleDevice);
onScanning(bleDevice);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ private static class BleScannerHolder {

public void scan(UUID[] serviceUuids, String[] names, String mac, boolean fuzzy,
long timeOut, final BleScanCallback callback) {

startLeScan(serviceUuids, new BleScanPresenter(names, mac, fuzzy, false, timeOut) {
@Override
public void onScanStarted(boolean success) {
Expand Down
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ android {
applicationId "com.clj.blesample"
minSdkVersion 14
targetSdkVersion 25
versionCode 217
versionName "2.1.7"
versionCode 220
versionName "2.2.0"
}
buildTypes {
release {
Expand Down
8 changes: 4 additions & 4 deletions app/src/main/java/com/clj/blesample/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,10 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);
initView();

BleManager.getInstance().init(getApplication());

BleManager.getInstance()
.enableLog(true)
.setMaxConnectCount(7)
Expand Down Expand Up @@ -250,7 +248,7 @@ public void onScanFinished(List<BleDevice> scanResultList) {
});
}

private void connect(BleDevice bleDevice) {
private void connect(final BleDevice bleDevice) {
BleManager.getInstance().connect(bleDevice, new BleGattCallback() {
@Override
public void onStartConnect() {
Expand Down Expand Up @@ -283,7 +281,9 @@ public void onDisConnected(boolean isActiveDisConnected, BleDevice bleDevice, Bl
mDeviceAdapter.removeDevice(bleDevice);
mDeviceAdapter.notifyDataSetChanged();

if (!isActiveDisConnected) {
if (isActiveDisConnected) {
Toast.makeText(MainActivity.this, getString(R.string.active_disconnected), Toast.LENGTH_LONG).show();
}else {
Toast.makeText(MainActivity.this, getString(R.string.disconnected), Toast.LENGTH_LONG).show();
ObserverManager.getInstance().notifyObserver(bleDevice);
}
Expand Down
Loading

0 comments on commit a690320

Please sign in to comment.