Skip to content

Commit

Permalink
优化搜索回调,超时任务
Browse files Browse the repository at this point in the history
  • Loading branch information
zengfansheng committed Nov 8, 2022
1 parent 03b9c9e commit d1561fa
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 6 deletions.
2 changes: 1 addition & 1 deletion library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ signing {
sign configurations.archives
}

def lib_version = '1.4.2'
def lib_version = '1.4.3'
def user_name = 'wandersnail'
def lib_group = 'cn.wandersnail'
def lib_artifact = 'easyble-x'
Expand Down
22 changes: 17 additions & 5 deletions library/src/main/java/cn/wandersnail/ble/AbstractScanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,17 @@
import android.util.Log;
import android.util.SparseArray;

import androidx.annotation.CallSuper;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

import java.lang.reflect.Method;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArrayList;

import androidx.annotation.CallSuper;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import cn.wandersnail.ble.callback.ScanListener;
import cn.wandersnail.ble.util.Logger;

Expand Down Expand Up @@ -221,7 +222,18 @@ void parseScanResult(BluetoothDevice device, boolean isConnectedBySys, @Nullable
@Override
public void startScan(@NonNull Context context) {
synchronized (this) {
if (!isBtEnabled() || (getType() != ScannerType.CLASSIC && isScanning) || !isReady()) {
if (getType() != ScannerType.CLASSIC && isScanning) {
return;
}
if (!isReady()) {
String errorMsg = "Scanner not ready.";
handleScanCallback(false, null, false, ScanListener.ERROR_SCANNER_NOT_READY, errorMsg);
logger.log(Log.ERROR, Logger.TYPE_SCAN_STATE, errorMsg);
}
if (!isBtEnabled()) {
String errorMsg = "Bluetooth is not turned on.";
handleScanCallback(false, null, false, ScanListener.ERROR_BLUETOOTH_OFF, errorMsg);
logger.log(Log.ERROR, Logger.TYPE_SCAN_STATE, errorMsg);
return;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -943,6 +943,7 @@ private void handlePhyChange(boolean read, int txPhy, int rxPhy, int status) {

private void handleGattStatusFailed(int status) {
if (currentRequest != null) {
connHandler.removeMessages(MSG_REQUEST_TIMEOUT);//移除超时检测
handleFailedCallback(currentRequest, REQUEST_FAIL_TYPE_GATT_STATUS_FAILED, status, false);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ public interface ScanListener {
* 缺少连接权限。 {@link Manifest.permission#BLUETOOTH_CONNECT}
*/
int ERROR_LACK_CONNECT_PERMISSION = 4;
/**
* 蓝牙未开启
*/
int ERROR_BLUETOOTH_OFF = 5;
/**
* 扫描器未准备好
*/
int ERROR_SCANNER_NOT_READY = 6;


/**
Expand Down

0 comments on commit d1561fa

Please sign in to comment.