Skip to content

Commit

Permalink
增加连接错误回调
Browse files Browse the repository at this point in the history
  • Loading branch information
zengfansheng committed Apr 8, 2022
1 parent 1cc429d commit d040135
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 2 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.3.6'
def lib_version = '1.3.7'
def user_name = 'wandersnail'
def lib_group = 'cn.wandersnail'
def lib_artifact = 'easyble-x'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public class ConnectionConfiguration {
*/
boolean useAutoConnectAfterConnectionFailure = true;
@RequiresApi(Build.VERSION_CODES.M)
int transport = BluetoothDevice.TRANSPORT_LE;
int transport = BluetoothDevice.TRANSPORT_AUTO;
@RequiresApi(Build.VERSION_CODES.O)
int phy = BluetoothDevice.PHY_LE_1M_MASK;
@NonNull
Expand Down
10 changes: 10 additions & 0 deletions library/src/main/java/cn/wandersnail/ble/ConnectionImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,7 @@ private void doOnConnectionStateChange(int status, int newState) {
logE(Logger.TYPE_CONNECTION_STATE, "GATT error! [status: %d, name: %s, addr: %s]",
status, device.name, device.address);
connectFailed = true;
notifyConnectionError(status);
if (status == 133) {
doClearTaskAndRefresh();
} else {
Expand Down Expand Up @@ -382,6 +383,7 @@ private void doOnServicesDiscovered(int status) {
}
} else {
connectFailed = true;
notifyConnectionError(status);
doClearTaskAndRefresh();
logE(Logger.TYPE_CONNECTION_STATE, "GATT error! [status: %d, name: %s, addr: %s]",
status, device.name, device.address);
Expand Down Expand Up @@ -982,6 +984,14 @@ private void logD(int type, String format, Object... args) {
log(Log.DEBUG, type, format, args);
}

private void notifyConnectionError(int status) {
MethodInfo info = MethodInfoGenerator.onConnectionError(status);
observable.notifyObservers(info);
if (observer != null) {
posterDispatcher.post(observer, info);
}
}

private void notifyRequestFailed(GenericRequest request, int failType) {
MethodInfo info = MethodInfoGenerator.onRequestFailed(request, failType, request.value);
handleCallbacks(request.callback, info);
Expand Down
7 changes: 7 additions & 0 deletions library/src/main/java/cn/wandersnail/ble/EventObserver.java
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,11 @@ default void onConnectFailed(@NonNull Device device, int failType) {
*/
default void onConnectTimeout(@NonNull Device device, int type) {
}

/**
* 连接错误
*
* @param status 状态码
*/
default void onConnectionError(int status) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ static MethodInfo onConnectFailed(Device device, int failType) {
new MethodInfo.Parameter(int.class, failType));
}

static MethodInfo onConnectionError(int status) {
return new MethodInfo("onConnectionError", new MethodInfo.Parameter(int.class, status));
}

static MethodInfo onConnectTimeout(Device device, int type) {
return new MethodInfo("onConnectTimeout", new MethodInfo.Parameter(Device.class, device),
new MethodInfo.Parameter(int.class, type));
Expand Down

0 comments on commit d040135

Please sign in to comment.