文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

当 USB 设备连接到 Android 设备时会发生什么?

2024-11-30 00:30

关注
UsbDevice[
    mName=/dev/bus/usb/002/005,
    mVendorId=1008,
    mProductId=1694,
    mClass=0,
    mSubclass=0,
    mProtocol=0,
    mManufacturerName=HP Inc.,
    mProductName=HP Laser 1008a,
    mVersion=2.0,
    mSerialNumber=CNB1RC683F,
    mConfigurations=[
        UsbConfiguration[
            mId=1,
            mName=null,
            mAttributes=192,
            mMaxPower=1,
            mInterfaces=[
                UsbInterface[
                    mId=0,
                    mAlternateSetting=0,
                    mName=null,
                    mClass=7,
                    mSubclass=1,
                    mProtocol=2,
                    mEndpoints=[
                        UsbEndpoint[mAddress=2,mAttributes=2,mMaxPacketSize=512,mInterval=10]
                        UsbEndpoint[mAddress=129,mAttributes=2,mMaxPacketSize=512,mInterval=10]
                    ]
                UsbInterface[
                    mId=0,
                    mAlternateSetting=1,
                    mName=null,
                    mClass=7,
                    mSubclass=1,
                    mProtocol=4,
                    mEndpoints=[
                        UsbEndpoint[mAddress=2,mAttributes=2,mMaxPacketSize=512,mInterval=10]
                        UsbEndpoint[mAddress=129,mAttributes=2,mMaxPacketSize=512,mInterval=10]
                    ]
                UsbInterface[
                    mId=1,
                    mAlternateSetting=0,
                    mName=null,
                    mClass=255,
                    mSubclass=4,
                    mProtocol=1,
                    mEndpoints=[
                        UsbEndpoint[mAddress=6,mAttributes=2,mMaxPacketSize=512,mInterval=10]
                        UsbEndpoint[mAddress=133,mAttributes=2,mMaxPacketSize=512,mInterval=10]
                    ]
                UsbInterface[
                    mId=1,
                    mAlternateSetting=1,
                    mName=null,
                    mClass=7,
                    mSubclass=1,
                    mProtocol=4,
                    mEndpoints=[
                        UsbEndpoint[mAddress=6,mAttributes=2,mMaxPacketSize=512,mInterval=10]
                        UsbEndpoint[mAddress=133,mAttributes=2,mMaxPacketSize=512,mInterval=10]
                    ]
                ]
            ]

VID

(Vendor ID)是USB设备的一个标识符,由USB Implementers Forum(USB-IF)分配给设备制造商的一个16位的十六进制数,用于标识其生产的设备。每个设备制造商都有一个唯一的VID。

在USB设备中,VID与PID(Product ID)一起使用,用于识别和管理设备。当USB设备连接到计算机时,操作系统会检测设备的VID和PID,并根据这些信息加载相应的驱动程序,确保设备能够正常工作。

厂商在开发USB产品前,需要从USB-IF取得厂商标识符(Vendor ID)。申请VID的方式包括成为USB-IF会员并缴纳年费,或者通过第三方机构进行申请。获得VID后,厂商可以进行USB测试,并将测试结果连同USB商标许可协议递交给USB-IF协会进行审核。

PID

(Product ID)是USB设备的一个标识符,用于标识同一制造商生产的不同设备。PID由设备制造商定义,长度通常为8位,由低4位的类型字段和高4位的校验字段组成。类型字段的不同组合用于标识不同类型的USB数据包,如令牌包、握手包、数据包和特殊包。

在USB通信中,PID字段用于指明数据传输的方向、帧开始、数据传输的结果以及数据包的奇偶性等。

在USB设备的识别和管理中,VID和PID一起发挥着关键作用。当USB设备挂载连接到时,操作系统会检测设备的VID和PID,根据这些信息加载相应的驱动程序,确保设备能够正常工作。VID和PID还用于设备管理,包括设备的连接和断开、设备的状态监测和控制等。

ADB WiFi中USB连接处理

通用获取USB设备方法:

UsbManager usbManager = (UsbManager) getSystemService(Context.USB_SERVICE);  
HashMap deviceList = usbManager.getDeviceList();  
if (!deviceList.isEmpty()) {  
    for (Map.Entry entry : deviceList.entrySet()) {  
        UsbDevice device = entry.getValue();  
        int vendorId = device.getVendorId();  
        int productId = device.getProductId();  
        String deviceName = device.getDeviceName();  
        String productName = device.getProductName();  
        String manufacturerName = device.getManufacturerName();  
          
        // 打印设备信息  
        Log.d("USB设备信息", "Vendor ID: " + vendorId);  
        Log.d("USB设备信息", "Product ID: " + productId);  
        Log.d("USB设备信息", "Device Name: " + deviceName);  
        Log.d("USB设备信息", "Product Name: " + productName);  
        Log.d("USB设备信息", "Manufacturer: " + manufacturerName);  
    }  
} else {  
    Log.d("USB设备信息", "没有USB设备连接.");  
}

广播实时监听USB设备接入拔出:

public class MainActivity extends AppCompatActivity {  
    private UsbReceiver usbReceiver;  
    private UsbManager usbManager;  
  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
  
        usbManager = (UsbManager) getSystemService(Context.USB_SERVICE);  
        usbReceiver = new UsbReceiver();  
        IntentFilter filter = new IntentFilter();  
        filter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED);  
        filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);  
        registerReceiver(usbReceiver, filter);  
    }  
  
    @Override  
    protected void onResume() {  
        super.onResume();  
        registerReceiver(usbReceiver, new IntentFilter(UsbManager.ACTION_USB_PERMISSION));  
    }  
  
    @Override  
    protected void onPause() {  
        super.onPause();  
        unregisterReceiver(usbReceiver);  
    }  
  
    private final class UsbReceiver extends BroadcastReceiver {  
        public void onReceive(Context context, Intent intent) {  
            String action = intent.getAction();  
            if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)) {  
                UsbDevice device = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);  
                if (device != null) {  
                    // 请求USB设备访问权限  
                    PendingIntent pi = PendingIntent.getBroadcast(context, 0, new Intent(ACTION_USB_PERMISSION), 0);  
                    usbManager.requestPermission(device, pi);  
                }  
            } else if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action)) {  
                UsbDevice device = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);  
                if (device != null) {  
                    // 处理USB设备拔出事件  
                }  
            } else if (UsbManager.ACTION_USB_PERMISSION.equals(action)) {  
                synchronized (this) {  
                    UsbDevice device = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);  
                    if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {  
                        if (device != null) {  
                            // USB设备权限被授予,可以进行通信了  
                        }  
                    } else {  
                        // 权限被拒绝,处理逻辑  
                    }  
                }  
            }  
        }  
    }  
}

