/* * * * * * here are the parameters of the client needs to * * * * * * */
Public static String deviceName="0862664 d0169757";
Public static String productKey="a18laywNXCJ";
Public static String secret="* * * * * * * * * * * *";
//used to test the topic of
Private static String subTopic="/" + + "/" + deviceName productKey + "/get";
Private static String pubTopic="/" + + "/" + deviceName productKey + "/update";
Public static void StartConnect () throws the Exception {
//client devices own a tag, suggestion is MAC or SN, can't be empty, 32 characters or
String clientId="0862664 d0169757";//InetAddress. GetLocalHost (.) getHostAddress ();
//device certification
MapParams=new HashMap (a);
Params. Put (" productKey, "productKey);//this is corresponding to the user registered in the console device productkey
Params. Put (" deviceName ", deviceName);//this is corresponding to the user registered in the console device name
Params. Put (" clientId ", clientId);
String t=System. CurrentTimeMillis () + "";
Params. Put (" timestamp ", t);
//the MQTT server address
String targetServer="TCP://" + productKey + ". Iot-as-mqtt.cn-shanghai.aliyuncs.com: 1883 ".
//client ID format, two | | between the content of the custom tag for equipment end, scope of characters [a-z] [0-9] [a-z]
String mqttclientId=clientId + "| securemode=2, signmethod=hmacsha1, timestamp=" + t + "|";
String mqttUsername=deviceName + & amp; "" + productKey;//the MQTT user name format
String mqttPassword="A9E93DE6CFF17C42C90DE890F8BD4DCBF99BC376";//SignUtil. Sign (params, secret, "hmacsha1");//signature
System. Err. Println (" mqttclientId="+ mqttclientId +" & amp; MqttPassword="+ mqttPassword);
ConnectMqtt (targetServer mqttclientId, mqttUsername mqttPassword, deviceName);
}
Public static void connectMqtt (url String, the String clientId, String mqttUsername,
String mqttPassword, final String deviceName) throws the Exception {
MemoryPersistence persistence=new MemoryPersistence ();
//SSLSocketFactory socketFactory=createSSLSocket ();
Final MqttClient sampleClient=new MqttClient (url, clientId, persistence);
MqttConnectOptions connOpts=new MqttConnectOptions ();
ConnOpts. SetMqttVersion (4);//the MQTT 3.1.1
//connOpts setSocketFactory (socketFactory);
//Settings are automatic reconnection
//connOpts setAutomaticReconnect (true);
//if it is true, then clean up all the offline messages, namely QoS1 or 2 all receive content not
ConnOpts. SetCleanSession (false);
ConnOpts. SetUserName (mqttUsername);
ConnOpts. SetPassword (mqttPassword. ToCharArray ());
ConnOpts. SetKeepAliveInterval (65);
//LogUtil. Print (clientId + "connect, destination:" + url);
SampleClient. Connect (connOpts);
SampleClient. SetCallback (new MqttCallback () {
@ Override
Public void connectionLost (Throwable cause) {
System. The out. Println (" connection fails, the reason: "+ cause);
Cause the printStackTrace ();
}
@ Override
Public void messageArrived (String topic, MqttMessage message) throws the Exception {
System. Out.println (" message is received, come to the Topic [" + Topic + "], content is: ["
+ new String (message. GetPayload (), "utf-8") + ", ");
}
@ Override
Public void deliveryComplete (IMqttDeliveryToken token) {
//if is QoS0 news, token. There is no reply resp
System. Out.println (" message is sent successfully! "+ ((token==null | | token. The method getResponse ()==null)? "Null"
: token. The method getResponse (). GetKey ()));
}
});
CodePudding user response:
SampleClient. Connect (connOpts); Call this function when the abnormal Invalid client ID (2)CodePudding user response:
//the client ID format, two | | between the content of the custom tag for equipment end, scope of characters [a-z] [0-9] [a-z], but you | | ", "between the two, don't know if this reason, when I use MQTT, clientId not so complicated, is a unique identifier,CodePudding user response: