title: Setup an MQTT Connection in Android
body: <p>I am totally new to using MQTT with Android, I am trying this code :</p>
  
  <pre><code>public class MQTTActivity extends AppCompatActivity {
  private HistoryAdapter mAdapter;
  MqttAndroidClient mqttAndroidClient;
  
  final String serverUri = "tcp://iot.eclipse.org:1883";
  
  String clientId = "ExampleAndroidClient";
  final String subscriptionTopic = "exampleAndroidTopic";
  final String publishTopic = "exampleAndroidPublishTopic";
  final String publishMessage = "Hello World!";
  
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_scrolling);
  
  
      FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
      fab.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View view) {
              publishMessage();
          }
      });
  
  
      RecyclerView mRecyclerView = (RecyclerView) findViewById(R.id.history_recycler_view);
      RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(this);
      mRecyclerView.setLayoutManager(mLayoutManager);
  
      mAdapter = new HistoryAdapter(new ArrayList&lt;String&gt;());
      mRecyclerView.setAdapter(mAdapter);
  
      clientId = clientId + System.currentTimeMillis();
  
      mqttAndroidClient = new MqttAndroidClient(getApplicationContext(), serverUri, clientId);
      mqttAndroidClient.setCallback(new MqttCallbackExtended() {
          @Override
          public void connectComplete(boolean reconnect, String serverURI) {
  
              if (reconnect) {
                  addToHistory("Reconnected to : " + serverURI);
                  // Because Clean Session is true, we need to re-subscribe
                  subscribeToTopic();
              } else {
                  addToHistory("Connected to: " + serverURI);
              }
          }
  
          @Override
          public void connectionLost(Throwable cause) {
              addToHistory("The Connection was lost.");
          }
  
          @Override
          public void messageArrived(String topic, MqttMessage message) throws Exception {
              addToHistory("Incoming message: " + new String(message.getPayload()));
          }
  
          @Override
          public void deliveryComplete(IMqttDeliveryToken token) {
  
          }
      });
  
      MqttConnectOptions mqttConnectOptions = new MqttConnectOptions();
      mqttConnectOptions.setAutomaticReconnect(true);
      mqttConnectOptions.setCleanSession(false);
  
  
  
  
  
  
  
      try {
          //addToHistory("Connecting to " + serverUri);
          mqttAndroidClient.connect(mqttConnectOptions, null, new IMqttActionListener() {
              @Override
              public void onSuccess(IMqttToken asyncActionToken) {
                  DisconnectedBufferOptions disconnectedBufferOptions = new DisconnectedBufferOptions();
                  disconnectedBufferOptions.setBufferEnabled(true);
                  disconnectedBufferOptions.setBufferSize(100);
                  disconnectedBufferOptions.setPersistBuffer(false);
                  disconnectedBufferOptions.setDeleteOldestMessages(false);
                  mqttAndroidClient.setBufferOpts(disconnectedBufferOptions);
                  subscribeToTopic();
              }
  
              @Override
              public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
                  addToHistory("Failed to connect to: " + serverUri);
              }
          });
  
  
      } catch (MqttException ex){
          ex.printStackTrace();
      }
  
  }
  
  private void addToHistory(String mainText){
      System.out.println("LOG: " + mainText);
      mAdapter.add(mainText);
      Snackbar.make(findViewById(android.R.id.content), mainText, Snackbar.LENGTH_LONG)
              .setAction("Action", null).show();
  
  }
  
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate the menu; this adds items to the action bar if it is present.
  
      return true;
  }
  
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
      // Handle action bar item clicks here. The action bar will
      // automatically handle clicks on the Home/Up button, so long
      // as you specify a parent activity in AndroidManifest.xml.
      int id = item.getItemId();
  
      //noinspection SimplifiableIfStatement
  
      return super.onOptionsItemSelected(item);
  }
  
  public void subscribeToTopic(){
      try {
          mqttAndroidClient.subscribe(subscriptionTopic, 0, null, new IMqttActionListener() {
              @Override
              public void onSuccess(IMqttToken asyncActionToken) {
                  addToHistory("Subscribed!");
              }
  
              @Override
              public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
                  addToHistory("Failed to subscribe");
              }
          });
  
          // THIS DOES NOT WORK!
          mqttAndroidClient.subscribe(subscriptionTopic, 0, new IMqttMessageListener() {
              @Override
              public void messageArrived(String topic, MqttMessage message) throws Exception {
                  // message Arrived!
                  System.out.println("Message: " + topic + " : " + new String(message.getPayload()));
              }
          });
  
      } catch (MqttException ex){
          System.err.println("Exception whilst subscribing");
          ex.printStackTrace();
      }
  }
  
  public void publishMessage(){
  
      try {
          MqttMessage message = new MqttMessage();
          message.setPayload(publishMessage.getBytes());
          mqttAndroidClient.publish(publishTopic, message);
          addToHistory("Message Published");
          if(!mqttAndroidClient.isConnected()){
              addToHistory(mqttAndroidClient.getBufferedMessageCount() + " messages in buffer.");
          }
      } catch (MqttException e) {
          System.err.println("Error Publishing: " + e.getMessage());
          e.printStackTrace();
      }
  }}
  </code></pre>
  
  <p>I am getting this exception :</p>
  
  <pre><code>FATAL EXCEPTION: main
  java.lang.RuntimeException: Error receiving broadcast Intent { act=android.net.conn.CONNECTIVITY_CHANGE flg=0x8000010 (has extras) } in org.eclipse.paho.android.service.MqttService$NetworkConnectionIntentReceiver@41c79f18
  at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:810)
  at android.os.Handler.handleCallback(Handler.java:800)
  at android.os.Handler.dispatchMessage(Handler.java:100)
  at android.os.Looper.loop(Looper.java:194)
  at android.app.ActivityThread.main(ActivityThread.java:5455)
  at java.lang.reflect.Method.invokeNative(Native Method)
  at java.lang.reflect.Method.invoke(Method.java:525)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:966)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:733)
  at dalvik.system.NativeStart.main(Native Method)
  Caused by: java.lang.SecurityException: Neither user 10083 nor current process has android.permission.WAKE_LOCK.
  at android.os.Parcel.readException(Parcel.java:1428)
  at android.os.Parcel.readException(Parcel.java:1382)
  at android.os.IPowerManager$Stub$Proxy.acquireWakeLock(IPowerManager.java:301)
  at android.os.PowerManager$WakeLock.acquireLocked(PowerManager.java:746)
  at android.os.PowerManager$WakeLock.acquire(PowerManager.java:715)
  at org.eclipse.paho.android.service.MqttService$NetworkConnectionIntentReceiver.onReceive(MqttService.java:821)
  at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:800)
  at android.os.Handler.handleCallback(Handler.java:800) 
  at android.os.Handler.dispatchMessage(Handler.java:100) 
  at android.os.Looper.loop(Looper.java:194) 
  at android.app.ActivityThread.main(ActivityThread.java:5455) 
  at java.lang.reflect.Method.invokeNative(Native Method) 
  at java.lang.reflect.Method.invoke(Method.java:525) 
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:966) 
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:733) 
  at dalvik.system.NativeStart.main(Native Method) 
  </code></pre>
  
  <p>I already added the Network and the Internet Connection permessions.
  I added the mqtt service to my manifest.</p>
  
  <p>I can't seem to get where the code is going wrong ?</p>