onResume();
        registerReceiver(mGattUpdateReceiver, makeGattUpdateIntentFilter());
        if (mBluetoothLeService!= null) {
            final boolean result = mBluetoothLeService.connect(mDeviceAddress);
            Log.d(TAG, "Connect request result=" + result);
        }
//        mqttConnect();
    }

    // This makes sure that when we rotate the screen do not disconnect and reconnect on BLE
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        // should use onRetainNonConfigurationInstance() and getLastNonConfigurationInstance() to save data across
        // configuration changes and then redraw with setContentView and populate ui.
        // However, the xml appears to remain consistent on orientation changes and so I have not redrawn it
    }

    @Override
    protected void onPause() {
        super.onPause();
        unregisterReceiver(mGattUpdateReceiver);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbindService(mServiceConnection);
        mBluetoothLeService = null;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.gatt_services, menu);
        if (mBleConnected) {
            menu.findItem(R.id.menu_connect).setVisible(false);
            menu.findItem(R.id.menu_disconnect).setVisible(true);
            if (mSetupComplete) {
                menu.findItem(R.id.menu_refresh).setActionView(null);
            } else {
                menu.findItem(R.id.menu_refresh).setActionView(
                        R.layout.actionbar_indeterminate_progress);
            }
        } else {
            menu.findItem(R.id.menu_connect).setVisible(true);
            menu.findItem(R.id.menu_disconnect).setVisible(false);
            menu.findItem(R.id.menu_refresh).setActionView(null);
        }
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch(item.getItemId()) {
            case R.id.menu_connect:
                mBluetoothLeService.connect(mDeviceAddress);
                return true;
            case R.id.menu_disconnect:
                mSetupComplete = false;
                mBluetoothLeService.disconnect();
                mqttDisconnect();
                return true;
            case android.R.id.home:
                onBackPressed();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private void updateBleConnectState(final int resourceId) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mBleConnectState.setText(resourceId);
            }
        });
    }

    private void updateMqttConnectState(final int resourceId) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mMqttConnectState.setText(resourceId);
                if (resourceId == R.string.connected) {
                    mMqttButton.setText(R.string.menu_disconnect);
                } else {
                    mMqttButton.setText(R.string.menu_connect);
                }

            }
        });
    }

    private void displayData(TextView textView, String data) {
        if (data!= null) {
            textView.setText(data);
        }
    }

    private void displayPeriod(TextView textView, short period) {
        if (period > 0) {
            textView.setText(String.format(Locale.UK,"%d ms", period));
        }
    }

    // Demonstrates how to iterate through the supported GATT Services/Characteristics.
    // In this sample, we populate the data structure that is bound to the ExpandableListView
    // on the UI.
    private void displayGattServices(List<BluetoothGattService> gattServices) {
        if (gattServices == null) return;
        String uuid = null;
        String unknownServiceString = getResources().getString(R.string.unknown_service);
        String unknownCharaString = getResources().getString(R.string.unknown_characteristic);
        ArrayList<HashMap<String, String>> gattServiceData = new ArrayList<HashMap<String, String>>();
        ArrayList<ArrayList<HashMap<String, String>>> gattCharacteristicData
                = new ArrayList<ArrayList<HashMap<String, String>>>();
        mGattCharacteristics = new ArrayList<ArrayList<BluetoothGattCharacteristic>>();

        // Loops through available GATT Services.
        for (BluetoothGattService gattService : gattServices) {
            HashMap<String, String> currentServiceData = new HashMap<String, String>();
            uuid = gattService.getUuid().toString();
            currentServiceData.put(
                    LIST_NAME, GattAttributes.lookup(uuid, unknownServiceString));
            currentServiceData.put(LIST_UUID, uuid);
            gattServiceData.add(currentServiceData);

            ArrayList<HashMap<String, String>> gattCharacteristicGroupData =
                    new ArrayList<HashMap<String, String>>();
            List<BluetoothGattCharacteristic> gattCharacteristics =
                    gattService.getCharacteristics();
            ArrayList<BluetoothGattCharacteristic> charas =
                    new ArrayList<BluetoothGattCharacteristic>();

            // Loops through available Characteristics.
            for (BluetoothGattCharacteristic gattCharacteristic : gattCharacteristics) {
                charas.add(gattCharacteristic);
                HashMap<String, String> currentCharaData = new HashMap<String, String>();
                uuid = gattCharacteristic.getUuid().toString();
                currentCharaData.put(
                        LIST_NAME, GattAttributes.lookup(uuid, unknownCharaString));
                currentCharaData.put(LIST_UUID, uuid);
                gattCharacteristicGroupData.add(currentCharaData);
            }
            mGattCharacteristics.add(charas);
            gattCharacteristicData.add(gattCharacteristicGroupData);
        }


        //clear ui first
        mGattServicesList.removeAllViews();
        mGattServicesListAdapter = new SimpleAdapter (
                this,
                gattServiceData,
                android.R.layout.simple_list_item_2,
                new String[] {LIST_NAME, LIST_UUID},
                new int[] { android.R.id.text1, android.R.id.text2 }
        );
        for (int i = 0; i < mGattServicesListAdapter.getCount(); i++) {
            View item = mGattServicesListAdapter.getView(i, null, null);
            mGattServicesList.addView(item);
        }
//        mGattServicesList.setAdapter(gattServiceAdapter);
    }

    private static IntentFilter makeGattUpdateIntentFilter() {
        final IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(BluetoothLeService.ACTION_GATT_CONNECTED);
        intentFilter.addAction(BluetoothLeService.ACTION_GATT_DISCONNECTED);
        intentFilter.addAction(BluetoothLeService.ACTION_GATT_SERVICES_DISCOVERED);
        intentFilter.addAction(BluetoothLeService.ACTION_DATA_AVAILABLE);
        // Additional categories
        intentFilter.addCategory(BluetoothLeService.ACCELEROMETER_MEASUREMENT);
        intentFilter.addCategory(BluetoothLeService.ACCELEROMETER_PERIOD);
        intentFilter.addCategory(BluetoothLeService.TEMPERATURE_MEASUREMENT);
        intentFilter.addCategory(BluetoothLeService.TEMPERATURE_PERIOD);
        intentFilter.addCategory(BluetoothLeService.BUTTON_A_MEASUREMENT);
        intentFilter.addCategory(BluetoothLeService.BUTTON_B_MEASUREMENT);
        intentFilter.addCategory(BluetoothLeService.MAGNETOMETER_MEASUREMENT);
        intentFilter.addCategory(BluetoothLeService.MAGNETOMETER_PERIOD);
        intentFilter.addCategory(BluetoothLeService.MAGNETOMETER_BEARING);
        return intentFilter;
    }

    public void publishMqttMessage(String topic, String message){
        try