2

我試圖找到何時何地CH34xAndroidDriver.isConnected()值成爲true。 我試圖找出並在烤麪包中顯示它的值。任何人都可以解釋清楚。試圖找到什麼時候CH34xAndroidDriver.isConnected()變爲真

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」 +((布爾)uartInterface.isConnected()),Toast.LENGTH_SHORT).show();),我發現它返回false但是當onClick()被稱爲它返回true。爲什麼如果任何機構有答案請檢查它。 謝謝

+0

如果你想,只要你連接顯示敬酒,你應該通過註冊'BroadcastReciever'實現相同 – SMR 2014-09-22 10:15:29

回答

1

方法ResumeUsbList()啓用USB連接並將isConnected()更改爲true。如果ResumeUsbList()失敗則返回2

檢查活動的的onResume()

相關問題