diff --git a/baseble/src/main/java/com/vise/baseble/ViseBle.java b/baseble/src/main/java/com/vise/baseble/ViseBle.java index 114c6a7..16f886d 100644 --- a/baseble/src/main/java/com/vise/baseble/ViseBle.java +++ b/baseble/src/main/java/com/vise/baseble/ViseBle.java @@ -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); - } - } - /** * 开始扫描 * @@ -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(); } /** @@ -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) { } @@ -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) { } diff --git a/baseble/src/main/java/com/vise/baseble/callback/scan/IScanCallback.java b/baseble/src/main/java/com/vise/baseble/callback/scan/IScanCallback.java index 1249a06..f6a82b8 100644 --- a/baseble/src/main/java/com/vise/baseble/callback/scan/IScanCallback.java +++ b/baseble/src/main/java/com/vise/baseble/callback/scan/IScanCallback.java @@ -1,5 +1,6 @@ package com.vise.baseble.callback.scan; +import com.vise.baseble.model.BluetoothLeDevice; import com.vise.baseble.model.BluetoothLeDeviceStore; /** @@ -9,7 +10,7 @@ */ public interface IScanCallback { //发现设备 - void onDeviceFound(BluetoothLeDeviceStore bluetoothLeDeviceStore); + void onDeviceFound(BluetoothLeDevice bluetoothLeDevice); //扫描完成 void onScanFinish(BluetoothLeDeviceStore bluetoothLeDeviceStore); diff --git a/baseble/src/main/java/com/vise/baseble/callback/scan/IScanFilter.java b/baseble/src/main/java/com/vise/baseble/callback/scan/IScanFilter.java index 99e053f..47042a7 100644 --- a/baseble/src/main/java/com/vise/baseble/callback/scan/IScanFilter.java +++ b/baseble/src/main/java/com/vise/baseble/callback/scan/IScanFilter.java @@ -1,7 +1,6 @@ package com.vise.baseble.callback.scan; import com.vise.baseble.model.BluetoothLeDevice; -import com.vise.baseble.model.BluetoothLeDeviceStore; /** * @Description: 扫描过滤接口,根据需要实现过滤规则 @@ -9,5 +8,5 @@ * @date: 17/9/10 18:19. */ public interface IScanFilter { - BluetoothLeDeviceStore onFilter(BluetoothLeDevice bluetoothLeDevice); + BluetoothLeDevice onFilter(BluetoothLeDevice bluetoothLeDevice); } diff --git a/baseble/src/main/java/com/vise/baseble/callback/scan/ListFilterScanCallback.java b/baseble/src/main/java/com/vise/baseble/callback/scan/ListFilterScanCallback.java index 775383a..625c982 100644 --- a/baseble/src/main/java/com/vise/baseble/callback/scan/ListFilterScanCallback.java +++ b/baseble/src/main/java/com/vise/baseble/callback/scan/ListFilterScanCallback.java @@ -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; @@ -20,33 +19,32 @@ public ListFilterScanCallback(IScanCallback scanCallback) { public ListFilterScanCallback setDeviceNameList(List deviceNameList) { this.deviceNameList = deviceNameList; - bluetoothLeDeviceStore.clear(); return this; } public ListFilterScanCallback setDeviceMacList(List 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; } } diff --git a/baseble/src/main/java/com/vise/baseble/callback/scan/RegularFilterScanCallback.java b/baseble/src/main/java/com/vise/baseble/callback/scan/RegularFilterScanCallback.java index 1688fe5..72a1d4e 100644 --- a/baseble/src/main/java/com/vise/baseble/callback/scan/RegularFilterScanCallback.java +++ b/baseble/src/main/java/com/vise/baseble/callback/scan/RegularFilterScanCallback.java @@ -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; @@ -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; } } diff --git a/baseble/src/main/java/com/vise/baseble/callback/scan/ScanCallback.java b/baseble/src/main/java/com/vise/baseble/callback/scan/ScanCallback.java index 5b79aec..3454b92 100644 --- a/baseble/src/main/java/com/vise/baseble/callback/scan/ScanCallback.java +++ b/baseble/src/main/java/com/vise/baseble/callback/scan/ScanCallback.java @@ -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; @@ -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;//用来存储扫描到的设备 @@ -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) { @@ -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) { @@ -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; } } diff --git a/baseble/src/main/java/com/vise/baseble/callback/scan/SingleFilterScanCallback.java b/baseble/src/main/java/com/vise/baseble/callback/scan/SingleFilterScanCallback.java index 1ef6538..6b4f178 100644 --- a/baseble/src/main/java/com/vise/baseble/callback/scan/SingleFilterScanCallback.java +++ b/baseble/src/main/java/com/vise/baseble/callback/scan/SingleFilterScanCallback.java @@ -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; @@ -22,18 +21,17 @@ 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())) { @@ -41,6 +39,7 @@ public BluetoothLeDeviceStore onFilter(BluetoothLeDevice bluetoothLeDevice) { 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 @@ -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; } } diff --git a/baseble/src/main/java/com/vise/baseble/callback/scan/UuidFilterScanCallback.java b/baseble/src/main/java/com/vise/baseble/callback/scan/UuidFilterScanCallback.java index d92cf06..27868c6 100644 --- a/baseble/src/main/java/com/vise/baseble/callback/scan/UuidFilterScanCallback.java +++ b/baseble/src/main/java/com/vise/baseble/callback/scan/UuidFilterScanCallback.java @@ -3,7 +3,6 @@ import android.os.ParcelUuid; import com.vise.baseble.model.BluetoothLeDevice; -import com.vise.baseble.model.BluetoothLeDeviceStore; import java.util.UUID; @@ -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; } } diff --git a/baseble/src/main/java/com/vise/baseble/core/DeviceMirror.java b/baseble/src/main/java/com/vise/baseble/core/DeviceMirror.java index 95eb95b..3096c10 100644 --- a/baseble/src/main/java/com/vise/baseble/core/DeviceMirror.java +++ b/baseble/src/main/java/com/vise/baseble/core/DeviceMirror.java @@ -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(); diff --git a/newapp/src/main/java/com/vise/bledemo/activity/DeviceScanActivity.java b/newapp/src/main/java/com/vise/bledemo/activity/DeviceScanActivity.java index d8125d7..2cfa9d4 100644 --- a/newapp/src/main/java/com/vise/bledemo/activity/DeviceScanActivity.java +++ b/newapp/src/main/java/com/vise/bledemo/activity/DeviceScanActivity.java @@ -34,13 +34,16 @@ public class DeviceScanActivity extends AppCompatActivity { //设备扫描结果展示适配器 private DeviceAdapter adapter; + private BluetoothLeDeviceStore bluetoothLeDeviceStore = new BluetoothLeDeviceStore(); + /** * 扫描回调 */ private ScanCallback periodScanCallback = new ScanCallback(new IScanCallback() { @Override - public void onDeviceFound(final BluetoothLeDeviceStore bluetoothLeDeviceStore) { - ViseLog.i("Founded Scan Device:" + bluetoothLeDeviceStore); + public void onDeviceFound(final BluetoothLeDevice bluetoothLeDevice) { + ViseLog.i("Founded Scan Device:" + bluetoothLeDevice); + bluetoothLeDeviceStore.addDevice(bluetoothLeDevice); runOnUiThread(new Runnable() { @Override public void run() { @@ -102,6 +105,7 @@ protected void onPause() { super.onPause(); stopScan(); invalidateOptionsMenu(); + bluetoothLeDeviceStore.clear(); } @Override