Home > Mobile >  Android in org. Eclipse. Paho. Client. Mqttv3 - ali cloud 1.0.2 links MQTT server error Ivalid clien
Android in org. Eclipse. Paho. Client. Mqttv3 - ali cloud 1.0.2 links MQTT server error Ivalid clien

Time:10-09

/* * * * * * 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
Map Params=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:

refer to the second floor weixin_40290083 response:
//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,

I changed to pure letters also not line, get rid of a comma in the middle of the also not line, is the exception, invalid client id, the clientID is them up, or is there any relation to and back,

CodePudding user response:

refer to the second floor weixin_40290083 response:
//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,

I in c # under the Windows so that other links can be connected to the backstage, I don't know why the android, under the same parameters to the error

CodePudding user response:

When used on my android is to use the current time of millisecond value plus phone ID as clientId, because the MQTT service was built their server, just myself in test environment, also does not have what problem, see the information on this ID value cannot be the same, have what effect to concrete,

CodePudding user response:

Great god help genuflect is begged

CodePudding user response:

The sink?
  • Related