Android USB串口通信实现 以及绕过USB弹框验证
时间: 2023-12-01 14:04:52 浏览: 183
Android USB串口通信实现:
1. 首先需要在AndroidManifest.xml文件中添加USB权限:
```xml
<uses-feature android:name="android.hardware.usb.host" />
<uses-permission android:name="android.permission.USB_PERMISSION" />
```
2. 获取USB设备权限:
```java
private static final String ACTION_USB_PERMISSION = "com.android.example.USB_PERMISSION";
private UsbManager mUsbManager;
private PendingIntent mPermissionIntent;
private void requestPermission() {
mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);
IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
registerReceiver(mUsbReceiver, filter);
HashMap<String, UsbDevice> deviceList = mUsbManager.getDeviceList();
for (UsbDevice device : deviceList.values()) {
mUsbManager.requestPermission(device, mPermissionIntent);
}
}
private final BroadcastReceiver mUsbReceiver = 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) {
// 权限获取成功,进行USB通信
}
} else {
// 权限获取失败
}
}
}
}
};
```
3. 进行USB通信:
```java
private UsbDeviceConnection mConnection;
private UsbEndpoint mEndpointIn;
private UsbEndpoint mEndpointOut;
private void setupDevice(UsbDevice device) {
mConnection = mUsbManager.openDevice(device);
if (mConnection != null && mConnection.claimInterface(device.getInterface(0), true)) {
UsbInterface intf = device.getInterface(0);
for (int i = 0; i < intf.getEndpointCount(); i++) {
UsbEndpoint endpoint = intf.getEndpoint(i);
if (endpoint.getDirection() == UsbConstants.USB_DIR_IN) {
mEndpointIn = endpoint;
} else if (endpoint.getDirection() == UsbConstants.USB_DIR_OUT) {
mEndpointOut = endpoint;
}
}
} else {
mConnection = null;
}
}
private void sendData(byte[] data) {
if (mConnection != null && mEndpointOut != null) {
mConnection.bulkTransfer(mEndpointOut, data, data.length, TIMEOUT);
}
}
private byte[] readData() {
byte[] buffer = new byte[1024];
if (mConnection != null && mEndpointIn != null) {
mConnection.bulkTransfer(mEndpointIn, buffer, buffer.length, TIMEOUT);
}
return buffer;
}
```
绕过USB弹框验证:
1. 获取系统管理员权限:
```java
public void getAdmin() {
Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, new ComponentName(this, AdminReceiver.class));
startActivityForResult(intent, REQUEST_CODE_ADD_DEVICE_ADMIN);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE_ADD_DEVICE_ADMIN && resultCode == RESULT_OK) {
// 系统管理员权限获取成功
}
}
```
2. 使用DevicePolicyManager方法进行操作:
```java
private DevicePolicyManager mDpm;
private void disableUsbDialog() {
mDpm = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
ComponentName componentName = new ComponentName(this, AdminReceiver.class);
mDpm.addUserRestriction(componentName, UserManager.DISALLOW_USB_FILE_TRANSFER);
}
private void enableUsbDialog() {
mDpm.clearUserRestriction(new ComponentName(this, AdminReceiver.class), UserManager.DISALLOW_USB_FILE_TRANSFER);
}
```
其中,AdminReceiver类需要继承DeviceAdminReceiver,并在AndroidManifest.xml文件中进行注册。
阅读全文