初始版本
This commit is contained in:
@ -0,0 +1,46 @@
|
||||
package com.qidian.baseble.exception;
|
||||
|
||||
import com.qidian.baseble.common.BleExceptionCode;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* @Description: BLE异常基类
|
||||
* @author: <a href="http://www.xiaoyaoyou1212.com">DAWI</a>
|
||||
* @date: 16/8/14 10:28.
|
||||
*/
|
||||
public class BleException implements Serializable {
|
||||
private BleExceptionCode code;
|
||||
private String description;
|
||||
|
||||
public BleException(BleExceptionCode code, String description) {
|
||||
this.code = code;
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public BleExceptionCode getCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
public BleException setCode(BleExceptionCode code) {
|
||||
this.code = code;
|
||||
return this;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public BleException setDescription(String description) {
|
||||
this.description = description;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "BleException{" +
|
||||
"code=" + code +
|
||||
", description='" + description + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,47 @@
|
||||
package com.qidian.baseble.exception;
|
||||
|
||||
import android.bluetooth.BluetoothGatt;
|
||||
|
||||
import com.qidian.baseble.common.BleExceptionCode;
|
||||
|
||||
/**
|
||||
* @Description: 连接异常
|
||||
* @author: <a href="http://www.xiaoyaoyou1212.com">DAWI</a>
|
||||
* @date: 16/8/14 10:29.
|
||||
*/
|
||||
public class ConnectException extends BleException {
|
||||
private BluetoothGatt bluetoothGatt;
|
||||
private int gattStatus;
|
||||
|
||||
public ConnectException(BluetoothGatt bluetoothGatt, int gattStatus) {
|
||||
super(BleExceptionCode.CONNECT_ERR, "Connect Exception Occurred! ");
|
||||
this.bluetoothGatt = bluetoothGatt;
|
||||
this.gattStatus = gattStatus;
|
||||
}
|
||||
|
||||
public int getGattStatus() {
|
||||
return gattStatus;
|
||||
}
|
||||
|
||||
public ConnectException setGattStatus(int gattStatus) {
|
||||
this.gattStatus = gattStatus;
|
||||
return this;
|
||||
}
|
||||
|
||||
public BluetoothGatt getBluetoothGatt() {
|
||||
return bluetoothGatt;
|
||||
}
|
||||
|
||||
public ConnectException setBluetoothGatt(BluetoothGatt bluetoothGatt) {
|
||||
this.bluetoothGatt = bluetoothGatt;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "ConnectException{" +
|
||||
"gattStatus=" + gattStatus +
|
||||
", bluetoothGatt=" + bluetoothGatt +
|
||||
"} " + super.toString();
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,33 @@
|
||||
package com.qidian.baseble.exception;
|
||||
|
||||
import com.qidian.baseble.common.BleExceptionCode;
|
||||
|
||||
/**
|
||||
* @Description: Gatt异常
|
||||
* @author: <a href="http://www.xiaoyaoyou1212.com">DAWI</a>
|
||||
* @date: 16/8/14 10:30.
|
||||
*/
|
||||
public class GattException extends BleException {
|
||||
private int gattStatus;
|
||||
|
||||
public GattException(int gattStatus) {
|
||||
super(BleExceptionCode.GATT_ERR, "Gatt Exception Occurred! ");
|
||||
this.gattStatus = gattStatus;
|
||||
}
|
||||
|
||||
public int getGattStatus() {
|
||||
return gattStatus;
|
||||
}
|
||||
|
||||
public GattException setGattStatus(int gattStatus) {
|
||||
this.gattStatus = gattStatus;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "GattException{" +
|
||||
"gattStatus=" + gattStatus +
|
||||
'}' + super.toString();
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,14 @@
|
||||
package com.qidian.baseble.exception;
|
||||
|
||||
import com.qidian.baseble.common.BleExceptionCode;
|
||||
|
||||
/**
|
||||
* @Description: 初始化异常
|
||||
* @author: <a href="http://www.xiaoyaoyou1212.com">DAWI</a>
|
||||
* @date: 16/8/14 10:30.
|
||||
*/
|
||||
public class InitiatedException extends BleException {
|
||||
public InitiatedException() {
|
||||
super(BleExceptionCode.INITIATED_ERR, "Initiated Exception Occurred! ");
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,14 @@
|
||||
package com.qidian.baseble.exception;
|
||||
|
||||
import com.qidian.baseble.common.BleExceptionCode;
|
||||
|
||||
/**
|
||||
* @Description: 其他异常
|
||||
* @author: <a href="http://www.xiaoyaoyou1212.com">DAWI</a>
|
||||
* @date: 16/8/14 10:32.
|
||||
*/
|
||||
public class OtherException extends BleException {
|
||||
public OtherException(String description) {
|
||||
super(BleExceptionCode.OTHER_ERR, description);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,14 @@
|
||||
package com.qidian.baseble.exception;
|
||||
|
||||
import com.qidian.baseble.common.BleExceptionCode;
|
||||
|
||||
/**
|
||||
* @Description: 超时异常
|
||||
* @author: <a href="http://www.xiaoyaoyou1212.com">DAWI</a>
|
||||
* @date: 16/8/14 10:29.
|
||||
*/
|
||||
public class TimeoutException extends BleException {
|
||||
public TimeoutException() {
|
||||
super(BleExceptionCode.TIMEOUT, "Timeout Exception Occurred! ");
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,57 @@
|
||||
package com.qidian.baseble.exception.handler;
|
||||
|
||||
import com.qidian.baseble.exception.BleException;
|
||||
import com.qidian.baseble.exception.ConnectException;
|
||||
import com.qidian.baseble.exception.GattException;
|
||||
import com.qidian.baseble.exception.InitiatedException;
|
||||
import com.qidian.baseble.exception.OtherException;
|
||||
import com.qidian.baseble.exception.TimeoutException;
|
||||
|
||||
/**
|
||||
* @Description: 异常处理
|
||||
* @author: <a href="http://www.xiaoyaoyou1212.com">DAWI</a>
|
||||
* @date: 16/8/14 10:35.
|
||||
*/
|
||||
public abstract class BleExceptionHandler {
|
||||
public BleExceptionHandler handleException(BleException exception) {
|
||||
if (exception != null) {
|
||||
if (exception instanceof ConnectException) {
|
||||
onConnectException((ConnectException) exception);
|
||||
} else if (exception instanceof GattException) {
|
||||
onGattException((GattException) exception);
|
||||
} else if (exception instanceof TimeoutException) {
|
||||
onTimeoutException((TimeoutException) exception);
|
||||
} else if (exception instanceof InitiatedException) {
|
||||
onInitiatedException((InitiatedException) exception);
|
||||
} else {
|
||||
onOtherException((OtherException) exception);
|
||||
}
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* connect failed
|
||||
*/
|
||||
protected abstract void onConnectException(ConnectException e);
|
||||
|
||||
/**
|
||||
* gatt error status
|
||||
*/
|
||||
protected abstract void onGattException(GattException e);
|
||||
|
||||
/**
|
||||
* operation timeout
|
||||
*/
|
||||
protected abstract void onTimeoutException(TimeoutException e);
|
||||
|
||||
/**
|
||||
* operation inititiated error
|
||||
*/
|
||||
protected abstract void onInitiatedException(InitiatedException e);
|
||||
|
||||
/**
|
||||
* other exceptions
|
||||
*/
|
||||
protected abstract void onOtherException(OtherException e);
|
||||
}
|
||||
@ -0,0 +1,40 @@
|
||||
package com.qidian.baseble.exception.handler;
|
||||
|
||||
import com.qidian.baseble.exception.ConnectException;
|
||||
import com.qidian.baseble.exception.GattException;
|
||||
import com.qidian.baseble.exception.InitiatedException;
|
||||
import com.qidian.baseble.exception.OtherException;
|
||||
import com.qidian.baseble.exception.TimeoutException;
|
||||
import com.vise.log.ViseLog;
|
||||
|
||||
/**
|
||||
* @Description: 异常默认处理
|
||||
* @author: <a href="http://www.xiaoyaoyou1212.com">DAWI</a>
|
||||
* @date: 16/8/14 10:35.
|
||||
*/
|
||||
public class DefaultBleExceptionHandler extends BleExceptionHandler {
|
||||
@Override
|
||||
protected void onConnectException(ConnectException e) {
|
||||
ViseLog.e(e.getDescription());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onGattException(GattException e) {
|
||||
ViseLog.e(e.getDescription());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onTimeoutException(TimeoutException e) {
|
||||
ViseLog.e(e.getDescription());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onInitiatedException(InitiatedException e) {
|
||||
ViseLog.e(e.getDescription());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onOtherException(OtherException e) {
|
||||
ViseLog.e(e.getDescription());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user