其他分享
首页 > 其他分享> > Android USB串口通信实现 以及绕过USB弹框验证,androidstudio社区

Android USB串口通信实现 以及绕过USB弹框验证,androidstudio社区

作者:互联网

上面的代码运行之后,如果没有问题则会得到一个UsbDevice,先看看google文档给出的这个类的解释:

This class represents a USB device attached to the android device with the android device acting as the USB host. Each device contains one or more UsbInterfaces, each of which contains a number of UsbEndpoints (the channels via which data is transmitted over USB).

此类表示连接到Android设备的USB设备,其中android设备充当USB主机。 每个设备都包含一个或多个UsbInterfaces,每个UsbInterfaces包含许多UsbEndpoints(相当于一个通道,通过USB来进行数据传输的通道)。

其实这个类就是用来描述USB设备的信息的,可以通过这个类获取到设备的输出输入端口,以及设备标识等信息。

获取到需要的设备之后,请求使用权限:

private static final String ACTION_USB_PERMISSION = “com.android.example.USB_PERMISSION”;
public static final String ACTION_USB_ATTACHED = “android.hardware.usb.action.USB_DEVICE_ATTACHED”;
public static final String ACTION_USB_DETACHED = “android.hardware.usb.action.USB_DEVICE_DETACHED”;
private void requestUserPermission() {
Intent intent = new Intent(ACTION_USB_PERMISSION);
PendingIntent mPermissionIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
IntentFilter permissionFilter = new IntentFilter(ACTION_USB_PERMISSION);
context.registerReceiver(usbPermissionReceiver, permissionFilter);
//申请权限 会弹框提示用户授权
usbManager.requestPermission(usbDevice, mPermissionIntent);
}

这里我们声明一个广播Receiver,当接受到授权成功的广播后做一些其他处理:

private boolean serialPortConnected;
private UsbDeviceConnection connection;
private final BroadcastReceiver cardReaderReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context arg0, Intent arg1) {
if (arg1.getAction().equals(ACTION_USB_PERMISSION)) {
boolean granted = arg1.getExtras().getBoolean(UsbManager.EXTRA_PERMISSION_GRANTED); //用户是否同意授权使用usb
if (granted)
{
connection = usbManager.openDevice(device); //建立一个连接,通过这个连接读写数据
new ConnectionThread().start(); //开始读写数据
}
} else if (arg1.getAction().equals(ACTION_USB_ATTACHED)) {
if (!serialPortConnected)
findSerialPortDevice();
} else if (arg1.getAction().equals(ACTION_USB_DETACHED)) {
serialPortConnected = false;
}
}
};

收发数据

授权成功之后,就可以建立一个连接来读写数据了。UsbDeviceConnection就是这个连接

google文档给出的解释是:

This class is used for sending and receiving data and control messages to a USB device. Instances of this class are created by openDevice(UsbDevice).

这个类用于向USB设备发送和接收数据,以及控制消息。 它的实例由openDevice(UsbDevice)这个方法创建。

在这个时候,我们已经可以和设备进行数据传输了(理论上)。在大部分情况下还需要对USB串口进行一些配置,比如波特率,停止位,数据控制等,不然两边配置不同,收到的数据会乱码。具体怎么配置,需要看串口设备的芯片是什么了,现在主流的基本上就是PL2303,我使用的转接线也是PL2303的。幸运的是github上有个专门的库UsbSerial,将这些繁琐的配置都打包好了,我们直接用就好了,使用方法可以去github上看,写得很详细。

发送命令

那怎么给usb外设发送数据呢?UsbDeviceConnection有一个方法用于发送数据:

int bulkTransfer(outEndpoint, data, data.length, TIMEOUT);

第一个参数是数据传输的端口,这个端口可不是随便设置的,我们要找到具有数据传输功能的接口UsbInterface,从它里面找到数据输入和输出端口UsbEndpoint 。

mInterface = device.getInterface(0); //一般第一个就是我们需要的
int numberEndpoints = mInterface.getEndpointCount();
for(int i=0;i<=numberEndpoints-1;i++)
{
UsbEndpoint endpoint = mInterface.getEndpoint(i);
if(endpoint.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK
&& endpoint.getDirection() == UsbConstants.USB_DIR_IN)
inEndpoint = endpoint;
else if(endpoint.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK
&& endpoint.getDirection() == UsbConstants.USB_DIR_OUT)
outEndpoint = endpoint;
}

