java - 如何异步验证 Nymi band?

标签 java android asynchronous

我正在尝试异步验证 Nymi band 。但是当我尝试这样做时,出现以下异常:

java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()

如您所见,我通过以下方法获得了所有的 toast :

runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(TestBluetooth.this, "Failed to initialize NCL library!", Toast.LENGTH_LONG).show();
                    }
                });

但我仍然得到了异常(exception)。该方法在onCreate()方法中正常运行时工作正常,但异步运行时失败。

编辑:即使删除了所有的 toast,我仍然遇到异常。

这是我的 Thread 类,我在其中异步调用 validate() :

public class NymiAsync extends AsyncTask<Integer,Integer,Integer> {

@Override
protected Integer doInBackground(Integer... integers) {

    try{
        TestBluetooth tb=new TestBluetooth();
        tb.startValidatingNymi();
    }catch (Exception e){
        e.printStackTrace();
    }
    return 0;
}

}

这是我具有验证方法的主类:

public class TestBluetooth extends  Activity implements OnClickListener,ProvisionController.ProvisionProcessListener,
    ValidationController.ValidationProcessListener {

boolean isBluetoothEnabled = false;
static boolean nclInitialized = false;
static final String LOG_TAG = "AndroidExample";

SharedPreferences prefs;

Button checkBlue,proviNymi,validateNymi,disconnectNymi;

ProvisionController provisionController;
ValidationController valiationController;
boolean connectNymi = true;
int nymiHandle = Ncl.NYMI_HANDLE_ANY;
NclProvision provision;
//NclProvision provisionmid;

String temp_ID,temp_Key;
public String keyuse;
public String iduse;
public  LinearLayout progressbar;
public ProgressBar pbHeaderProgress;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.testbluetooth);

    // I tried this too  final Handler timedThread= new Handler(Looper.getMainLooper());

    final Handler timedThread=new Handler();
    timedThread.postDelayed(new Runnable() {
        @Override
        public void run() {

            NymiAsync task=new NymiAsync();
            task.execute(1,1,1);
        }
    },10000);



}

public void startValidatingNymi(){

    progressbar = (LinearLayout) findViewById(R.id.linlaHeaderProgress);
    pbHeaderProgress=(ProgressBar) findViewById(R.id.pbHeaderProgress);
    pbHeaderProgress.getIndeterminateDrawable().setColorFilter(Color.parseColor("#1109EE"), android.graphics.PorterDuff.Mode.SRC_ATOP);

    //  prefs = getSharedPreferences(Util.SharedPrefKey, Context.MODE_PRIVATE);
    //  prefs.edit().clear().commit();

    prefs=getSharedPreferences(Util.SharedPrefKey,MODE_PRIVATE);

    temp_ID = prefs.getString(Util.provID, null);

    temp_Key = prefs.getString(Util.provKey, null);

    if ((temp_ID!=null) || (temp_Key!=null)){

        // SHOW THE SPINNER WHILE LOADING FEEDS
        progressbar.setVisibility(View.VISIBLE);

        //Toast.makeText(getBaseContext(), "Nymi band is already provisined" , Toast.LENGTH_SHORT ).show();
        initializeNcl();

        provision = new NclProvision();
        load();

        if (valiationController == null) {
            valiationController = new ValidationController(TestBluetooth.this);
        }
        else {
            valiationController.stop();
        }

        valiationController.startValidation(TestBluetooth.this, provision);




        proviNymi = (Button) findViewById(R.id.provisionNymi);
        proviNymi.setOnClickListener(this);
        proviNymi.setEnabled(false);

        validateNymi = (Button) findViewById(R.id.validateNymi);
        validateNymi.setOnClickListener(this);
        validateNymi.setEnabled(false);

        disconnectNymi = (Button) findViewById(R.id.disconnectNymi);
        disconnectNymi.setOnClickListener(this);
        disconnectNymi.setEnabled(false);


    }else {

      //  Toast.makeText(getBaseContext(), "provision key is null!" , Toast.LENGTH_SHORT ).show();

        checkBlue = (Button) findViewById(R.id.testBLuetooth);
        checkBlue.setOnClickListener(this);

        proviNymi = (Button) findViewById(R.id.provisionNymi);
        proviNymi.setOnClickListener(this);

        validateNymi = (Button) findViewById(R.id.validateNymi);
        validateNymi.setOnClickListener(this);
        validateNymi.setEnabled(false);

        disconnectNymi = (Button) findViewById(R.id.disconnectNymi);
        disconnectNymi.setOnClickListener(this);
        disconnectNymi.setEnabled(false);
    }


}



