如何异步验证Nymi频段?

时间:2015-06-29 19:24:39

标签: java android asynchronous

我试图异步验证Nymi频段。但是当我尝试这样做时,我得到以下异常:

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

我可以看到以下方法中的所有祝酒词:

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

但我仍然得到例外。在onCreate()方法中正常运行时,该方法可以正常工作,但无法异步运行。

编辑:即使删除了所有的祝酒词,我仍然会得到例外。

这是我的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作为内部类之后,我可以使用以下命令运行它async:

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秒运行一次。

2 个答案:

答案 0 :(得分:1)

问题在于asynctask:

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;
    }
}

答案 1 :(得分:1)

这是因为TestBlutooth中的代码:

 final Handler timedThread=new Handler();

但是在doInBackground中你创建了一个TestBluetooth实例,所以你得到了异常