第二个参数是发送的数据

第三个参数是数据的大小,最后一个参数是设置超时时间。这个方法的返回值是int类型,它表示本次发送数据成功的字节数,如果失败的话,就返回-1。

接收数据

我们已经找到了数据输入端口usbEndpointIn,因为数据的输入是不定时的,因此我们可以另开一个线程,来专门接受数据。

int maxSize = inEndpoint.getMaxPacketSize();
ByteBuffer byteBuffer = ByteBuffer.allocate(maxSize); //创建一个缓冲区接收数据
U

《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》

【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享

sbRequest usbRequest = new UsbRequest(); //注意UsbRequest是异步处理的
usbRequest.initialize(connection, inEndpoint);
usbRequest.queue(byteBuffer, maxSize);
if(connection.requestWait() == usbRequest){
byte[] retData = byteBuffer.array();
for(Byte byte1 : retData){
Log.d(TAG,byte1)
}
}

绕过USB系统授权

不知道是Android的bug还是什么,给usb授权的时候会有一个弹框提醒,虽然可以勾选不再提示,但是没有任何用,关机重启之后,还是会重新弹出来。因为是Android开发板,就算外接显示屏,也不会触屏呀!一两台设备还好,外接鼠标搞定,要是上百上千台,那不得累死!

所以有没有什么方法,可以跳过USB授权验证呢?答案是有的。

我们不需要这个弹框,可以看看点击弹框确认按钮之后做了什么操作。我们可以模仿点击确认之后的流程,骗过系统。

当弹框出现的时候,可以通过adb shell查看当前的activity:

adb shell dumpsys activity | grep -i run

可以清楚的看到当前的activity是UsbPermissionActivity,AndroidSdk里面是可以搜得到这个activity的,我的开发板是6.0的,所以选的android-23,那我们分析一下这个activity做了些什么。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-s8a99Dh9-1638155876307)(https://user-gold-cdn.xitu.io/2019/6/18/16b69777645bceb3?imageView2/0/w/1280/h/960/ignore-error/1)]

先把代码全部贴出来:

public class UsbPermissionActivity extends AlertActivity
implements DialogInterface.OnClickListener, CheckBox.OnCheckedChangeListener {

private static final String TAG = “UsbPermissionActivity”;

private CheckBox mAlwaysUse;
private TextView mClearDefaultHint;
private UsbDevice mDevice;
private UsbAccessory mAccessory;
private PendingIntent mPendingIntent;
private String mPackageName;
private int mUid;
private boolean mPermissionGranted;
private UsbDisconnectedReceiver mDisconnectedReceiver;

@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);

Intent intent = getIntent();
mDevice = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
mAccessory = (UsbAccessory)intent.getParcelableExtra(UsbManager.EXTRA_ACCESSORY);
mPendingIntent = (PendingIntent)intent.getParcelableExtra(Intent.EXTRA_INTENT);
mUid = intent.getIntExtra(Intent.EXTRA_UID, -1);
mPackageName = intent.getStringExtra(“package”);

PackageManager packageManager = getPackageManager();
ApplicationInfo aInfo;
try {
aInfo = packageManager.getApplicationInfo(mPackageName, 0);
} catch (PackageManager.NameNotFoundException e) {
Log.e(TAG, “unable to look up package name”, e);
finish();
return;
}
String appName = aInfo.loadLabel(packageManager).toString();

final AlertController.AlertParams ap = mAlertParams;
ap.mIcon = aInfo.loadIcon(packageManager);
ap.mTitle = appName;
if (mDevice == null) {
ap.mMessage = getString(R.string.usb_accessory_permission_prompt, appName);
mDisconnectedReceiver = new UsbDisconnectedReceiver(this, mAccessory);
} else {
ap.mMessage = getString(R.string.usb_device_permission_prompt, appName);
mDisconnectedReceiver = new UsbDisconnectedReceiver(this, mDevice);
}
ap.mPositiveButtonText = getString(android.R.string.ok);
ap.mNegativeButtonText = getString(android.R.string.cancel);
ap.mPositiveButtonListener = this;
ap.mNegativeButtonListener = this;

// add “always use” checkbox
LayoutInflater inflater = (LayoutInflater)getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
ap.mView = inflater.inflate(com.android.internal.R.layout.always_use_checkbox, null);
mAlwaysUse = (CheckBox)ap.mView.findViewById(com.android.internal.R.id.alwaysUse);
if (mDevice == null) {
mAlwaysUse.setText(R.string.always_use_accessory);
} else {
mAlwaysUse.setText(R.string.always_use_device);
}
mAlwaysUse.setOnCheckedChangeListener(this);
mClearDefaultHint = (TextView)ap.mView.findViewById(
com.android.internal.R.id.clearDefaultHint);
mClearDefaultHint.setVisibility(View.GONE);

setupAlert();

}

