package uni.dcloud.io.uniplugin_richalert;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbManager;
import android.os.Build;
import android.util.Log;
import android.widget.Toast;
import java.util.HashMap;
public class USBTool {
private static final String TAG = USBTool.class.getSimpleName();
// private static final String ACTION_USB_PERMISSION = "com.github.mjdev.libaums.USB_PERMISSION";
private int currentDevice = -1;
private Activity activity;
private USBTool() {
}
private static USBTool instance;
public static USBTool getInstance() {
if (instance == null) {
instance = new USBTool();
}
return instance;
}
//获取USB设备ACTION
private static final String ACTION_USB_PERMISSION = "com.android.usb.USB_PERMISSION";
//开始申请USB权限
private void getUsbPermission(UsbDevice mUSBDevice, UsbManager mUsbManager) {
PendingIntent pendingIntent = PendingIntent.getBroadcast(activity, 0, new Intent(ACTION_USB_PERMISSION), 0);
IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
filter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED);
filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
activity.registerReceiver(usbReceiver, filter);
mUsbManager.requestPermission(mUSBDevice, pendingIntent); // 该代码执行后,系统弹出一个对话框/等待权限
}
/**
* Searches for connected mass storage devices, and initializes them if it
* could find some.
*/
public String discoverDevice(Activity activity) {
this.activity = activity;
// 获取USB设备列表及定位到要申请权限的USB设备
UsbManager mUsbManager = (UsbManager) activity.getSystemService(Context.USB_SERVICE);
HashMap<String, UsbDevice> devices = mUsbManager.getDeviceList();
UsbDevice usbDevice = null;
for (UsbDevice device : devices.values()) {
if(device != null){
usbDevice = device;
}
}
String mInfo = "";
// UsbDevice usbDevice = (UsbDevice) activity.getIntent().getParcelableExtra(UsbManager.EXTRA_DEVICE);
if(usbDevice != null) {
getUsbPermission(usbDevice, mUsbManager);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
mInfo =
"DeviceID: " + usbDevice.getDeviceId() + "\n" +
"DeviceName: " + usbDevice.getDeviceName() + "\n" +
"DeviceClass: " + usbDevice.getDeviceClass() + " - " +
"DeviceSubClass: " + usbDevice.getDeviceSubclass() + "\n" +
"SerialNumber: " + usbDevice.getSerialNumber() + "\n" +
"VendorID: " + usbDevice.getVendorId() + "\n" +
"ProductID: " + usbDevice.getProductId() + "\n";
}
}
return mInfo;
}
public void register(Context context) {
IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
filter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED);
filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
context.registerReceiver(usbReceiver, filter);
}
public void unregisterReceiver() {
activity.unregisterReceiver(usbReceiver);
}
private final BroadcastReceiver usbReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (ACTION_USB_PERMISSION.equals(action)) {
UsbDevice usbDevice = (UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
if (usbDevice != null) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
String mInfo = "DeviceID: " + usbDevice.getDeviceId() + "\n" +
"DeviceName: " + usbDevice.getDeviceName() + "\n" +
"DeviceClass: " + usbDevice.getDeviceClass() + " - " +
"DeviceSubClass: " + usbDevice.getDeviceSubclass() + "\n" +
"SerialNumber: " + usbDevice.getSerialNumber() + "\n" +
"VendorID: " + usbDevice.getVendorId() + "\n" +
"ProductID: " + usbDevice.getProductId() + "\n";
Log.d(TAG, "USB device mInfo=="+mInfo);
}
}
} else {
showToast("未获取到U盘权限");
}
} else if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)) {
UsbDevice device = (UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
showToast("onReceive: USB设备已连接");
Log.d(TAG, "USB device attached");
// determine if connected device is a mass storage devuce
if (device != null) {
discoverDevice(activity);
}
} else if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action)) {
UsbDevice device = (UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
showToast("onReceive: USB设备已拔出");
Log.d(TAG, "USB device detached");
// determine if connected device is a mass storage devuce
if (device != null) {
if (currentDevice != -1) {
}
// check if there are other devices or set action bar title
// to no device if not
discoverDevice(activity);
}
}
}
};
public void showToast(String s) {
Toast.makeText(activity, s, Toast.LENGTH_SHORT).show();
}
public void setUsBlistener(USBlistener usBlistener) {
this.usBlistener = usBlistener;
}
private USBlistener usBlistener = null;
public void getDeviceInfo() {
}
public interface USBlistener {
void hasPermissionUSB();
void attachedUSB();
void detachedUSB();
}
public void requestPermission(UsbDevice usbDevice) {
}
}
网友评论