接收广播意图时出错。 Eclipse Paho

时间:2016-05-24 07:22:28

标签: android android-studio mqtt iot paho

我正在尝试使用eclipse paho库为我的物联网项目设计的Android应用程序。 Android应用程序只是将陀螺仪和加速度计数据发送到iot.eclipse.org上的代理。 我使用以下代码与MQTT Server建立连接。  `public void establishMQTTConnection(){

0:00:00.053972316 11839 0x7f0274003b70 WARN           audiobasesink gstaudiobasesink.c:1139:gst_audio_base_sink_wait_event:<alsasink0> error: Sink not negotiated before eos event.
ERROR: from element /GstPipeline:pipeline0/GstAlsaSink:alsasink0: The stream is in the wrong format.
Additional debug info:
gstaudiobasesink.c(1139): gst_audio_base_sink_wait_event (): /GstPipeline:pipeline0/GstAlsaSink:alsasink0:
Sink not negotiated before eos event.
ERROR: pipeline doesn't want to preroll.

问题是app永远不会建立连接。 我在logcat中收到以下错误。

    clientId = MqttClient.generateClientId();
    Log.d(TAG,clientId+"");
    client =
            new MqttAndroidClient(this.getApplicationContext(),  "iot.eclipse.org:1883",
                    clientId);

   // MqttConnectOptions options = new MqttConnectOptions();
    //options.setUserName("aiupvrxz");
    //options.setPassword("dOW0vFQzRGKA".toCharArray());
    try {

        token = client.connect();
        token.setActionCallback(new IMqttActionListener() {
            @Override
            public void onSuccess(IMqttToken asyncActionToken) {
                // We are connected
                Log.d(TAG, "onSuccess");
            }

            @Override
            public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
                // Something went wrong e.g. connection timeout or firewall problems
                Log.d(TAG, "onFailure");

            }
        });
    } catch (MqttException e) {
        e.printStackTrace();
    }
}` 

... AndroidManifest

`

05-24 12:48:20.772 10236-10236/iot.dulanga.com.androidclientv2 E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Error receiving broadcast Intent { act=android.net.conn.CONNECTIVITY_CHANGE flg=0x10000010 (has extras) } in org.eclipse.paho.android.service.MqttService$NetworkConnectionIntentReceiver@41fc9348
   at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:737)
   at android.os.Handler.handleCallback(Handler.java:605)
   at android.os.Handler.dispatchMessage(Handler.java:92)
   at android.os.Looper.loop(Looper.java:137)
   at android.app.ActivityThread.main(ActivityThread.java:4573)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:511)
android.permission.WAKE_LOCK
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:788)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
   at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.SecurityException: Neither user 10121 nor current process has android.permission.WAKE_LOCK.
   at android.os.Parcel.readException(Parcel.java:1327)
   at android.os.Parcel.readException(Parcel.java:1281)
   at android.os.IPowerManager$Stub$Proxy.acquireWakeLock(IPowerManager.java:432)
   at android.os.PowerManager$WakeLock.acquireLocked(PowerManager.java:295)
   at android.os.PowerManager$WakeLock.acquire(PowerManager.java:264)
   at org.eclipse.paho.android.service.MqttService$NetworkConnectionIntentReceiver.onReceive(MqttService.java:794)
   at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:728)
   at android.os.Handler.handleCallback(Handler.java:605) 
   at android.os.Handler.dispatchMessage(Handler.java:92) 
   at android.os.Looper.loop(Looper.java:137) 
   at android.app.ActivityThread.main(ActivityThread.java:4573) 
   at java.lang.reflect.Method.invokeNative(Native Method) 
   at java.lang.reflect.Method.invoke(Method.java:511) 
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:788) 
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555) 
   at dalvik.system.NativeStart.main(Native Method)

`

任何人都可以帮我解决我的代码问题。非常感谢!!!!

2 个答案:

答案 0 :(得分:2)

例外的中间是关键部分:

Caused by: java.lang.SecurityException: Neither user 10121 nor current process has android.permission.WAKE_LOCK.

确保已将所需部分添加到应用程序清单

<uses-permission android:name="android.permission.WAKE_LOCK" />

答案 1 :(得分:0)

在我的情况下,我忘记在清单中添加以下两行:

<uses-permission android:name="android.permission.WAKE_LOCK" />

当然,您还需要添加以下权限:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />

并在应用程序范围内添加以下行:

<service android:name="org.eclipse.paho.android.service.MqttService" />