我正在使用wso2 IOT server从Android手机将数据发送到this sample code,并且当我通过文本框手动发送数据时,它可以正常工作,如下所示。
问题是活动开始时我无法发送数据。我想这样做是因为稍后在电话上将监听一些事件,然后进行筛选,然后使用CEP并将筛选后的事件发送到IOT服务器。但是,我坚持第一步,即不向用户发送一些数据到IOT服务器。
我试图在RegisteredActivity类的onStart()
中发送数据
@Override
protected void onStart() {
super.onStart();
String message = "custom message";
try {
deviceManagementService.publishMessage(message);
} catch (TransportHandlerException e) {
e.printStackTrace();
}
}
错误是在这种情况下,应用程序甚至无法启动
任何帮助将不胜感激😀
编辑1
RegisteredActivity class
的代码是public class RegisteredActivity extends Activity {
private DeviceManagementService deviceManagementService;
private boolean isBound = false;
private ServiceConnection deviceManagementServiceConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
DeviceManagementService.LocalBinder binder = (DeviceManagementService.LocalBinder) service;
deviceManagementService = binder.getService();
isBound = true;
}
public void onServiceDisconnected(ComponentName arg0) {
isBound = false;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_registered);
Intent serviceIntent = new Intent(this, DeviceManagementService.class);
startService(serviceIntent);
final Button btnDisconnect = findViewById(R.id.btnDisconnect);
btnDisconnect.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
unregister();
}
});
final EditText txtPayload = findViewById(R.id.editTextPayload);
final Button btnPublish = findViewById(R.id.btnPublish);
btnPublish.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (isBound) {
try {
String message = "wowww";
// deviceManagementService.publishMessage(message);
deviceManagementService.publishMessage(txtPayload.getText().toString());
} catch (TransportHandlerException e) {
e.printStackTrace();
}
}
}
});
}
@Override
public void onResume() {
super.onResume();
Intent intent = new Intent(this, DeviceManagementService.class);
bindService(intent, deviceManagementServiceConnection, Context.BIND_AUTO_CREATE);
}
@Override
public void onPause() {
super.onPause();
if (isBound) {
unbindService(deviceManagementServiceConnection);
}
}
@Override
protected void onStart() {
super.onStart();
String message = "costom message";
try {
deviceManagementService.publishMessage(message);
} catch (TransportHandlerException e) {
e.printStackTrace();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
}
private void unregister() {
if (!LocalRegistry.isExist(getApplicationContext())) {
Intent activity = new Intent(getApplicationContext(), RegisterActivity.class);
startActivity(activity);
}
LocalRegistry.removeUsername(getApplicationContext());
LocalRegistry.removeDeviceId(getApplicationContext());
LocalRegistry.removeServerURL(getApplicationContext());
LocalRegistry.removeAccessToken(getApplicationContext());
LocalRegistry.removeRefreshToken(getApplicationContext());
LocalRegistry.removeMqttEndpoint(getApplicationContext());
LocalRegistry.setExist(false);
//Stop current running background services.
Intent myService = new Intent(this, DeviceManagementService.class);
stopService(myService);
Intent registerActivity = new Intent(getApplicationContext(), RegisterActivity.class);
registerActivity.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(registerActivity);
finish();
}
}
错误是
02-02 05:13:21.611 12135-12135/org.wso2.iot.mqttsample E/MQTTTransportHandler: MQTT Client Error whilst client [null:sample] tried to publish to queue at [tcp://192.168.56.1:1886] under topic [carbon.super/sample/null/events]
02-02 05:13:21.615 12135-12135/org.wso2.iot.mqttsample W/System.err: org.wso2.iot.mqttsample.mqtt.transport.TransportHandlerException: MQTT Client Error whilst client [null:sample] tried to publish to queue at [tcp://192.168.56.1:1886] under topic [carbon.super/sample/null/events]
02-02 05:13:21.623 12135-12135/org.wso2.iot.mqttsample W/System.err: at org.wso2.iot.mqttsample.mqtt.transport.MQTTTransportHandler.publishToQueue(MQTTTransportHandler.java:285)
02-02 05:13:21.623 12135-12135/org.wso2.iot.mqttsample W/System.err: at org.wso2.iot.mqttsample.mqtt.transport.MQTTTransportHandler.publishToQueue(MQTTTransportHandler.java:242)
02-02 05:13:21.623 12135-12135/org.wso2.iot.mqttsample W/System.err: at org.wso2.iot.mqttsample.mqtt.MQTTHandler.publishDeviceData(MQTTHandler.java:161)
02-02 05:13:21.623 12135-12135/org.wso2.iot.mqttsample W/System.err: at org.wso2.iot.mqttsample.services.DeviceManagementService.publishMessage(DeviceManagementService.java:71)
02-02 05:13:21.623 12135-12135/org.wso2.iot.mqttsample W/System.err: at org.wso2.iot.mqttsample.RegisteredActivity$3.onClick(RegisteredActivity.java:87)
02-02 05:13:21.624 12135-12135/org.wso2.iot.mqttsample W/System.err: at android.view.View.performClick(View.java:6294)
02-02 05:13:21.624 12135-12135/org.wso2.iot.mqttsample W/System.err: at android.view.View$PerformClick.run(View.java:24770)
02-02 05:13:21.624 12135-12135/org.wso2.iot.mqttsample W/System.err: at android.os.Handler.handleCallback(Handler.java:790)
02-02 05:13:21.624 12135-12135/org.wso2.iot.mqttsample W/System.err: at android.os.Handler.dispatchMessage(Handler.java:99)
02-02 05:13:21.624 12135-12135/org.wso2.iot.mqttsample W/System.err: at android.os.Looper.loop(Looper.java:164)
02-02 05:13:21.624 12135-12135/org.wso2.iot.mqttsample W/System.err: at android.app.ActivityThread.main(ActivityThread.java:6494)
02-02 05:13:21.625 12135-12135/org.wso2.iot.mqttsample W/System.err: at java.lang.reflect.Method.invoke(Native Method)
02-02 05:13:21.626 12135-12135/org.wso2.iot.mqttsample W/System.err: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
02-02 05:13:21.626 12135-12135/org.wso2.iot.mqttsample W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
02-02 05:13:21.629 12135-12135/org.wso2.iot.mqttsample W/System.err: Caused by: Client is not connected (32104)
02-02 05:13:21.629 12135-12135/org.wso2.iot.mqttsample W/System.err: at org.eclipse.paho.client.mqttv3.internal.ExceptionHelper.createMqttException(ExceptionHelper.java:31)
02-02 05:13:21.629 12135-12135/org.wso2.iot.mqttsample W/System.err: at org.eclipse.paho.client.mqttv3.internal.ClientComms.sendNoWait(ClientComms.java:143)
02-02 05:13:21.629 12135-12135/org.wso2.iot.mqttsample W/System.err: at org.eclipse.paho.client.mqttv3.MqttAsyncClient.publish(MqttAsyncClient.java:858)
02-02 05:13:21.629 12135-12135/org.wso2.iot.mqttsample W/System.err: at org.eclipse.paho.client.mqttv3.MqttClient.publish(MqttClient.java:361)
02-02 05:13:21.629 12135-12135/org.wso2.iot.mqttsample W/System.err: at org.eclipse.paho.client.mqttv3.MqttClient.publish(MqttClient.java:353)
02-02 05:13:21.629 12135-12135/org.wso2.iot.mqttsample W/System.err: at org.wso2.iot.mqttsample.mqtt.transport.MQTTTransportHandler.publishToQueue(MQTTTransportHandler.java:278)
02-02 05:13:21.630 12135-12135/org.wso2.iot.mqttsample W/System.err: ... 13 more
02-02 05:13:24.679 12135-12155/org.wso2.iot.mqttsample D/EGL_emulation: eglMakeCurrent: 0xa9aad160: ver 2 0 (tinfo 0x8e6481c0)
最佳答案
为了自动发布事件,您可以使用Siddhi Sinks。您可以为输出事件流定义Siddhi Sink。 Siddhi Sink具有发布方法。您可以通过扩展Sink类来编写自己的Siddhi Sink。每当触发输出事件流时,就可以使用Siddhi Sinks发布这些事件。
除了DeviceManagementService类之外,您还可以使用SiddhiService类来执行Siddhi App。
悉达沉-https://wso2.github.io/siddhi/documentation/siddhi-4.0/#sink
您也可以在这里在Android中看到我对Siddhi的实现之一-https://github.com/Gathika94/EdgeComputingGateway_Android。
在此实现中,我使用定制的Siddhi Sink实现了您想要的东西。
关于android - 通过电话本身将MQTT数据发送到服务器,即不使用setOnClickListener()或任何其他事件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48574946/