public void load(){

    iduse = prefs.getString(Util.provID,null);
    Toast.makeText(getBaseContext(), iduse , Toast.LENGTH_SHORT ).show();
    keyuse = prefs.getString(Util.provKey,null);

    if ((iduse!=null)||(keyuse!=null)) {

        provision.id = new NclProvisionId();
        provision.id.v = Base64.decode(iduse, Base64.DEFAULT);
        provision.key = new NclProvisionKey();
        provision.key.v = Base64.decode(keyuse, Base64.DEFAULT);
        final String temp= keyuse.toString();
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                  Toast.makeText(getBaseContext(), "the temp provision key is " +temp , Toast.LENGTH_SHORT ).show();

            }
        });

    }else {

        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                   Toast.makeText(getBaseContext(), "Provision key is null!" , Toast.LENGTH_SHORT ).show();
            }
        });


    }
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub

    if(v.getId() == checkBlue.getId()){

       // Toast.makeText(getBaseContext(), "Checking bluetooth is enabled or not!" , Toast.LENGTH_SHORT ).show();

        BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        if (mBluetoothAdapter == null) {
            // Device does not support Bluetooth
        } else {
            if (!mBluetoothAdapter.isEnabled()) {
                // Bluetooth is not enable :)
               // Toast.makeText(getBaseContext(), " bluetooth is not enabled !" , Toast.LENGTH_SHORT ).show();
                isBluetoothEnabled=false;
            }else {

             //   Toast.makeText(getBaseContext(), " bluetooth is enabled !" , Toast.LENGTH_SHORT ).show();
                isBluetoothEnabled=true;
                Log.d("is nabled", "blue is enalble");




            }
        }

    }


    if (v.getId()==proviNymi.getId()){

        connectNymi = true;

        initializeNcl();

        nymiHandle = -1;
        if (provisionController == null) {
            provisionController = new ProvisionController(TestBluetooth.this);
        }
        else {
            provisionController.stop();
        }
        provisionController.startProvision(TestBluetooth.this);
    }


    if (v.getId()==validateNymi.getId()){

        proviNymi.setEnabled(false);
        if (valiationController == null) {
            valiationController = new ValidationController(TestBluetooth.this);
        }
        else {
            valiationController.stop();
        }
        valiationController.startValidation(TestBluetooth.this, provisionController.getProvision());
    }



    if (v.getId()==disconnectNymi.getId()){

        prefs = getSharedPreferences(Util.SharedPrefKey, Context.MODE_PRIVATE);
        prefs.edit().clear().commit();

        if (nymiHandle >= 0) {
            disconnectNymi.setEnabled(false);
            validateNymi.setEnabled(true);
            proviNymi.setEnabled(true);
            Ncl.disconnect(nymiHandle);
            nymiHandle = -1;
        }
    }



}

/**
 * Initialize the NCL library
 */
protected void initializeNcl() {
    if (!nclInitialized) {
        if (connectNymi) {
            initializeNclForNymiBand();

        }
    }
}

/**
 * Initialize NCL library for connecting to a Nymi Band
 * @return true if the library is initialized
 */
protected boolean initializeNclForNymiBand() {
    if (!nclInitialized) {


        NclCallback nclCallback = new MyNclCallback();
        boolean result = Ncl.init(nclCallback, null, "NCLExample", NclMode.NCL_MODE_DEFAULT, this);

        if (!result) { // failed to initialize NCL

            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                     Toast.makeText(TestBluetooth.this, "Failed to initialize NCL library!", Toast.LENGTH_LONG).show();

                }
            });

            return false;
        }
        nclInitialized = true;
       // nclInitialized();
    }
    return true;
}

@Override
public void onStartProcess(ProvisionController controller) {
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            Toast.makeText(TestBluetooth.this, "Nymi start provision ..",
                    Toast.LENGTH_LONG).show();

        }
    });
}




public void save(){



   final String id = Base64.encodeToString(provision.id.v, Base64.DEFAULT);
    final String key = Base64.encodeToString(provision.key.v, Base64.DEFAULT);
    SharedPreferences pref = getSharedPreferences(Util.SharedPrefKey, MODE_PRIVATE);
    SharedPreferences.Editor editor = pref.edit();
    editor.putString(Util.provID, id);
    editor.putString(Util.provKey, key);
    editor.apply();
    editor.commit();

    runOnUiThread(new Runnable() {
        public void run() {
            Toast.makeText(TestBluetooth.this, id + key,
                    Toast.LENGTH_LONG).show();
        }
    });

}

@Override
public void onAgreement(final ProvisionController controller) {
    nymiHandle = controller.getNymiHandle();
    controller.accept();
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            Toast.makeText(TestBluetooth.this, "Agree on pattern: " + Arrays.toString(controller.getLedPatterns()),
                    Toast.LENGTH_LONG).show();
        }
    });
}

@Override
public void onProvisioned(final ProvisionController controller) {
    nymiHandle = controller.getNymiHandle();
    provision = controller.getProvision();
    controller.stop();
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            proviNymi.setEnabled(false);
            validateNymi.setEnabled(true);
            Toast.makeText(TestBluetooth.this, "Nymi provisioned: " + Arrays.toString(provision.id.v),
                    Toast.LENGTH_LONG).show();
            save();
        }
    });
}

