当前位置: 首页 > 工具软件 > USBManager > 使用案例 >

UsbManager, UsbDevice的简单示例

岳嘉容
2023-12-01

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical">

    <TextView
        android:id="@+id/msgTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Message Stub"
        android:textColor="@android:color/holo_red_light" />

    <Button
        android:id="@+id/retrievePrinterButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="检索打印机" />
    <Button
        android:id="@+id/requestPermissionButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="请求权限" />
</LinearLayout>
MainActivity.java
package com.raycloud.wolf.usbmanagertest;

import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.hardware.usb.UsbConstants;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;

public class MainActivity extends AppCompatActivity {
    private TextView msgTextView;
    private UsbManager usbManager;
    private HashSet<UsbDevice> printerSet = new HashSet();
    private BroadcastReceiver usbBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action.equals(UsbManager.ACTION_USB_DEVICE_ATTACHED)) {
                Toast.makeText(context, "接入了一个USB设备", Toast.LENGTH_SHORT).show();
                retrievePrinter();
            } else if (action.equals(UsbManager.ACTION_USB_DEVICE_DETACHED)) {
                Toast.makeText(context, "移除了一个USB设备", Toast.LENGTH_SHORT).show();
                retrievePrinter();
            } else {
                if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false))
                    Toast.makeText(context, "已授予打印机USB权限", Toast.LENGTH_SHORT).show();
                else
                    Toast.makeText(context, "未授予打印机USB权限", Toast.LENGTH_SHORT).show();
            }
        }
    };
    //    private final String tag = "MainActivity";
    private static final String ACTION_USB_PERMISSION_GRANTED = "com.raycloud.wolf.usbmanagertest.action.USB_PERMISSION_GRANTED";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        msgTextView = findViewById(R.id.msgTextView);
        findViewById(R.id.retrievePrinterButton).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                retrievePrinter();
            }
        });
        findViewById(R.id.requestPermissionButton).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Iterator<UsbDevice> iterator = printerSet.iterator();
                while (iterator.hasNext()) {
                    UsbDevice usbDevice = iterator.next();
                    if (!usbManager.hasPermission(usbDevice))
                        usbManager.requestPermission(usbDevice, PendingIntent.getBroadcast(MainActivity.this, 0, new Intent(ACTION_USB_PERMISSION_GRANTED), 0));
                    Toast.makeText(MainActivity.this, "打印机已具备USB权限", Toast.LENGTH_SHORT).show();
                }
            }
        });
        usbManager = (UsbManager) getSystemService(USB_SERVICE);
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED);
        intentFilter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
        intentFilter.addAction(ACTION_USB_PERMISSION_GRANTED);
        registerReceiver(usbBroadcastReceiver, intentFilter);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(usbBroadcastReceiver);
    }

    private void retrievePrinter() {
        HashMap deviceList = usbManager.getDeviceList();
        Iterator<UsbDevice> iterator = deviceList.values().iterator();
        int numberOfPrinters = 0;
        while (iterator.hasNext()) {
            UsbDevice usbDevice = iterator.next();
            int interfaceCount = usbDevice.getInterfaceCount();
            for (int i = 0; i != interfaceCount; ++i) {
                if (usbDevice.getInterface(i).getInterfaceClass() == UsbConstants.USB_CLASS_PRINTER) {
                    ++numberOfPrinters;
                    printerSet.add(usbDevice);
                }
            }
        }
        msgTextView.setText(String.format("检索到%d台打印机", numberOfPrinters));
    }
}

 

转载于:https://www.cnblogs.com/buyishi/p/8867706.html

 类似资料: