我正在尝试收听TLS(SLL)之后的MQTT网络,尽管其他平台/开发人员也无法连接。我没有收到任何身份验证错误,超时错误或其他异常,只是什么也没有发生。
在下面查看我的Android Mqtt客户端代码:
package myMqttPackage;
import android.os.Bundle;
import android.os.Message;
import android.util.Log;
import org.eclipse.paho.android.service.MqttAndroidClient;
import org.eclipse.paho.client.mqttv3.DisconnectedBufferOptions;
import org.eclipse.paho.client.mqttv3.IMqttActionListener;
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.IMqttToken;
import org.eclipse.paho.client.mqttv3.MqttCallbackExtended;
import org.eclipse.paho.client.mqttv3.MqttClientPersistence;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.eclipse.paho.client.mqttv3.MqttPersistable;
import org.eclipse.paho.client.mqttv3.MqttPersistenceException;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Properties;
import iotrecord.IoTRecord;
import xyz.controller.core.Application.DeviceApp;
import xyz.controller.core.services.MqttBrokerService;
import xyz.controller.core.info.AlarmTopic;
import xyz.controller.core.info.AlarmTopicTool;
import xyz.controller.core.info.MasterTopicTool;
import xyz.controller.core.info.MasterTopicV2;
import xyz.controller.core.info.RawECG;
import xyz.controller.core.info.RawECGTopicTool;
import xyz.controller.core.info.StartSession;
import xyz.controller.core.info.StartSessionTool;
import xyz.controller.core.info.StopSession;
import xyz.controller.core.info.StopSessionTool;
import xyz.controller.core.info.infoRecordFactory;
public class MqttAdhocClient {
private static final String TAG = "MqttAdhocClient";
private MqttAndroidClient mqttClient;
private MqttConnectOptions connectOptions;
private static final int QOS = 0;
private IoTRecord latestIoTRecord;
private String ID;
public MqttAdhocClient(String ID) {
this.ID = ID;
}
public String getID() {
return ID;
}
public void setID(String ID) {
this.ID = ID;
}
//setup
public MqttAndroidClient setupClient(String serverPath, String serverPort, boolean authentiction, String username, String password, String clientId, boolean TLS)
{
Log.i(TAG,"setupClient()");
String mqttServerURI;
String [] serverURI = new String[1];
// set options for establishing a broker connection
connectOptions = new MqttConnectOptions();
Properties prop = new Properties();
if (!TLS) {
mqttServerURI = "tcp://" + serverPath + ":" + serverPort;
serverURI[0] = mqttServerURI;
}
else
{
mqttServerURI = "ssl://" + serverPath + ":" + serverPort;
serverURI[0] = mqttServerURI;
//com.ibm.ssl.protocol
prop.setProperty("com.ibm.ssl.protocol","TLS");
}
// setup the audrey broker
mqttClient = new MqttAndroidClient(DeviceApp.getInstance().getApplicationContext(), mqttServerURI, clientId);
mqttClient.setCallback(mqttClientCallback);
connectOptions.setSSLProperties(prop);
connectOptions.setServerURIs(serverURI);
connectOptions.setAutomaticReconnect(true);
connectOptions.setCleanSession(true);
if (authentiction)
{
connectOptions.setUserName(username);
if (password!=null && password.length()>0) {
connectOptions.setPassword(password.toCharArray());
}
}
try {
if (mqttClient.isConnected())
{
mqttClient.disconnect();
}
mqttClient.connect(connectOptions);
}
catch (MqttException exc)
{
exc.printStackTrace();
}
return mqttClient;
}
/**
* Callback used in creating the MQTT clients. Handles message arrival, delivery,
* and connection status callbacks. This is shared between the clients since they
* both send and receive the same messages.
*/
private MqttCallbackExtended mqttClientCallback = new MqttCallbackExtended() {
@Override
public void connectComplete(boolean reconnect, String serverURI) {
Log.i(TAG, "connectComplete " + serverURI);
//it seems that we must list to both patterns alertTopicClient & alertTopicClient/
subscribe("StartSession",MqttBrokerService.Broker.info);
subscribe("Master_Topic",MqttBrokerService.Broker.info);
subscribe("Raw_ECG",MqttBrokerService.Broker.info);
subscribe("Alarm",MqttBrokerService.Broker.info);
subscribe("StopSession",MqttBrokerService.Broker.info);
subscribe("#",MqttBrokerService.Broker.info);
if (reconnect) {
Log.i(TAG, "Reconnected to broker at " + serverURI);
subscribe("StartSession",MqttBrokerService.Broker.info);
subscribe("Master_Topic",MqttBrokerService.Broker.info);
subscribe("Raw_ECG",MqttBrokerService.Broker.info);
subscribe("Alarm",MqttBrokerService.Broker.info);
subscribe("StopSession",MqttBrokerService.Broker.info);
subscribe("#",MqttBrokerService.Broker.info);
} else {
Log.d(TAG, "Connected to broker at " + serverURI);
// set options for buffering of messages when disconnected
// we only really care about the current situation, so disable buffering
DisconnectedBufferOptions bufferOptions = new DisconnectedBufferOptions();
bufferOptions.setBufferEnabled(false);
bufferOptions.setPersistBuffer(false);
bufferOptions.setDeleteOldestMessages(true);
mqttClient.setBufferOpts(bufferOptions);
}
}
@Override
public void connectionLost(Throwable cause) {
if (cause == null)
Log.d(TAG, "Connection closed");
else
Log.e(TAG, "Connection unexpectedly lost: " + cause.getMessage());
// NOTE: connectOptions.setAutomaticReconnect will automatically handle
// reconnecting if the connection is lost
}
@Override
public void messageArrived(String topic, MqttMessage message) throws Exception {
Log.d(TAG, "messageArrived(), Received message from topic " + topic);
String msg = new String(message.getPayload());
Log.d(TAG, "Msg topic: " +topic);
Log.d(TAG, "Msg content: " + msg);
infoRecordFactory infoRecordFactory = null;
if (topic.contains("StartSession"))
{
StartSession startSession = null;
infoRecordFactory = new StartSessionTool();
try {
startSession = (StartSession) infoRecordFactory.create(new JSONObject(msg));
if (startSession.getMac_bin().equals(getID()))
{
//add sensor to UI
}
}
catch (JSONException exc)
{
exc.printStackTrace();
}
}
else if (topic.contains("Master_Topic"))
{
infoRecordFactory = new MasterTopicTool();
MasterTopicV2 masterTopic;
try {
//add sensor to UI
masterTopic = (MasterTopicV2 ) infoRecordFactory.create(new JSONObject(msg));
if (masterTopic.getMac_bin().equals(getID()))
{
//add sensor to UI
}
}
catch (JSONException exc)
{
exc.printStackTrace();
}
}
else if (topic.contains("Raw_ECG"))
{
infoRecordFactory = new RawECGTopicTool();
RawECG rawECG;
try {
//add sensor to UI
rawECG = (RawECG) infoRecordFactory.create(new JSONObject(msg));
if (rawECG.getMac_bin().equals(getID()))
{
//add sensor to UI
}
}
catch (JSONException exc)
{
exc.printStackTrace();
}
}
else if (topic.contains("Alarm"))
{
//add sensor to UI
infoRecordFactory = new AlarmTopicTool();
AlarmTopic alarmTopic;
alarmTopic = (AlarmTopic) infoRecordFactory.create(new JSONObject(msg));
if (alarmTopic.getMac_bin().equals(getID()))
{
//add sensor to UI
}
}
else if (topic.contains("StopSession"))
{
//remove sensor from UI
infoRecordFactory = new StopSessionTool();
StopSession stopSession;
stopSession = (StopSession) infoRecordFactory.create(new JSONObject(msg));
if ( stopSession.getMac_bin().equals(getID()))
{
//add sensor to UI
}
}
else
{
}
}
@Override
public void deliveryComplete(IMqttDeliveryToken token) {
Log.d(TAG, "Message delivery complete for message with ID " + token.getMessageId());
}
};
/**
* Subscribes to an MQTT topic to receive data from the server.
*
* @param topic MQTT topic to subscribe to
* @param source Broker to receive messages from
*/
private void subscribe(String topic, MqttBrokerService.Broker source) {
Log.i(TAG,"subscribed to:"+topic+",source:"+source);
// subscribe to topic on audrey broker
try {
// don't bother subscribing if not already connected
if ( mqttClient !=null && mqttClient.isConnected()) {
mqttClient.subscribe(topic, QOS, null, new IMqttActionListener() {
@Override
public void onSuccess(IMqttToken iMqttToken) {
Log.d(TAG, "yams: Subscribed to topic " + topic);
}
@Override
public void onFailure(IMqttToken iMqttToken, Throwable throwable) {
Log.e(TAG, "yams: Subscribe to topic " + topic);
if (throwable != null) {
Log.e(TAG, "yams: Subscribe to topic " + topic + " unsuccessful: "
+ throwable.getMessage());
}
}
});
}
} catch (MqttException e) {
Log.e(TAG, "yams: Exception when subscribing to topic " + topic + ": "
+ e.getMessage());
}
}
}
在服务中实例化MqttAdhocClient的代码段:
MqttAndroidClient client = mqttAdhocClient.setupClient(connectionPath, port, true, username, password, deviceId, true);
if (client.isConnected())
{
Log.i(TAG,"client:"+client.getServerURI()+",IS CONNECTED");
}
else
{
Log.i(TAG,"client:"+client.getServerURI()+",IS NOT CONNECTED");
}
日志显示:
2018-11-20 11:54:56.820 29016-29041 / xyz.controller.core I / DpmTcmClient:RegisterTcmMonitor from:com.android.okhttp.TcmIdleTimerMonitor
2018-11-20 11:55:02.899 29016-29216 / xyz.controller.core I / MqttAdhocClient:setupClient()
018-11-20 11:55:02.904 29016-29216 / xyz.controller.core I / MqttBrokerService: client:ssl://someurl.com:8883,未连接 >