@Override
public void onFailure(ProvisionController controller) {
    controller.stop();
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            Toast.makeText(TestBluetooth.this, "Nymi provision failed!",
                    Toast.LENGTH_LONG).show();
        }
    });
}

@Override
public void onDisconnected(ProvisionController controller) {
    controller.stop();
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            validateNymi.setEnabled(provision != null);
            disconnectNymi.setEnabled(false);
            Toast.makeText(TestBluetooth.this, "Nymi disconnected: " + provision,
                    Toast.LENGTH_LONG).show();
        }
    });
}

@Override
public void onStartProcess(ValidationController controller) {
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            Toast.makeText(TestBluetooth.this, "Nymi start validation for: " + Arrays.toString(provision.id.v),
                    Toast.LENGTH_LONG).show();

        }
    });
}

@Override
public void onFound(ValidationController controller) {
    nymiHandle = controller.getNymiHandle();
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            Toast.makeText(TestBluetooth.this, "Nymi validation found Nymi on: " + Arrays.toString(provision.id.v),
                    Toast.LENGTH_LONG).show();



        }
    });
}



@Override
public void onValidated(ValidationController controller) {
    nymiHandle = controller.getNymiHandle();
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            validateNymi.setEnabled(false);
            disconnectNymi.setEnabled(true);

            // HIDE THE SPINNER AFTER LOADING FEEDS
            progressbar.setVisibility(View.GONE);

            Toast.makeText(TestBluetooth.this, "Nymi validated!",
                    Toast.LENGTH_LONG).show();

            prefs.edit().putBoolean(Util.isValidated, true).commit();

           //move to new activity once nymi is validated
            Intent intent = new Intent(TestBluetooth.this,CustomNotificationTest.class);
            startActivity(intent);

        }
    });


}

@Override
public void onFailure(ValidationController controller) {
    controller.stop();
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            Toast.makeText(TestBluetooth.this, "Nymi validated failed!",
                    Toast.LENGTH_LONG).show();
        }
    });
}

@Override
public void onDisconnected(ValidationController controller) {
    controller.stop();
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            disconnectNymi.setEnabled(false);
            validateNymi.setEnabled(true);
            proviNymi.setEnabled(true);
            Toast.makeText(TestBluetooth.this, "Nymi disconnected: " + provision,
                    Toast.LENGTH_LONG).show();
        }
    });
}

/**
 * Callback for NclEventInit
 *
 */
class MyNclCallback implements NclCallback {
    @Override
    public void call(NclEvent event, Object userData) {
        Log.d(LOG_TAG, this.toString() + ": " + event.getClass().getName());
        if (event instanceof NclEventInit) {
            if (!((NclEventInit) event).success) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(TestBluetooth.this, "Failed to initialize NCL library!", Toast.LENGTH_LONG).show();
                    }
                });
            }
        }
    }
}

}

编辑:将 NYmiAssync 作为内部类后,我可以使用以下命令异步运行它:

new Thread() {
        public void run() {

            TestBluetooth.this.runOnUiThread(new Runnable(){

                @Override
                public void run() {
                    try {
                        NymiAsync task = new NymiAsync();
                        task.execute(1, 1, 1);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                });
        }
    }.start();

但是,我不知道如何让它每 10 秒运行一次。

最佳答案

问题出在异步任务中:

public class NymiAsync extends AsyncTask<Integer,Integer,Integer> {
    @Override
    protected Integer doInBackground(Integer... integers) {
        try{
            TestBluetooth tb=new TestBluetooth();
            tb.startValidatingNymi();
        }catch (Exception e){
            e.printStackTrace();
        }
        return 0;
    }
}

只需将该类设为 TestBluetooth 的内部类,然后调用 startValidatingNymi()

public class NymiAsync extends AsyncTask<Integer,Integer,Integer> {
    @Override
    protected Integer doInBackground(Integer... integers) {
        try{
            startValidatingNymi();
        }catch (Exception e){
            e.printStackTrace();
        }
        return 0;
    }
}

关于java - 如何异步验证 Nymi band?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31123890/

相关文章:

java - 将非 Activity 类之间的变量传递给android Activity 类

java - Android:应用程序关闭后 onTaskCompleted 崩溃

c++ - Boost.Asio 异步服务器。限制为一个连接

java - 什么时候应该有自定义异常?

java - android - 生成随机日期和时间

java - 重新映射 JAX-WS 地址

android - 如何在 Kotlin 中使用 Koin 注入(inject) ViewModel?

c# - C#中的异步套接字服务器,客户端通过套接字服务器进行客户端通信

java - 自动静音 Android N 及更高版本

java - JSON异常 : A JSONObject text must begin with '{' at character