gpt4 book ai didi

android - 通过电话本身将 MQTT 数据发送到服务器,即不使用 setOnClickListener() 或任何其他事件

转载 作者:行者123 更新时间:2023-11-29 19:02:15 25 4
gpt4 key购买 nike

我正在将数据发送到 wso2 IOT server从 android 手机使用 this sample code当我从文本框手动发送数据时,它工作正常,如下所示。

enter image description here

问题是我无法在 Activity 开始时发送数据。我想这样做是因为稍后手机会监听一些事件,然后使用 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 类的代码是

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 应用程序。

Siddhi 水槽 - 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com