Android usb Host

Android设备支持otg功能,把Android手机当成Host通过otg转接线连接usb设备(u盘,usb转串口设备)
Android Host模式在Android3.1以上就直接支持了

1.Android系统端

1)确定内核驱动是否支持usb host
查看system/lib/目录下是否有libusbhost.so
system/lib/libusbhost.so
2)配置文件
是否有配置文件

$ cat /system/etc/permissions/android.hardware.usb.host.xml
<permissions>
    <feature name="android.hardware.usb.host" />
</permissions>

$ cat android.hardware.usb.accessory.xml
<permissions>
    <feature name="android.hardware.usb.accessory" />
    <library name="com.android.future.usb.accessory"
            file="/system/framework/com.android.future.usb.accessory.jar" />
</permissions>
在/system/etc/permissions下的handheld_core_hardware.xml或者tablet_core_hardware.xml文件的段中添加
<permissions>
    <feature name="android.hardware.usb.host" />
</permissions>

2.Android 应用端

添加 标签申明应用将使用 android.hardware.usb.host特性

<uses-feature android:name="android.hardware.usb.host" /><activity中增加android.hardware.usb.action.USB_DEVICE_ATTACHED
<activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
            </intent-filter>

            <meta-data
                android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
                android:resource="@xml/device_filter" />
        </activity>

device_filter只是一个过滤设备的文件,里面有usb id号,当有相应id的设备设别到应用就会弹出
检测到usb设备
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!-- 0x0403 / 0x6001: FTDI FT232R UART -->
    <usb-device vendor-id="1027" product-id="24577" />
    <!-- 0x0403 / 0x6015: FTDI FT231X -->
    <usb-device vendor-id="1027" product-id="24597" />
    <!-- 0x10C4 / 0xEA60: CP210x UART Bridge -->
    <usb-device vendor-id="4292" product-id="60000" />
</resources>

2)应用程序申请权限
当识别到usb从设备后,apk会弹出”xx应用访问usb设备”这里一定要选择
接收,进行授权处理,如果不接受不能进行usb通信,需要在代码中进行申请

private static final String ACTION_USB_PERMISSION = "com.bshui.androidserial.USB_PERMISSION";
private PendingIntent mPermissionIntent;
  mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);
        //注册USB设备权限管理广播
        IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
        registerReceiver(usbReceiver, filter);



private final BroadcastReceiver usbReceiver = new BroadcastReceiver() {

        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (ACTION_USB_PERMISSION.equals(action)) {
                synchronized (this) {
                    UsbDevice device = (UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
                    if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
                        if (device != null) {
                        }
                    } else {
                        showTmsg("用户不允许USB访问设备,程序退出!");
                        finish();
                    }
                }
            }
        }
    };

这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
以下是一个简单的Android USB Host API使用示例代码: ``` private static final String ACTION_USB_PERMISSION = "com.android.example.USB_PERMISSION"; private UsbManager usbManager; private PendingIntent permissionIntent; private UsbDevice device; private UsbDeviceConnection connection; private UsbInterface usbInterface; private UsbEndpoint endpointIn; private UsbEndpoint endpointOut; // 初始化USB Manager和Permission Intent usbManager = (UsbManager) getSystemService(Context.USB_SERVICE); permissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0); // 获取USB设备列表 HashMap<String, UsbDevice> deviceList = usbManager.getDeviceList(); // 遍历设备列表,找到需要连接的设备 for (UsbDevice usbDevice : deviceList.values()) { if (usbDevice.getVendorId() == VENDOR_ID && usbDevice.getProductId() == PRODUCT_ID) { device = usbDevice; break; } } // 如果找到了设备,请求USB权限 if (device != null) { usbManager.requestPermission(device, permissionIntent); } // 在BroadcastReceiver中处理USB权限请求结果 private final BroadcastReceiver usbReceiver = new BroadcastReceiver() { public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (ACTION_USB_PERMISSION.equals(action)) { synchronized (this) { UsbDevice usbDevice = (UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE); if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) { if (usbDevice != null) { // 获取USB设备连接 connection = usbManager.openDevice(usbDevice); if (connection != null) { // 获取USB接口和端点 usbInterface = usbDevice.getInterface(0); endpointIn = usbInterface.getEndpoint(0); endpointOut = usbInterface.getEndpoint(1); // 打开USB接口 connection.claimInterface(usbInterface, true); } } } else { Log.d(TAG, "USB permission denied"); } } } } }; // 发送数据到USB设备 private void sendToUsbDevice(byte[] data) { if (connection != null && endpointOut != null) { int result = connection.bulkTransfer(endpointOut, data, data.length, TIMEOUT); if (result < 0) { Log.e(TAG, "Failed to send data to USB device"); } } } // 从USB设备接收数据 private byte[] receiveFromUsbDevice() { if (connection != null && endpointIn != null) { byte[] buffer = new byte[endpointIn.getMaxPacketSize()]; int result = connection.bulkTransfer(endpointIn, buffer, buffer.length, TIMEOUT); if (result >= 0) { return buffer; } else { Log.e(TAG, "Failed to receive data from USB device"); } } return null; } ``` 希望这个示例代码能够帮助你使用Android USB Host API。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值