2

CH34xAndroidDriver.isConnected() 값이 true가되는 시간과 위치를 찾으려고합니다. 나는 그 값을 알아 내고 축배에 표시하려고 노력했다. 아무도 그것을 분명히 설명 할 수는 없다. 라인 74 개까지CH34xAndroidDriver.isConnected()가 true가 될 때 찾으려고합니다.

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

(Toast.makeText (global_context, .show() "155K '+ ((부울) uartInterface.isConnected()) Toast.LENGTH_SHORT)) 내가 거짓하지만 때 반환 발견 onClick()이 호출되면 true를 반환합니다. 어떤 몸든지 응답 pls가 그것을 검사하는 경우에 왜. 감사합니다.

+0

확인이

을 반환 실패하면, 당신은'BroadcastReciever'를 등록하여 동일하게 구현해야합니다 – SMR

답변

1

ResumeUsbList() 메소드는 usb 연결을 활성화하고 isConnected()를 true로 변경합니다. ResumeUsbList()는 당신이 즉시 연결로 토스트를 표시 할 경우 활동의 onResume()