package com.uxxu.konashi.sample.piosample;

import android.bluetooth.BluetoothGattCharacteristic;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.Switch;

import com.uxxu.konashi.lib.Konashi;
import com.uxxu.konashi.lib.KonashiListener;
import com.uxxu.konashi.lib.KonashiManager;
import com.uxxu.konashi.lib.KonashiUtils;

import org.jdeferred.DoneCallback;
import org.jdeferred.FailCallback;

import info.izumin.android.bletia.BletiaException;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Switch mBlinkSwitch, mModeSwitch;

    private KonashiManager mKonashiManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mBlinkSwitch = (Switch)findViewById(R.id.switch_blink);
        mBlinkSwitch.setOnCheckedChangeListener(mOnBlinkCheckedChangeListener);
        mModeSwitch = (Switch)findViewById(R.id.switch_mode);
        mModeSwitch.setOnCheckedChangeListener(mOnModeCheckedChangeListener);
        findViewById(R.id.btn_connect).setOnClickListener(this);
        findViewById(R.id.btn_disconnect).setOnClickListener(this);

        mKonashiManager = new KonashiManager();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mKonashiManager.addListener(mKonashiListener);
        mKonashiManager.initialize(this);
        refreshViews();
    }

    @Override
    protected void onPause() {
        mKonashiManager.removeListener(mKonashiListener);
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        if (mKonashiManager != null) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    mKonashiManager.reset();
                    mKonashiManager.disconnect();
                    mKonashiManager = null;
                }
            }).start();
        }
        super.onDestroy();
    }

    private void refreshViews() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                boolean isReady = mKonashiManager.isReady();
                findViewById(R.id.btn_connect).setEnabled(!isReady);
                findViewById(R.id.btn_disconnect).setEnabled(isReady);
                mModeSwitch.setEnabled(isReady);
                mBlinkSwitch.setEnabled(isReady);
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_connect:
                mKonashiManager.find(this);
                break;
            case R.id.btn_disconnect:
                mKonashiManager.disconnect();
                break;
        }
    }

    private final CompoundButton.OnCheckedChangeListener mOnBlinkCheckedChangeListener = new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
            int value = b ? Konashi.HIGH : Konashi.LOW;
            mKonashiManager.digitalWrite(Konashi.PIO1, value);
        }
    };

    private final CompoundButton.OnCheckedChangeListener mOnModeCheckedChangeListener = new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
            int value = b ? Konashi.OUTPUT : Konashi.INPUT;
            mKonashiManager.pinMode(Konashi.PIO1, value);
        }
    };

    private final KonashiListener mKonashiListener = new KonashiListener() {
        @Override
        public void onConnect(KonashiManager manager) {
            refreshViews();
        }

        @Override
        public void onDisconnect(KonashiManager manager) {
            refreshViews();
        }

        @Override
        public void onError(KonashiManager manager, BletiaException e) {

        }

        @Override
        public void onUpdatePioOutput(KonashiManager manager, int value) {

        }

        @Override
        public void onUpdateUartRx(KonashiManager manager, byte[] value) {

        }

        @Override
        public void onUpdateBatteryLevel(KonashiManager manager, int level) {

        }
    };
}
@@ -1840,51 +1840,8 @@
 ) %7B%0A
-        if (mKonashiManager != null) %7B%0A    
     
@@ -1876,36 +1876,32 @@
 ) %7B%0A            
- 
-   
 @Override%0A      
@@ -1886,36 +1886,32 @@
       @Override%0A
-    
             publ
@@ -1934,36 +1934,32 @@
                 
- 
-   
 mKonashiManager.
@@ -1965,17 +1965,16 @@
 .reset()
-;
 %0A       
@@ -1986,42 +1986,175 @@
     
-    mKonashiManager.disconnect();%0A
+.then(new DoneCallback%3CBluetoothGattCharacteristic%3E() %7B%0A                    @Override%0A                    public void onDone(BluetoothGattCharacteristic result) %7B%0A    
     
@@ -2188,17 +2188,27 @@
 ager
- = null;%0A
+.disconnect();%0A    
     
@@ -2237,17 +2237,13 @@
     
+  
-%7D).start(
+  %7D
 );%0A 
@@ -2241,33 +2241,57 @@
     %7D);%0A        
-%7D
+    %7D%0A        %7D).start();
 %0A        super.o

Fix onDestroy()