androidthings / sample-uartloopback

Echo received characters over a UART with Android Things
Apache License 2.0
78 stars 39 forks source link

UART Cant open #8

Open Ragulmurugan opened 6 years ago

Ragulmurugan commented 6 years ago

E/Exception: UART cant opencom.google.android.things.pio.PioException: android.os.ServiceSpecificException: UART0 is already in use (code 16)

Fleker commented 6 years ago

Seems like you've opened it in another activity without closing it. See this Stackoverflow answer to learn more.

Ragulmurugan commented 6 years ago

But I'm closing UART before passing the Intent in every activity and this problem occurs randomly if it occurs always I can consider I forget to Close somewhere even I had the main thread.sleep before passing the Intent but nothing solved this problem.

Fleker commented 6 years ago

Where are you closing it? In the activity's onDestroy method? Do you need to architect your application with multiple activities?

Ragulmurugan commented 6 years ago

I'm closing it before passing the intent like

            try {
                closeUart();
            } catch (IOException e) {
                Log.e(TAG, "Error closing UART device:", e);
            }
            Intent i = new Intent(MainActivity.this,MainActivity2.class);
            String strName = "j";
            i.putExtra(Intent.EXTRA_TEXT, strName);
            startActivity(i);
            overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);finish();
            finish();

I'm not closing it in onDestroy method.

Fleker commented 6 years ago

Can you add a type of asynchronous wait before starting your activity, like using a Handler:

try {
    closeUart();
} catch (IOException e) {
    Log.e(TAG, "Error closing UART device:", e);
}
h = new Handler(Looper.getMainLooper()) {
    @Override
    public void handleMessage(int what) {
        Intent i = new Intent(MainActivity.this,MainActivity2.class);
        String strName = "j";
        i.putExtra(Intent.EXTRA_TEXT, strName);
        startActivity(i);
        overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);finish();
        finish();
    }
};
h.sendEmptyMessageDelayed(0, 50);

(this code wasn't tested, so it may not be completely valid)

Ragulmurugan commented 6 years ago

I have tried using main thread timer before passing the intent. Like Thread.sleep(1000). This time I'll try your idea let me use handler . Thanks dude