Skip to content

Commit

Permalink
修复扫描卡顿问题;
Browse files Browse the repository at this point in the history
优化断开连接处理。
  • Loading branch information
xiaoyaoyou1212 committed Nov 21, 2017
1 parent 929c9c0 commit 823eebf
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 79 deletions.
28 changes: 3 additions & 25 deletions baseble/src/main/java/com/vise/baseble/ViseBle.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,28 +77,6 @@ public void init(Context context) {
}
}

/**
* 开始扫描
*
* @param leScanCallback 回调
*/
public void startLeScan(BluetoothAdapter.LeScanCallback leScanCallback) {
if (bluetoothAdapter != null) {
bluetoothAdapter.startLeScan(leScanCallback);
}
}

/**
* 停止扫描
*
* @param leScanCallback 回调
*/
public void stopLeScan(BluetoothAdapter.LeScanCallback leScanCallback) {
if (bluetoothAdapter != null) {
bluetoothAdapter.stopLeScan(leScanCallback);
}
}

/**
* 开始扫描
*
Expand All @@ -108,7 +86,7 @@ public void startScan(ScanCallback scanCallback) {
if (scanCallback == null) {
throw new IllegalArgumentException("this ScanCallback is Null!");
}
scanCallback.setScan(true).setScanTimeout(BleConfig.getInstance().getScanTimeout()).scan();
scanCallback.setScan(true).scan();
}

/**
Expand Down Expand Up @@ -160,7 +138,7 @@ public void connectByMac(String mac, final IConnectCallback connectCallback) {
}
startScan(new SingleFilterScanCallback(new IScanCallback() {
@Override
public void onDeviceFound(BluetoothLeDeviceStore bluetoothLeDeviceStore) {
public void onDeviceFound(BluetoothLeDevice bluetoothLeDevice) {

}

Expand Down Expand Up @@ -198,7 +176,7 @@ public void connectByName(String name, final IConnectCallback connectCallback) {
}
startScan(new SingleFilterScanCallback(new IScanCallback() {
@Override
public void onDeviceFound(BluetoothLeDeviceStore bluetoothLeDeviceStore) {
public void onDeviceFound(BluetoothLeDevice bluetoothLeDevice) {

}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.vise.baseble.callback.scan;

import com.vise.baseble.model.BluetoothLeDevice;
import com.vise.baseble.model.BluetoothLeDeviceStore;

/**
Expand All @@ -9,7 +10,7 @@
*/
public interface IScanCallback {
//发现设备
void onDeviceFound(BluetoothLeDeviceStore bluetoothLeDeviceStore);
void onDeviceFound(BluetoothLeDevice bluetoothLeDevice);

//扫描完成
void onScanFinish(BluetoothLeDeviceStore bluetoothLeDeviceStore);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package com.vise.baseble.callback.scan;

import com.vise.baseble.model.BluetoothLeDevice;
import com.vise.baseble.model.BluetoothLeDeviceStore;

/**
* @Description: 扫描过滤接口,根据需要实现过滤规则
* @author: <a href="http://www.xiaoyaoyou1212.com">DAWI</a>
* @date: 17/9/10 18:19.
*/
public interface IScanFilter {
BluetoothLeDeviceStore onFilter(BluetoothLeDevice bluetoothLeDevice);
BluetoothLeDevice onFilter(BluetoothLeDevice bluetoothLeDevice);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.vise.baseble.callback.scan;

import com.vise.baseble.model.BluetoothLeDevice;
import com.vise.baseble.model.BluetoothLeDeviceStore;

import java.util.List;

Expand All @@ -20,33 +19,32 @@ public ListFilterScanCallback(IScanCallback scanCallback) {

public ListFilterScanCallback setDeviceNameList(List<String> deviceNameList) {
this.deviceNameList = deviceNameList;
bluetoothLeDeviceStore.clear();
return this;
}

public ListFilterScanCallback setDeviceMacList(List<String> deviceMacList) {
this.deviceMacList = deviceMacList;
bluetoothLeDeviceStore.clear();
return this;
}

@Override
public BluetoothLeDeviceStore onFilter(BluetoothLeDevice bluetoothLeDevice) {
public BluetoothLeDevice onFilter(BluetoothLeDevice bluetoothLeDevice) {
BluetoothLeDevice tempDevice = null;
if (deviceNameList != null && deviceNameList.size() > 0) {
for (String deviceName : deviceNameList) {
if (bluetoothLeDevice != null && bluetoothLeDevice.getName() != null && deviceName != null
&& deviceName.equalsIgnoreCase(bluetoothLeDevice.getName().trim())) {
bluetoothLeDeviceStore.addDevice(bluetoothLeDevice);
tempDevice = bluetoothLeDevice;
}
}
} else if (deviceMacList != null && deviceMacList.size() > 0) {
for (String deviceMac : deviceMacList) {
if (bluetoothLeDevice != null && bluetoothLeDevice.getAddress() != null && deviceMac != null
&& deviceMac.equalsIgnoreCase(bluetoothLeDevice.getAddress().trim())) {
bluetoothLeDeviceStore.addDevice(bluetoothLeDevice);
tempDevice = bluetoothLeDevice;
}
}
}
return bluetoothLeDeviceStore;
return tempDevice;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import android.text.TextUtils;

import com.vise.baseble.model.BluetoothLeDevice;
import com.vise.baseble.model.BluetoothLeDeviceStore;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand All @@ -29,32 +28,29 @@ public RegularFilterScanCallback setRegularDeviceName(String regularDeviceName)
if (!TextUtils.isEmpty(this.regularDeviceName)) {
pattern = Pattern.compile(this.regularDeviceName);
}
bluetoothLeDeviceStore.clear();
return this;
}

public RegularFilterScanCallback setDeviceRssi(int deviceRssi) {
this.deviceRssi = deviceRssi;
bluetoothLeDeviceStore.clear();
return this;
}

@Override
public BluetoothLeDeviceStore onFilter(BluetoothLeDevice bluetoothLeDevice) {
public BluetoothLeDevice onFilter(BluetoothLeDevice bluetoothLeDevice) {
BluetoothLeDevice tempDevice = null;
String tempName = bluetoothLeDevice.getName();
int tempRssi = bluetoothLeDevice.getRssi();
matcher = pattern.matcher(tempName);
if (this.deviceRssi < 0) {
if (matcher.matches() && tempRssi >= this.deviceRssi) {
bluetoothLeDeviceStore.addDevice(bluetoothLeDevice);
} else if (matcher.matches() && tempRssi < this.deviceRssi) {
bluetoothLeDeviceStore.removeDevice(bluetoothLeDevice);
tempDevice = bluetoothLeDevice;
}
} else {
if (matcher.matches()) {
bluetoothLeDeviceStore.addDevice(bluetoothLeDevice);
tempDevice = bluetoothLeDevice;
}
}
return bluetoothLeDeviceStore;
return tempDevice;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import android.os.Looper;

import com.vise.baseble.ViseBle;
import com.vise.baseble.common.BleConstant;
import com.vise.baseble.common.BleConfig;
import com.vise.baseble.model.BluetoothLeDevice;
import com.vise.baseble.model.BluetoothLeDeviceStore;

Expand All @@ -16,8 +16,7 @@
* @date: 17/8/1 22:58.
*/
public class ScanCallback implements BluetoothAdapter.LeScanCallback, IScanFilter {
protected Handler handler = new Handler(Looper.getMainLooper());
protected int scanTimeout = BleConstant.TIME_FOREVER; //表示一直扫描
protected Handler handler = new Handler(Looper.myLooper());
protected boolean isScan = true;//是否开始扫描
protected boolean isScanning = false;//是否正在扫描
protected BluetoothLeDeviceStore bluetoothLeDeviceStore;//用来存储扫描到的设备
Expand All @@ -29,12 +28,6 @@ public ScanCallback(IScanCallback scanCallback) {
throw new NullPointerException("this scanCallback is null!");
}
bluetoothLeDeviceStore = new BluetoothLeDeviceStore();

}

public ScanCallback setScanTimeout(int scanTimeout) {
this.scanTimeout = scanTimeout;
return this;
}

public ScanCallback setScan(boolean scan) {
Expand All @@ -46,22 +39,21 @@ public boolean isScanning() {
return isScanning;
}

public int getScanTimeout() {
return scanTimeout;
}

public void scan() {
if (isScan) {
if (isScanning) {
return;
}
bluetoothLeDeviceStore.clear();
if (scanTimeout > 0) {
if (BleConfig.getInstance().getScanTimeout() > 0) {
handler.postDelayed(new Runnable() {
@Override
public void run() {
isScanning = false;
ViseBle.getInstance().stopLeScan(ScanCallback.this);

if (ViseBle.getInstance().getBluetoothAdapter() != null) {
ViseBle.getInstance().getBluetoothAdapter().stopLeScan(ScanCallback.this);
}

if (bluetoothLeDeviceStore.getDeviceMap() != null
&& bluetoothLeDeviceStore.getDeviceMap().size() > 0) {
Expand All @@ -70,29 +62,38 @@ public void run() {
scanCallback.onScanTimeout();
}
}
}, scanTimeout);
}, BleConfig.getInstance().getScanTimeout());
}
isScanning = true;
ViseBle.getInstance().startLeScan(ScanCallback.this);
if (ViseBle.getInstance().getBluetoothAdapter() != null) {
ViseBle.getInstance().getBluetoothAdapter().startLeScan(ScanCallback.this);
}
} else {
isScanning = false;
ViseBle.getInstance().stopLeScan(ScanCallback.this);
if (ViseBle.getInstance().getBluetoothAdapter() != null) {
ViseBle.getInstance().getBluetoothAdapter().stopLeScan(ScanCallback.this);
}
}
}

public ScanCallback removeHandlerMsg() {
handler.removeCallbacksAndMessages(null);
bluetoothLeDeviceStore.clear();
return this;
}

@Override
public void onLeScan(BluetoothDevice bluetoothDevice, int rssi, byte[] scanRecord) {
scanCallback.onDeviceFound(onFilter(new BluetoothLeDevice(bluetoothDevice, rssi, scanRecord, System.currentTimeMillis())));
BluetoothLeDevice bluetoothLeDevice = new BluetoothLeDevice(bluetoothDevice, rssi, scanRecord, System.currentTimeMillis());
BluetoothLeDevice filterDevice = onFilter(bluetoothLeDevice);
if (filterDevice != null) {
bluetoothLeDeviceStore.addDevice(filterDevice);
scanCallback.onDeviceFound(filterDevice);
}
}

@Override
public BluetoothLeDeviceStore onFilter(BluetoothLeDevice bluetoothLeDevice) {
bluetoothLeDeviceStore.addDevice(bluetoothLeDevice);
return bluetoothLeDeviceStore;
public BluetoothLeDevice onFilter(BluetoothLeDevice bluetoothLeDevice) {
return bluetoothLeDevice;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.vise.baseble.ViseBle;
import com.vise.baseble.model.BluetoothLeDevice;
import com.vise.baseble.model.BluetoothLeDeviceStore;

import java.util.concurrent.atomic.AtomicBoolean;

Expand All @@ -22,25 +21,25 @@ public SingleFilterScanCallback(IScanCallback scanCallback) {

public ScanCallback setDeviceName(String deviceName) {
this.deviceName = deviceName;
bluetoothLeDeviceStore.clear();
return this;
}

public ScanCallback setDeviceMac(String deviceMac) {
this.deviceMac = deviceMac;
bluetoothLeDeviceStore.clear();
return this;
}

@Override
public BluetoothLeDeviceStore onFilter(BluetoothLeDevice bluetoothLeDevice) {
public BluetoothLeDevice onFilter(BluetoothLeDevice bluetoothLeDevice) {
BluetoothLeDevice tempDevice = null;
if (!hasFound.get()) {
if (bluetoothLeDevice != null && bluetoothLeDevice.getAddress() != null && deviceMac != null
&& deviceMac.equalsIgnoreCase(bluetoothLeDevice.getAddress().trim())) {
hasFound.set(true);
isScanning = false;
removeHandlerMsg();
ViseBle.getInstance().stopScan(SingleFilterScanCallback.this);
tempDevice = bluetoothLeDevice;
bluetoothLeDeviceStore.addDevice(bluetoothLeDevice);
scanCallback.onScanFinish(bluetoothLeDeviceStore);
} else if (bluetoothLeDevice != null && bluetoothLeDevice.getName() != null && deviceName != null
Expand All @@ -49,10 +48,11 @@ public BluetoothLeDeviceStore onFilter(BluetoothLeDevice bluetoothLeDevice) {
isScanning = false;
removeHandlerMsg();
ViseBle.getInstance().stopScan(SingleFilterScanCallback.this);
tempDevice = bluetoothLeDevice;
bluetoothLeDeviceStore.addDevice(bluetoothLeDevice);
scanCallback.onScanFinish(bluetoothLeDeviceStore);
}
}
return bluetoothLeDeviceStore;
return tempDevice;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import android.os.ParcelUuid;

import com.vise.baseble.model.BluetoothLeDevice;
import com.vise.baseble.model.BluetoothLeDeviceStore;

import java.util.UUID;

Expand All @@ -21,27 +20,26 @@ public UuidFilterScanCallback(IScanCallback scanCallback) {

public UuidFilterScanCallback setUuid(String uuid) {
this.uuid = UUID.fromString(uuid);
bluetoothLeDeviceStore.clear();
return this;
}

public UuidFilterScanCallback setUuid(UUID uuid) {
this.uuid = uuid;
bluetoothLeDeviceStore.clear();
return this;
}

@Override
public BluetoothLeDeviceStore onFilter(BluetoothLeDevice bluetoothLeDevice) {
public BluetoothLeDevice onFilter(BluetoothLeDevice bluetoothLeDevice) {
BluetoothLeDevice tempDevice = null;
if (bluetoothLeDevice != null && bluetoothLeDevice.getDevice() != null
&& bluetoothLeDevice.getDevice().getUuids() != null
&& bluetoothLeDevice.getDevice().getUuids().length > 0) {
for (ParcelUuid parcelUuid : bluetoothLeDevice.getDevice().getUuids()) {
if (uuid != null && uuid == parcelUuid.getUuid()) {
bluetoothLeDeviceStore.addDevice(bluetoothLeDevice);
tempDevice = bluetoothLeDevice;
}
}
}
return bluetoothLeDeviceStore;
return tempDevice;
}
}
2 changes: 2 additions & 0 deletions baseble/src/main/java/com/vise/baseble/core/DeviceMirror.java
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,8 @@ public synchronized boolean refreshDeviceCache() {
* 主动断开设备连接
*/
public synchronized void disconnect() {
connectState = ConnectState.CONNECT_INIT;
connectRetryCount = 0;
if (bluetoothGatt != null) {
isActiveDisconnect = true;
bluetoothGatt.disconnect();
Expand Down
Loading

0 comments on commit 823eebf

Please sign in to comment.