| | |
| | | return clientId; |
| | | } |
| | | |
| | | final String tag = "MqttRecvClient1"; |
| | | final String tag = "uniToAndroid->>>MqttRecvClient--->"; |
| | | /** |
| | | * 当前 mqtt client |
| | | */ |
| | |
| | | * @param payload 内容 |
| | | */ |
| | | public void publish(String topic, String payload) throws MqttException { |
| | | |
| | | //回复时,mqtt主题中的方向要变化,要做方向替换 |
| | | topic = topic.replace("PCToAndroid", "AndroidToPC"); |
| | | String finalTopic = topic; |
| | | mClient.publish(topic, payload.getBytes(), 1, false, null, new IMqttActionListener() { |
| | | @Override |
| | | public void onSuccess(IMqttToken asyncActionToken) { |
| | | |
| | | LogUtils.e(tag, "回复主题--->" + finalTopic + "\r\n" + payload); |
| | | } |
| | | |
| | | @Override |
| | | public void onFailure(IMqttToken asyncActionToken, Throwable exception) { |
| | | |
| | | LogUtils.e(tag, "回复失败--->" + finalTopic + "\r\n" + exception.getMessage()); |
| | | } |
| | | }); |
| | | } |
| | | |
| | | /** |
| | | * 取消订阅 |
| | | * |
| | | */ |
| | | public void unsubscribe(String topic) throws MqttException { |
| | | mClient.unsubscribe(topic, null, new IMqttActionListener() { |
| | |
| | | |
| | | /** |
| | | * 断开连接 |
| | | * |
| | | */ |
| | | public void disconnect() throws MqttException { |
| | | mClient.disconnect(null, new IMqttActionListener() { |
| | |
| | | * uni通过qtt传过来的数据处理 |
| | | * |
| | | * @param topic 主题 |
| | | * @param payload 内容 |
| | | * @param payload 负载内容 |
| | | */ |
| | | @RequiresApi(api = Build.VERSION_CODES.O) |
| | | void sendMqttToUni(final String topic, String payload) { |
| | |
| | | return; |
| | | } |
| | | String[] split = topic.split("/"); |
| | | String event = null; |
| | | for (String s : split) { |
| | | if (s.startsWith("uni_")) { |
| | | event = s; |
| | | break; |
| | | if (split.length < 3) { |
| | | return; |
| | | } |
| | | } |
| | | |
| | | //表示大类 |
| | | String request_module_topic = split[2]; |
| | | MqttDCUniMPJSCallback mqttDCUniMPJSCallback = new MqttDCUniMPJSCallback(); |
| | | mqttDCUniMPJSCallback.setMqttCallBack(new MqttCallBack() { |
| | | @Override |
| | | public void onResult(Object data) { |
| | | if (null != data) |
| | | try { |
| | | //返回数据给uni |
| | | publish(topic, data.toString()); |
| | | } catch (Exception ignored) { |
| | | } |
| | | } |
| | | }); |
| | | // JSON.parse(payload) |
| | | HdlUniLogic.getInstance().onOtherUniMPEventReceive(HDLUniMP.UNI_APP_ID, topic, payload, mqttDCUniMPJSCallback); |
| | | HdlUniLogic.getInstance().onOtherUniMPEventReceive(HDLUniMP.UNI_APP_ID, request_module_topic, payload, mqttDCUniMPJSCallback); |
| | | } |
| | | |
| | | private boolean mIsUni = false; |