taihuzhipan 2019年07月31日
安卓上用org.eclipse.paho.client.mqttv3-1.0.2链接阿里云mqtt服务端报错Ivalid Client ID
/******这里是客户端需要的参数*******/
public static String deviceName = "0862664D0169757";
public static String productKey = "a18laywNXCJ";
public static String secret = "************";

//用于测试的topic
private static String subTopic = "/" + productKey + "/" + deviceName + "/get";
private static String pubTopic = "/" + productKey + "/" + deviceName + "/update";

public static void StartConnect() throws Exception {
//客户端设备自己的一个标记,建议是MAC或SN,不能为空,32字符内
String clientId = "0862664D0169757";//InetAddress.getLocalHost().getHostAddress();

//设备认证
Map<String, String> params = new HashMap<String, String>();
params.put("productKey", productKey); //这个是对应用户在控制台注册的 设备productkey
params.put("deviceName", deviceName); //这个是对应用户在控制台注册的 设备name
params.put("clientId", clientId);
String t = System.currentTimeMillis() + "";
params.put("timestamp", t);

//MQTT服务器地址
String targetServer = "tcp://" + productKey + ".iot-as-mqtt.cn-shanghai.aliyuncs.com:1883";

//客户端ID格式,两个||之间的内容为设备端自定义的标记,字符范围[0-9][a-z][A-Z]
String mqttclientId = clientId + "|securemode=2,signmethod=hmacsha1,timestamp=" + t + "|";
String mqttUsername = deviceName + "&" + productKey; //mqtt用户名格式
String mqttPassword = "A9E93DE6CFF17C42C90DE890F8BD4DCBF99BC376";//SignUtil.sign(params, secret, "hmacsha1"); //签名

System.err.println("mqttclientId=" + mqttclientId+"&mqttPassword="+mqttPassword);

connectMqtt(targetServer, mqttclientId, mqttUsername, mqttPassword, deviceName);
}

public static void connectMqtt(String url, String clientId, String mqttUsername,
String mqttPassword, final String deviceName) throws Exception {
MemoryPersistence persistence = new MemoryPersistence();
//SSLSocketFactory socketFactory = createSSLSocket();
final MqttClient sampleClient = new MqttClient(url, clientId, persistence);
MqttConnectOptions connOpts = new MqttConnectOptions();
connOpts.setMqttVersion(4); // MQTT 3.1.1
//connOpts.setSocketFactory(socketFactory);

//设置是否自动重连
//connOpts.setAutomaticReconnect(true);

//如果是true,那么清理所有离线消息,即QoS1或者2的所有未接收内容
connOpts.setCleanSession(false);

connOpts.setUserName(mqttUsername);
connOpts.setPassword(mqttPassword.toCharArray());
connOpts.setKeepAliveInterval(65);

//LogUtil.print(clientId + "进行连接, 目的地: " + url);
sampleClient.connect(connOpts);

sampleClient.setCallback(new MqttCallback() {
@Override
public void connectionLost(Throwable cause) {
System.out.println("连接失败,原因:" + cause);
cause.printStackTrace();
}

@Override
public void messageArrived(String topic, MqttMessage message) throws Exception {
System.out.println("接收到消息,来至Topic [" + topic + "] , 内容是:["
+ new String(message.getPayload(), "UTF-8") + "], ");
}

@Override
public void deliveryComplete(IMqttDeliveryToken token) {
//如果是QoS0的消息,token.resp是没有回复的
System.out.println("消息发送成功! " + ((token == null || token.getResponse() == null) ? "null"
: token.getResponse().getKey()));
}
});
...全文
118 点赞 收藏 7
写回复
7 条回复

还没有回复,快来抢沙发~

发动态
发帖子
Android
创建于2009-10-09

4.6w+

社区成员

9.0w+

社区内容

移动平台 Android
社区公告
暂无公告