我正在試用Android預覽模式下的東西。
面對通過代碼與WiFi以及本地網絡通信面臨的很多挑戰。
其中一種情況是在RPi3上將Android Thing作爲MQTT代理。
我不確定問題是否與MQTT代碼或Android事物有關。
MqttException(0) - AndroidThings上的java.net.NoRouteToHostException
任何人都可以幫助我找出問題嗎?
這裏是我的代碼:
package com.example.androidthings.myproject;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import com.google.android.things.pio.Gpio;
import com.google.android.things.pio.PeripheralManagerService;
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
import java.io.IOException;
public class MainActivity extends Activity implements MqttCallback{
private static final String TAG = MainActivity.class.getSimpleName();
public static final String MOTOR_A_PIN_1 = "BCM21"; //physical pin #40
public static final String MOTOR_A_PIN_2 = "BCM20"; //physical pin #38
public static final String MOTOR_B_PIN_1 = "BCM24"; //physical pin #18
public static final String MOTOR_B_PIN_2 = "BCM23"; //physical pin #16
private Gpio motorAPin1;
private Gpio motorAPin2;
private Gpio motorBPin1;
private Gpio motorBPin2;
private MqttClient client;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "onCreate.....Motor MQTT");
try {
MemoryPersistence persistance = new MemoryPersistence();
client = new MqttClient("tcp://127.0.0.1:1883", "AndroidThingBroker", persistance);
client.connect();
String topic = "topic/androidthings";
int qos = 1;
client.subscribe(topic, qos);
} catch (MqttException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
PeripheralManagerService service = new PeripheralManagerService();
try {
// Create GPIO connection for L293D (Motor will run through L293D).
motorAPin1 = service.openGpio(MOTOR_A_PIN_1);
// Configure as an output.
motorAPin1.setDirection(Gpio.DIRECTION_OUT_INITIALLY_LOW);
// Create GPIO connection for L293D (Motor will run through L293D).
motorAPin2 = service.openGpio(MOTOR_A_PIN_2);
// Configure as an output.
motorAPin2.setDirection(Gpio.DIRECTION_OUT_INITIALLY_LOW);
// Create GPIO connection for L293D (Motor will run through L293D).
motorBPin1 = service.openGpio(MOTOR_B_PIN_1);
// Configure as an output.
motorBPin1.setDirection(Gpio.DIRECTION_OUT_INITIALLY_LOW);
// Create GPIO connection for L293D (Motor will run through L293D).
motorBPin2 = service.openGpio(MOTOR_B_PIN_2);
// Configure as an output.
motorBPin2.setDirection(Gpio.DIRECTION_OUT_INITIALLY_LOW);
} catch (IOException e) {
Log.e(TAG, "Error on PeripheralIO API", e);
}
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.d(TAG, "onDestroy");
if (motorAPin1 != null) {
try {
motorAPin1.close();
} catch (IOException e) {
Log.e(TAG, "Error on PeripheralIO API", e);
}
}
if (motorAPin2 != null) {
try {
motorAPin2.close();
} catch (IOException e) {
Log.e(TAG, "Error on PeripheralIO API", e);
}
}
if (motorBPin1 != null) {
try {
motorBPin1.close();
} catch (IOException e) {
Log.e(TAG, "Error on PeripheralIO API", e);
}
}
if (motorBPin2 != null) {
try {
motorBPin2.close();
} catch (IOException e) {
Log.e(TAG, "Error on PeripheralIO API", e);
}
}
}
@Override
public void connectionLost(Throwable cause) {
Log.d(TAG, "connectionLost....");
}
@Override
public void messageArrived(String topic, MqttMessage message) throws Exception {
String payload = new String(message.getPayload());
Log.d(TAG, payload);
// do stuff
}
@Override
public void deliveryComplete(IMqttDeliveryToken token) {
Log.d(TAG, "deliveryComplete....");
}
}
錯誤:
12-27 15:03:58.898 2245-2245/com.example.androidthings.myproject W/System.err:
Unable to connect to server (32103) - java.net.ConnectException: Connection refused
12-27 15:03:58.898 2245-2245/com.example.androidthings.myproject W/System.err: at org.eclipse.paho.client.mqttv3.internal.TCPNetworkModule.start(TCPNetworkModule.java:79)
12-27 15:03:58.899 2245-2245/com.example.androidthings.myproject W/System.err: at org.eclipse.paho.client.mqttv3.internal.ClientComms$ConnectBG.run(ClientComms.java:590)
12-27 15:03:58.899 2245-2245/com.example.androidthings.myproject W/System.err: at java.lang.Thread.run(Thread.java:761)
12-27 15:03:58.899 2245-2245/com.example.androidthings.myproject W/System.err: Caused by: java.net.ConnectException: Connection refused
12-27 15:03:58.899 2245-2245/com.example.androidthings.myproject W/System.err: at java.net.PlainSocketImpl.socketConnect(Native Method)
12-27 15:03:58.899 2245-2245/com.example.androidthings.myproject W/System.err: at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:334)
12-27 15:03:58.899 2245-2245/com.example.androidthings.myproject W/System.err: at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:196)
12-27 15:03:58.899 2245-2245/com.example.androidthings.myproject W/System.err: at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:178)
12-27 15:03:58.899 2245-2245/com.example.androidthings.myproject W/System.err: at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:356)
12-27 15:03:58.899 2245-2245/com.example.androidthings.myproject W/System.err: at java.net.Socket.connect(Socket.java:586)
12-27 15:03:58.900 2245-2245/com.example.androidthings.myproject W/System.err: at org.eclipse.paho.client.mqttv3.internal.TCPNetworkModule.start(TCPNetworkModule.java:70)
12-27 15:03:58.900 2245-2245/com.example.androidthings.myproject W/System.err: ... 2 more
**編輯**
更改IP地址爲127.0.0.1,現在接收不同的錯誤堆棧。我已經更新了上面的錯誤日誌。
我的不好... MQTT Broker已經關閉。我認爲我可以在設備上創建代理。然後我意識到它應該在服務器上運行,而我在初始測試期間啓動的服務器已關閉。
當我開始使用Mosquitto中間服務器,我現在可以從Android連接到它:d
通過'adb shell',嘗試'ifconfig'或'ping'或其他東西來看看你的Thing如何連接到網絡。 – CommonsWare
正在工作..我用adb外殼ifconfig來獲取發佈者發送消息的IP地址。 Java上的相同問題表示防火牆問題。但我正在家庭網絡上測試它。不知道什麼是根本原因。 –