我开发了一个执行mqtt操作并与网桥交互的应用程序(通过使用wifi连接网桥,就像我们连接某人的wifi一样)。
1->当直接与mqtt交互时,mqtt的所有操作都像连接、订阅和发布数据以及从mqtt获取数据一样工作。
2->当我与bride交互并再次尝试与mqtt交互时,它抛出异常。
MqttException (0) - java.net.UnknownHostException: Unable to resolve host "iot.rkl.com": No address associated with hostname
1608604152.272 9629-9629/com.asiczen.azlock W/System.err: at org.eclipse.paho.client.mqttv3.internal.ExceptionHelper.createMqttException(ExceptionHelper.java:38)
1608604152.272 9629-9629/com.asiczen.azlock W/System.err: at org.eclipse.paho.client.mqttv3.internal.ClientComms$ConnectBG.run(ClientComms.java:690)
1608604152.275 9629-9629/com.asiczen.azlock W/System.err: at org.eclipse.paho.client.mqttv3.internal.TCPNetworkModule.start(TCPNetworkModule.java:75)
1608604152.275 9629-9629/com.asiczen.azlock W/System.err: at org.eclipse.paho.client.mqttv3.internal.SSLNetworkModule.start(SSLNetworkModule.java:92)
1608604152.275 9629-9629/com.asiczen.azlock W/System.err: at org.eclipse.paho.client.mqttv3.internal.ClientComms$ConnectBG.run(ClientComms.java:676)
这里是执行mqtt操作的服务类
public class MqttMessageService extends Service {
private static final String TAG = MqttMessageService.class.getSimpleName();
public static final String EXTRA_DATA = "android.nfc.extra.DATA";
public static final String MQTT_CONNECTED = "android.nfc.extra.MQTT_CONNECTED";
public static final String MQTT_DISCONNECTED = "android.nfc.extra.MQTT_DISCONNECTED";
public static final String MQTT_DATA_AVAILABLE = "android.nfc.extra.MQTT_DATA_AVAILABLE";
public static final String MQTT_DELEVIERY_COMPLETED = "android.nfc.extra.MQTT_DELEVIERY_COMPLETED";
public static final String MQTT_UNABLE_TO_PUBLISH = "android.nfc.extra.MQTT_UNABLE_TO_PUBLISH";
public static final String MQTT_UNABLE_TO_SUBSCRIBE = "android.nfc.extra.MQTT_UNABLE_TO_SUBSCRIBE";
public static final String MQTT_USUBSCRIBE = "android.nfc.extra.MQTT_USUBSCRIBE";
public static final String MQTT_UN_SCBSCRIBE = "android.nfc.extra.MQTT_UN_SCBSCRIBE";
private MqttAndroidClient mqttAndroidClient;
public void connectMQTT(){
Log.d(TAG, "MQTT CONNECTION STAR: ");
PahoMqttClient pahoMqttClient = new PahoMqttClient();
mqttAndroidClient = pahoMqttClient.getMqttClient(getApplicationContext(), Packet.PublishTopic.MQTT_BROKER_URL, AppContext.getClientId(this));
mqttAndroidClient.setCallback(new MqttCallbackExtended() {
@Override
public void connectComplete(boolean b, String s) {
Log.d(TAG, "connectComplete: ");
Log.d(TAG, "MQTT CONNECTED: ");
broadcastUpdate(MqttMessageService.MQTT_CONNECTED);
}
@Override
public void connectionLost(Throwable throwable) {
Log.d(TAG, "connectionLost: ");
Log.d(TAG, " MQTT CONNECTION LOST: ");
broadcastUpdate(MqttMessageService.MQTT_DISCONNECTED);
}
@Override
public void messageArrived(String s, MqttMessage mqttMessage) {
//setMessageNotification(s, new String(mqttMessage.getPayload()));
String msg = new String(mqttMessage.getPayload(), StandardCharsets.ISO_8859_1);
Log.d(TAG, "MQTT MESSAGE ARRIVED: "+msg);
byte[] b = mqttMessage.getPayload();
broadcastUpdate(b);
}
@Override
public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {
}
});
}
@Override
public void onCreate() {
super.onCreate();
Log.d(TAG, "onCreate");
Log.d(TAG, "MQTT ONCREATE: ");
connectMQTT();
}
private final IBinder mBinder = new LocalService();
@Override
public IBinder onBind(Intent intent) {
Log.d(TAG, "MQTT ONBIND: ");
return mBinder;
}
@Override
public boolean onUnbind(Intent intent) {
// After using a given device, you should make sure that BluetoothGatt.close() is called
// such that resources are cleaned up properly. In this particular example, close() is
// invoked when the UI is disconnected from the Service.
//close();
Log.d(TAG, "MQTT ONUNBIND: ");
return super.onUnbind(intent);
}
public class LocalService extends Binder {
public MqttMessageService getService(){return MqttMessageService.this;}
}
public void subcribe(){
try {
//pahoMqttClient.subscribe(mqttAndroidClient, "BridgeId_SCAN_RESPONSE", 0);
IMqttToken token = mqttAndroidClient.subscribe(Utils.getSubscribeTopic(), 0);
token.setActionCallback(new IMqttActionListener() {
@Override
public void onSuccess(IMqttToken iMqttToken) {
Log.d(TAG, "MQTT SUBSCRIBED 0: ");
Log.d(TAG, "Subscribe Successfully " + "BridgeId_SCAN_RESPONSE");
broadcastUpdate(MqttMessageService.MQTT_USUBSCRIBE);
}
@Override
public void onFailure(IMqttToken iMqttToken, Throwable throwable) {
Log.e(TAG, "Subscribe Failed " + "BridgeId_SCAN_RESPONSE");
throwable.printStackTrace();
broadcastUpdate(MqttMessageService.MQTT_UNABLE_TO_SUBSCRIBE);
}
});
} catch (MqttException e) {
broadcastUpdate(MqttMessageService.MQTT_UNABLE_TO_SUBSCRIBE);
e.printStackTrace();
}catch (Exception e){
broadcastUpdate(MqttMessageService.MQTT_UNABLE_TO_SUBSCRIBE);
e.printStackTrace();
}
}
public void msubscribe(String topic){
try {
mqttAndroidClient.subscribe(topic, 0);
Log.d(TAG, "MQTT SUBSCRIBED 1: ");
} catch (MqttException e) {
e.printStackTrace();
}
}
public void publish(byte[] payload){
Log.d(TAG, "publish: ");
MqttMessage message = new MqttMessage(payload);
message.setId(320);
message.setRetained(false);
message.setQos(0);
try {
mqttAndroidClient.publish(Utils.getPublishTopic(), message);
Log.d(TAG, "MQTT DATA PUBLISHED: ");
} catch (MqttException e) {
e.printStackTrace();
broadcastUpdate(MqttMessageService.MQTT_UNABLE_TO_PUBLISH);
}catch (Exception e){
e.printStackTrace();
}
}
public void unSubscribe(){
IMqttToken token;
try {
token = mqttAndroidClient.unsubscribe(Utils.getSubscribeTopic());
token.setActionCallback(new IMqttActionListener() {
@Override
public void onSuccess(IMqttToken iMqttToken) {
Log.d(TAG, "UnSubscribe Successfully ");
Log.d(TAG, "MQTT UNSUBSCRIBED: ");
broadcastUpdate(MqttMessageService.MQTT_UN_SCBSCRIBE);
}
@Override
public void onFailure(IMqttToken iMqttToken, Throwable throwable) {
Log.e(TAG, "UnSubscribe Failed ");
broadcastUpdate(MqttMessageService.MQTT_UN_SCBSCRIBE);
}
});
} catch (MqttException e) {
broadcastUpdate(MqttMessageService.MQTT_UN_SCBSCRIBE);
e.printStackTrace();
}
}
public void disconnect() {
try {
IMqttToken mqttToken = mqttAndroidClient.disconnect();
mqttToken.setActionCallback(new IMqttActionListener() {
@Override
public void onSuccess(IMqttToken iMqttToken) {
Log.d(TAG, "MQTT DISCONNECTED: ");
Log.d(TAG, "Successfully disconnect");
}
@Override
public void onFailure(IMqttToken iMqttToken, Throwable throwable) {
Log.d(TAG, "Failed to disconnect " + throwable.toString());
}
});
} catch (Exception e){
e.printStackTrace();
}
}
private void broadcastUpdate(final String action) {
final Intent intent = new Intent(action);
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}
private void broadcastUpdate(byte[] data) {
final Intent intent = new Intent(MqttMessageService.MQTT_DATA_AVAILABLE);
intent.putExtra(EXTRA_DATA, data);
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}
}
paho连接电缆下方
public class PahoMqttClient {
public MqttAndroidClient getMqttClient(Context context, String brokerUrl, String clientId) {
mqttAndroidClient = new MqttAndroidClient(context, brokerUrl, clientId);
try {
MqttConnectOptions MQTT_CONNECTION_OPTIONS = getMqttConnectionOption();
if (brokerUrl.contains("ssl")) {
InputStream i= context.getResources().openRawResource(R.raw.client_ca);
InputStream i1= context.getResources().openRawResource(R.raw.client_crt);
InputStream inputStream = context.getResources().openRawResource(R.raw.client_key);
MQTT_CONNECTION_OPTIONS.setSocketFactory(createSSLSocketFactory(i,i1,inputStream));
}
IMqttToken token = mqttAndroidClient.connect(MQTT_CONNECTION_OPTIONS);
token.setActionCallback(new IMqttActionListener() {
@Override
public void onSuccess(IMqttToken asyncActionToken) {
mqttAndroidClient.setBufferOpts(getDisconnectedBufferOptions());
Log.d(TAG, "MQTT CONNECTED:0 ");
Log.d(TAG, "Success");
}
@Override
public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
Log.d(TAG, "MQTT CONNETION FAILED:0 ");
exception.printStackTrace();
Log.d(TAG, "Failure " + exception.toString());
}
});
} catch (MqttException e) {
e.printStackTrace();
}
return mqttAndroidClient;
}
}
提前谢谢
暂无答案!
目前还没有任何答案,快来回答吧!