欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

检查USB是否连接

程序员文章站 2024-02-11 14:56:40
...

//

 /**
     * 检测USB是否连接
     */
    private BroadcastReceiver mUSBReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)) {
                LogUtil.i("插入USB,继续线程", "log.txt");
                UpdatePresenter.IOSource oldIOSrc = mUpdatePresenter.getIOSource();
                LogUtil.i("连接USB" + mUpdatePresenter.getIOSource() + ",mIOSource:" + oldIOSrc);
                mUpdatePresenter.setIOSource(UpdatePresenter.IOSource.RS232);
                ToastUtil.showLong(App.application, "连接USB");
            }
            if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action)) {
                boolean isSuccess = mUpdatePresenter.restoreBackupIOSource();
                if (!isSuccess) {
                    mUpdatePresenter.setIOSource(UpdatePresenter.IOSource.BLUETOOTH);
                }
                LogUtil.i("断开USB,暂停线程", "log.txt");
                ToastUtil.showLong(App.application, "断开USB");
            }
        }
    };