@Override
public void onDestroy() {
//根据用户的操作决定是否调用service的方法
IBinder b = ServiceManager.getService(USB_SERVICE);
IUsbManager service = IUsbManager.Stub.asInterface(b);
// send response via pending intent
Intent intent = new Intent();
try {
if (mDevice != null) {
intent.putExtra(UsbManager.EXTRA_DEVICE, mDevice);
if (mPermissionGranted) {
service.grantDevicePermission(mDevice, mUid);
if (mAlwaysUse.isChecked()) {
final int userId = UserHandle.getUserId(mUid);
service.setDevicePackage(mDevice, mPackageName, userId);
}
}
}
if (mAccessory != null) {
intent.putExtra(UsbManager.EXTRA_ACCESSORY, mAccessory);
if (mPermissionGranted) {
service.grantAccessoryPermission(mAccessory, mUid);
if (mAlwaysUse.isChecked()) {
final int userId = UserHandle.getUserId(mUid);
service.setAccessoryPackage(mAccessory, mPackageName, userId);
}
}
}
intent.putExtra(UsbManager.EXTRA_PERMISSION_GRANTED, mPermissionGranted);
mPendingIntent.send(this, 0, intent);
} catch (PendingIntent.CanceledException e) {
Log.w(TAG, “PendingIntent was cancelled”);
} catch (RemoteException e) {
Log.e(TAG, “IUsbService connection failed”, e);
}

if (mDisconnectedReceiver != null) {
unregisterReceiver(mDisconnectedReceiver);
}
super.onDestroy();
}

public void onClick(DialogInterface dialog, int which) {
//记录下用户的操作
if (which == AlertDialog.BUTTON_POSITIVE) {
mPermissionGranted = true;
}
finish();
}

public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (mClearDefaultHint == null) return;

if(isChecked) {
mClearDefaultHint.setVisibility(View.VISIBLE);
} else {
mClearDefaultHint.setVisibility(View.GONE);
}
}
}

可以看到的是继承的是AlertActivity,onCreate方法里主要是初始化布局,还有通过intent获取到device和pendingIntent等,这里盲猜一下,应该是通过//盲猜这个mPendingIntent应该是通过usbManager.requestPermission(device, mPendingIntent);传过来的。

这里我们主要看onDestroy里面做的操作。其实这里已经很清楚的看出来了,如果用户同意授权,就会调用IUsbManager的方法进行跨进程通信,

我们把代码精简一下:

IBinder b = ServiceManager.getService(USB_SERVICE);
IUsbManager service = IUsbManager.Stub.asInterface(b);
Intent intent = new Intent();
intent.putExtra(UsbManager.EXTRA_DEVICE, mDevice);
service.grantDevicePermission(mDevice, mUid);
/*附加信息,可以不要
intent.putExtra(UsbManager.EXTRA_ACCESSORY, mAccessory);
service.grantAccessoryPermission(mAccessory, mUid);
intent.putExtra(UsbManager.EXTRA_PERMISSION_GRANTED, mPermissionGranted);
*/
mPendingIntent.send(this, 0, intent);

rvice.grantDevicePermission(mDevice, mUid);
/*附加信息,可以不要
intent.putExtra(UsbManager.EXTRA_ACCESSORY, mAccessory);
service.grantAccessoryPermission(mAccessory, mUid);
intent.putExtra(UsbManager.EXTRA_PERMISSION_GRANTED, mPermissionGranted);
*/
mPendingIntent.send(this, 0, intent);

标签:USB,EXTRA,private,弹框,intent,串口,mDevice,android
来源: https://blog.csdn.net/m0_64382950/article/details/121605639