ADB WiFi实时监听USB设备接入处理:

public class ADBControlEntrance {

    private static final String TAG = ADBControlEntrance.class.getSimpleName();

    private static volatile ADBControlEntrance sInstance;

    private UsbManager mUsbManager;
    private UsbDevice mUsbDevice;
    private AdbCrypto mCrypto;
    private AdbConnection mConnection;
    private AdbStream mStream;

    private final List mTerminalDataListenerList = Collections.synchronizedList(new ArrayList<>());

    private final List mWriteDataList = new ArrayList<>();
    private final Object mWriteSyncLock = new Object();

    private ReadDataThread mReadDataThread;
    private WriteDataThread mWriteDataThread;
    private Thread mConnectThread;

    private ADBControlEntrance() {

    }

    public static ADBControlEntrance getInstance() {
        if (sInstance == null) {
            synchronized (ADBControlEntrance.class) {
                if (sInstance == null) {
                    sInstance = new ADBControlEntrance();
                }
            }
        }
        return sInstance;
    }

    public void init(Context context) {
        mUsbManager = (UsbManager) context.getApplicationContext().getSystemService(Context.USB_SERVICE);

        try {
            mCrypto = AdbCrypto.generateAdbKeyPair(new Base64Impl());
            mCrypto.saveAdbKeyPair(new File(context.getFilesDir(), "private_key"), new File(context.getFilesDir(), "public_key"));
        } catch (NoSuchAlgorithmException | IOException e) {
            Log.e(TAG, "初始化创建密钥对失败:", e);
        }

        mReadDataThread = new ReadDataThread();
        mReadDataThread.start();

        mWriteDataThread = new WriteDataThread();
        mWriteDataThread.start();

        //注册USB广播
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(Constants.USB_PERMISSION);
        intentFilter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED);
        intentFilter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            context.registerReceiver(mUsbDeviceReceiver, intentFilter, Context.RECEIVER_EXPORTED);
        } else {
            context.registerReceiver(mUsbDeviceReceiver, intentFilter);
        }

        //遍历USB设备
        HashMap deviceList = mUsbManager.getDeviceList();
        for (UsbDevice device : deviceList.values()) {
            if (mUsbManager.hasPermission(device)) {
                asyncRefreshConnection(device);
            } else {
                //请求权限
                requestPermission(context, device);
            }
        }
    }

    public void addTerminalDataListener(TerminalDataListener terminalDataListener) {
        mTerminalDataListenerList.add(terminalDataListener);
    }

    public void removeTerminalDataListener(TerminalDataListener terminalDataListener) {
        mTerminalDataListenerList.remove(terminalDataListener);
    }

    public void removeAllTerminalDataListener() {
        mTerminalDataListenerList.clear();
    }

    public void release() {
        closeConnection(false);
        if (mReadDataThread != null) {
            mReadDataThread.interrupt();
            mReadDataThread = null;
        }
        if (mWriteDataThread != null) {
            mWriteDataThread.interrupt();
            mWriteDataThread = null;
        }
        mTerminalDataListenerList.clear();
    }

    public boolean isConnected() {
        return mConnection != null && mConnection.isConnected();
    }

    private void asyncRefreshConnection(UsbDevice device) {
        closeConnection(true);
        if (device == null) return;
        UsbInterface usbInterface = null;
        for (int index = 0; index < device.getInterfaceCount(); index++) {
            UsbInterface interface1 = device.getInterface(index);
            if (interface1.getInterfaceClass() == 255 && interface1.getInterfaceSubclass() == 66 && interface1.getInterfaceProtocol() == 1) {
                usbInterface = interface1;
                break;
            }
        }
        if (usbInterface == null) return;
        UsbDeviceConnection deviceConnection = mUsbManager.openDevice(device);
        if (deviceConnection.claimInterface(usbInterface, false)) {
            UsbChannel usbChannel = new UsbChannel(deviceConnection, usbInterface);
            try {
                mConnection = AdbConnection.create(usbChannel, mCrypto);
                mConnection.connect();
                mStream = mConnection.open("shell:");
                mUsbDevice = device;
                //设备已连接
                for (TerminalDataListener terminalDataListener : mTerminalDataListenerList) {
                    terminalDataListener.onDeviceConnect(0);
                }
                Log.d(TAG, "USB ADB连接:成功");
            } catch (IOException | InterruptedException e) {
                Log.e(TAG, "USB ADB连接", e);
                for (TerminalDataListener terminalDataListener : mTerminalDataListenerList) {
                    terminalDataListener.onDeviceDisConnect();
                }
            }
        } else {
            deviceConnection.close();
            for (TerminalDataListener terminalDataListener : mTerminalDataListenerList) {
                terminalDataListener.onDeviceDisConnect();
            }
        }
    }

    public void asyncRefreshConnection(String host) {
        closeConnection(false);
        if (TextUtils.isEmpty(host)) return;

        if (mConnectThread != null) {
            mConnectThread.interrupt();
            mConnectThread = null;
        }
        mConnectThread = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Socket socket = new Socket(host, 5555);
                    TcpChannel tcpChannel = new TcpChannel(socket);
                    mConnection = AdbConnection.create(tcpChannel, mCrypto);
                    mConnection.connect();
                    mStream = mConnection.open("shell:");
                    //设备已连接
                    for (TerminalDataListener terminalDataListener : mTerminalDataListenerList) {
                        terminalDataListener.onDeviceConnect(1);
                    }
                    Log.d(TAG, "TCP ADB连接:成功");
                } catch (IOException | InterruptedException e) {
                    Log.e(TAG, "TCP ADB连接:", e);
                    for (TerminalDataListener terminalDataListener : mTerminalDataListenerList) {
                        terminalDataListener.onDeviceDisConnect();
                    }
                }
            }
        });
        mConnectThread.start();
    }

    private class ReadDataThread extends Thread {
        @Override
        public void run() {
            super.run();
            while (!interrupted()) {
                asyncReadBuffer();
            }
        }
    }

    private void asyncReadBuffer() {
        if (mConnection != null && mStream != null) {
            while (!mStream.isClosed()) {
                try {
                    byte[] buffer = mStream.read();
                    String output = new String(buffer, StandardCharsets.US_ASCII);
                    for (TerminalDataListener terminalDataListener : mTerminalDataListenerList) {
                        terminalDataListener.onTerminalOutput(output);
                    }
                } catch (InterruptedException | IOException e) {
                    Log.e(TAG, "读取终端数据:", e);
                }
            }
        }
    }

    private class WriteDataThread extends Thread {
        @Override
        public void run() {
            super.run();
            while (!isInterrupted()) {
                if (mWriteDataList.isEmpty()) {
                    try {
                        synchronized (mWriteSyncLock) {
                            mWriteSyncLock.wait();
                        }
                    } catch (InterruptedException e) {
                        Log.e(TAG, "发送终端数据:", e);
                    }
                } else {
                    String command = mWriteDataList.remove(0);
                    try {
                        //发送命令
                        mStream.write((command + "\n").getBytes(StandardCharsets.UTF_8));
                    } catch (IOException | InterruptedException e) {
                        Log.e(TAG, "发送终端数据:", e);
                    }
                }
            }
        }
    }

    public boolean asyncWriteBuffer(String command) {
        if (TextUtils.isEmpty(command)) return false;
        if (mStream == null || mStream.isClosed()) return false;
        mWriteDataList.add(command);
        synchronized (mWriteSyncLock) {
            mWriteSyncLock.notify();
        }
        return true;
    }

    public void closeConnection(boolean callback) {
        //关闭连接
        if (mConnection != null) {
            try {
                mConnection.close();
            } catch (IOException e) {
                Log.e(TAG, "关闭USB ADB连接:", e);
            }
            mConnection = null;
        }
        if (callback) {
            for (TerminalDataListener terminalDataListener : mTerminalDataListenerList) {
                terminalDataListener.onDeviceDisConnect();
            }
        }
    }

    private void requestPermission(Context context, UsbDevice device) {
        mUsbManager.requestPermission(device, PendingIntent.getBroadcast(context.getApplicationContext(), 0, new Intent(Constants.USB_PERMISSION), PendingIntent.FLAG_IMMUTABLE));
    }

    private final BroadcastReceiver mUsbDeviceReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(intent.getAction())) {
                UsbDevice device = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
                if (device != null && mUsbDevice != null) {
                    //断开连接的和当前连接的是同一个设备 断开连接
                    if (mUsbDevice.getDeviceName().equals(device.getDeviceName())) {
                        //关闭连接
                        closeConnection(true);
                    }
                }
            }
            if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(intent.getAction())) {
                UsbDevice device = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
                if (device != null && mUsbDevice != null) {
                    //连接的和当前连接的不是同一个设备
                    if (!mUsbDevice.getDeviceName().equals(device.getDeviceName())) {
                        //开始连接
                        if (mUsbManager.hasPermission(device)) {
                            asyncRefreshConnection(device);
                        } else {
                            //请求权限
                            requestPermission(context, device);
                        }
                    }
                }
            }

            if (Constants.USB_PERMISSION.equals(intent.getAction())) {
                UsbDevice device = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
                //开始连接
                if (mUsbManager.hasPermission(device)) {
                    asyncRefreshConnection(device);
                } else {
                    //请求权限
                    requestPermission(context, device);
                }
            }
        }
    };
}
来源:沐雨花飞蝶内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-后端开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