最近在做Android设备与外设通信,需要用的串口通信。如果使用android-serialport-api发现Android设备需要获取root权限,否则会报权限不足的异常。所以决定使用Android提供的USB开发。
使用方法网上一搜有一堆。不做具体的描述,但是关于波特率的设置却很少,自己就碰到了这个问题。各种百度谷歌发现一个可以解决的办法。
private boolean configUsb(int paramInt) { byte[] arrayOfByte = new byte[8]; mDeviceConnection.controlTransfer(192, 95, 0, 0, arrayOfByte, 8, 1000); mDeviceConnection.controlTransfer(64, 161, 0, 0, null, 0, 1000); long l1 = 1532620800 / paramInt; for (int i = 3; ; i--) { if ((l1 <= 65520L) || (i <= 0)) { long l2 = 65536L - l1; int j = (short) (int) (0xFF00 & l2 | i); int k = (short) (int) (0xFF & l2); mDeviceConnection.controlTransfer(64, 154, 4882, j, null, 0, 1000); mDeviceConnection.controlTransfer(64, 154, 3884, k, null, 0, 1000); mDeviceConnection.controlTransfer(192, 149, 9496, 0, arrayOfByte, 8, 1000); mDeviceConnection.controlTransfer(64, 154, 1304, 80, null, 0, 1000); mDeviceConnection.controlTransfer(64, 161, 20511, 55562, null, 0, 1000); mDeviceConnection.controlTransfer(64, 154, 4882, j, null, 0, 1000); mDeviceConnection.controlTransfer(64, 154, 3884, k, null, 0, 1000); mDeviceConnection.controlTransfer(64, 164, 0, 0, null, 0, 1000); return true; } l1 >>= 3; } }在获取到mDeviceConnection对象之后调用configUSB(paramInt);参数为需要设置的波特率