package com.uxxu.konashi.sample.pwmsample;

import android.app.Activity;
import android.bluetooth.BluetoothGattCharacteristic;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.SeekBar;
import android.widget.ToggleButton;

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 KonashiManager mKonashiManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ((SeekBar) findViewById(R.id.seek_pwm)).setOnSeekBarChangeListener(mOnSeekBarChangeListener);
        findViewById(R.id.btn_find).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() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                mKonashiManager.reset()
                        .then(new DoneCallback<BluetoothGattCharacteristic>() {
                            @Override
                            public void onDone(BluetoothGattCharacteristic result) {
                                mKonashiManager.disconnect();
                            }
                        });
            }
        }).start();
        super.onDestroy();
    }

    private void refreshViews() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                boolean isReady = mKonashiManager.isReady();
                findViewById(R.id.btn_find).setVisibility(!isReady ? View.VISIBLE : View.GONE);
                findViewById(R.id.seek_pwm).setVisibility(isReady ? View.VISIBLE : View.GONE);
            }
        });
    }

    SeekBar.OnSeekBarChangeListener mOnSeekBarChangeListener = new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
            mKonashiManager.pwmLedDrive(Konashi.PIO1, (float) i);
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {

        }
    };

    @Override
    public void onClick(View view) {
        mKonashiManager.find(this);
    }

    private final KonashiListener mKonashiListener = new KonashiListener() {
        @Override
        public void onConnect(KonashiManager manager) {
            refreshViews();
            mKonashiManager.pwmMode(Konashi.PIO1, Konashi.PWM_ENABLE_LED_MODE)
                    .fail(new FailCallback<BletiaException>() {
                        @Override
                        public void onFail(BletiaException result) {
                            KonashiUtils.log(result.getMessage());
                        }
                    });
        }

        @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) {

        }
    };
}
@@ -252,16 +252,45 @@
 eekBar;%0A
+import android.widget.Toast;%0A
 import a
@@ -2066,109 +2066,8 @@
 ) %7B%0A
-        runOnUiThread(new Runnable() %7B%0A            @Override%0A            public void run() %7B%0A        
     
@@ -2119,32 +2119,24 @@
 ();%0A        
-       
- 
 findViewById
@@ -2199,32 +2199,24 @@
 View.GONE);%0A
-        
         find
@@ -2294,34 +2294,8 @@
 E);%0A
-            %7D%0A        %7D);%0A
     

Remove runOnUiThread