android 单向验证,Android MQTT 单向认证以及手动重连

/**

* Reconnect

* Only appropriate if cleanSession is false and we were connected.

* Declare as synchronized to avoid multiple calls to this method to send connect

* multiple times

*/

synchronized void reconnect() {

if (myClient ==null) {

service.traceError(TAG,"Reconnect myClient = null. Will not do reconnect");

return;

}

if (isConnecting) {

service.traceDebug(TAG, "The client is connecting. Reconnect return directly.");

return ;

}

if(!service.isOnline()){

service.traceDebug(TAG,

"The network is not reachable. Will not do reconnect");

return;

}

if(connectOptions.isAutomaticReconnect()){

//The Automatic reconnect functionality is enabled here

Log.i(TAG, "Requesting Automatic reconnect using New Java AC");

final Bundle resultBundle =new Bundle();

resultBundle.putString(

MqttServiceConstants.CALLBACK_ACTIVITY_TOKEN,

reconnectActivityToken);

resultBundle.putString(

MqttServiceConstants.CALLBACK_INVOCATION_CONTEXT, null);

resultBundle.putString(MqttServiceConstants.CALLBACK_ACTION,

MqttServiceConstants.CONNECT_ACTION);

try {

myClient.reconnect();

}catch (MqttException ex){

Log.e(TAG, "Exception occurred attempting to reconnect: " + ex.getMessage());

setConnectingState(false);

handleException(resultBundle, ex);

}

}else if (disconnected && !cleanSession) {

// use the activityToke the same with action connect

service.traceDebug(TAG,"Do Real Reconnect!");

final Bundle resultBundle =new Bundle();

resultBundle.putString(

MqttServiceConstants.CALLBACK_ACTIVITY_TOKEN,

reconnectActivityToken);

resultBundle.putString(

MqttServiceConstants.CALLBACK_INVOCATION_CONTEXT, null);

resultBundle.putString(MqttServiceConstants.CALLBACK_ACTION,

MqttServiceConstants.CONNECT_ACTION);

try {

IMqttActionListener listener =new MqttConnectionListener(resultBundle) {

@Override

public void onSuccess(IMqttToken asyncActionToken) {

// since the device's cpu can go to sleep, acquire a

// wakelock and drop it later.

service.traceDebug(TAG,"Reconnect Success!");

service.traceDebug(TAG,"DeliverBacklog when reconnect.");

doAfterConnectSuccess(resultBundle);

}

@Override

public void onFailure(IMqttToken asyncActionToken, Throwable exception) {

resultBundle.putString(

MqttServiceConstants.CALLBACK_ERROR_MESSAGE,

exception.getLocalizedMessage());

resultBundle.putSerializable(

MqttServiceConstants.CALLBACK_EXCEPTION,

exception);

service.callbackToActivity(clientHandle, Status.ERROR,

resultBundle);

doAfterConnectFail(resultBundle);

}

};

myClient.connect(connectOptions, null, listener);

setConnectingState(true);

}catch (MqttException e) {

service.traceError(TAG, "Cannot reconnect to remote server." + e.getMessage());

setConnectingState(false);

handleException(resultBundle, e);

}catch (Exception e){

/*  TODO: Added Due to: https://github.com/eclipse/paho.mqtt.android/issues/101

For some reason in a small number of cases, myClient is null here and so

a NullPointer Exception is thrown. This is a workaround to pass the exception

up to the application. myClient should not be null so more investigation is

required.

*/

service.traceError(TAG, "Cannot reconnect to remote server." + e.getMessage());

setConnectingState(false);

MqttException newEx =new MqttException(MqttException.REASON_CODE_UNEXPECTED_ERROR, e.getCause());

handleException(resultBundle, newEx);

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值