寻找 CH34xAndroidDriver.isConnected() 变为 true 的时间点。

3

我正在尝试查找CH34xAndroidDriver.isConnected()何时何地变为true的值。 我已经尝试查找并在toast中显示其值。 有人能清楚地解释吗。

public class UartLoopBackActivity extends Activity {
public static final String TAG = "com.wch.wchusbdriver";
private static final String ACTION_USB_PERMISSION = "com.wch.wchusbdriver.USB_PERMISSION";
/* thread to read the data */
public readThread handlerThread;
protected final Object ThreadLock = new Object();
/* declare UART interface variable */
public CH34xAndroidDriver uartInterface;

// byte timeout; // time out
public Context global_context;
public boolean isConfiged = false;
public boolean READ_ENABLE = false;
public SharedPreferences sharePrefSettings;
Drawable originalDrawable;
public String act_string;

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

    /* create editable text objects */
    readText = (EditText) findViewById(R.id.ReadValues);
    // writeText = (EditText) findViewById(R.id.WriteValues);

    global_context = this;

    configButton = (Button) findViewById(R.id.configButton);
    originalDrawable = configButton.getBackground();
    readBuffer = new char[512];
    baudRate = 9600;
    stopBit = 1;
    dataBit = 8;
    parity = 0;
    flowControl = 0;
    configButton.setOnClickListener(new OpenDeviceListener());
    // writeButton.setOnClickListener(new OnClickedWriteButton());

    // writeButton.setEnabled(false);
    //

    uartInterface = new CH34xAndroidDriver(
            (UsbManager) getSystemService(Context.USB_SERVICE), this,
            ACTION_USB_PERMISSION);
    act_string = getIntent().getAction();
    if (-1 != act_string.indexOf("android.intent.action.MAIN")) {

        Log.d(TAG, "android.intent.action.MAIN");
    } else if (-1 != act_string
            .indexOf("android.hardware.usb.action.USB_DEVICE_ATTACHED")) {
        Log.d(TAG, "android.hardware.usb.action.USB_DEVICE_ATTACHED");
    }

    if (!uartInterface.UsbFeatureSupported()) {
        Toast.makeText(this, "No Support USB host API", Toast.LENGTH_SHORT)
                .show();
        readText.setText("No Support USB host API");
        uartInterface = null;
        Toast.makeText(global_context,
                "148k" + ((Boolean) uartInterface.isConnected()),
                Toast.LENGTH_SHORT).show();
    }

    getWindow().setSoftInputMode(
            WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

    if (READ_ENABLE == false) {
        READ_ENABLE = true;

        handlerThread = new readThread(handler);
        handlerThread.start();
        Toast.makeText(global_context,"155k" + ((Boolean) uartInterface.isConnected()),Toast.LENGTH_SHORT).show();
    }

}

public class OpenDeviceListener implements View.OnClickListener {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        boolean flags;
        Toast.makeText(global_context,"170" + ((Boolean) uartInterface.isConnected()),Toast.LENGTH_SHORT).show();
        Log.d("onClick", "12");
        if (false == isConfiged) {
            Log.d("onClick", "58");
            isConfiged = true;
            Log.d("onClick", "98");
            // writeButton.setEnabled(true);
            if (uartInterface.isConnected()) {
                Log.d("onClick", "100");
                flags = uartInterface.UartInit();
                if (!flags) {
                    Log.d(TAG, "Init Uart Error");
                    Toast.makeText(global_context, "Init Uart Error",
                            Toast.LENGTH_SHORT).show();
                } else {
                    if (uartInterface.SetConfig(baudRate, dataBit, stopBit,
                            parity, flowControl)) {
                        Log.d(TAG, "Configed");
                    }
                }
            }

            if (isConfiged == true) {
                Toast.makeText(global_context,"193" + ((Boolean) uartInterface.isConnected()),Toast.LENGTH_SHORT).show();
                Log.d("onClick", "200");
                configButton.setEnabled(false);
            }
        }

    }

}


public void onHomePressed() {
    onBackPressed();
}

public void onBackPressed() {
    super.onBackPressed();
}

protected void onResume() {
    super.onResume();
    if (2 == uartInterface.ResumeUsbList()) {
        uartInterface.CloseDevice();
        Log.d(TAG, "Enter onResume Error");
    }
}

protected void onPause() {
    super.onPause();
}

protected void onStop() {
    if (READ_ENABLE == true) {
        READ_ENABLE = false;
    }
    super.onStop();
}

protected void onDestroy() {
    if (uartInterface != null) {
        if (uartInterface.isConnected()) {
            uartInterface.CloseDevice();
        }
        uartInterface = null;
    }

    super.onDestroy();
}

final Handler handler = new Handler() {
    @Override
    public void handleMessage(Message msg) {

        if (actualNumBytes != 0x00) {
            readText.append(String.copyValueOf(readBuffer, 0,
                    actualNumBytes));
            Toast.makeText(global_context,"269k" + ((Boolean) uartInterface.isConnected()),Toast.LENGTH_SHORT).show();
            actualNumBytes = 0;
        }

    }
};

/* usb input data handler */
private class readThread extends Thread {
    Handler mHandler;

    /* constructor */
    Handler mhandler;

    readThread(Handler h) {
        mhandler = h;
        this.setPriority(Thread.MIN_PRIORITY);
    }

    public void run() {
        while (READ_ENABLE) {
            Message msg = mhandler.obtainMessage();
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
            }
            // Log.d(TAG, "Thread");
            synchronized (ThreadLock) {
                if (uartInterface != null) {
                    actualNumBytes = uartInterface.ReadData(readBuffer, 64);

                    if (actualNumBytes > 0) {
                        mhandler.sendMessage(msg);
                    }
                }
            }
        }
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.uart_loop_back, menu);
    return true;
}
}

在第74行之前的代码(Toast.makeText(global_context,"155k" + ((Boolean) uartInterface.isConnected()),Toast.LENGTH_SHORT).show();)返回false,但当调用onClick()方法时返回true。如果有人知道答案,请检查一下,谢谢。


如果你希望在连接后立即显示一个 Toast,你应该通过注册一个 BroadcastReceiver 来实现相同的效果。 - undefined
1个回答

1

方法 ResumeUsbList() 可以启用 USB 连接并将 isConnected() 更改为 true。如果 ResumeUsbList() 失败,它会返回 2。

检查您的 Activity 的 onResume()。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接