最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【未解决】android中去new生成一个伪造的UsbDevice供模拟调试使用

Android crifan 2315浏览 0评论

【问题】

android项目中,由于为了支持debug模式。

而要去对于UsbDevice去新生成一个模拟的,伪造的,供后续调试使用。

【解决过程】

1.结果去使用:

		UsbDevice supportedUsbSerialDevice = null;
		UsbDevice usbDevice = new UsbDevice();
		supportedUsbSerialDevice = usbDevice;

却提示出错:

The constructor UsbDevice() is not visible

如图:

The constructor UsbDevice is not visible

很明显,此处不给直接new一个UsbDevice。

2.搜:

android new UsbDevice

找到:

http://www.oschina.net/code/explore/android-4.0.1/core/java/android/hardware/usb/UsbDevice.java

中的:

public static final Parcelable.Creator<UsbDevice> CREATOR =
    new Parcelable.Creator<UsbDevice>() {
    public UsbDevice createFromParcel(Parcel in) {
        String name = in.readString();
        int vendorId = in.readInt();
        int productId = in.readInt();
        int clasz = in.readInt();
        int subClass = in.readInt();
        int protocol = in.readInt();
        Parcelable[] interfaces = in.readParcelableArray(UsbInterface.class.getClassLoader());
        return new UsbDevice(name, vendorId, productId, clasz, subClass, protocol, interfaces);
    }

    public UsbDevice[] newArray(int size) {
        return new UsbDevice[size];
    }
};

但是由于不提供接口,所以没法写类似的new的代码。

3.参考:

Fake serial number of USB device

无解。

4.写成:

		//UsbDevice usbDevice = new UsbDevice();
		Parcel usbDevArg0 = new Parcel();
		UsbDevice usbDevice = UsbDevice.CREATOR.createFromParcel(usbDevArg0);
		supportedUsbSerialDevice = usbDevice;

但是接下来不知道如何给Parcel传递何种参数,才能fake出一个UsbDevice。

5.再去看:

http://www.oschina.net/code/explore/android-4.0.1/core/java/android/hardware/usb/UsbDevice.java

发现对应的UsbDevice是有new的,所以去试试:

		UsbDevice usbDevice = new UsbDevice(
				fakeUsbDevName,
				fakeUsbDevVendorId,
				fakeUsbDevProductId,
				fakeUsbDevClass,
				fakeUsbDevSubclass,
				fakeUsbDevProtocol,
				fakeUsbInterfaces);

结果还是不行:

The constructor UsbDevice(String, int, int, int, int, int, Parcelable[]) is undefined

 

6.后来折腾半天,去试试:

		UsbDevice usbDevice;
		try {
			usbDevice = UsbDevice.class.newInstance();
			
			supportedUsbSerialDevice = usbDevice;
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		

结果会出现InstantiationException。

 

【总结】

结果:

还是无法生成一个可用的,可被系统的USB相关函数操作的UsbDevice。。。

暂时放弃折腾了。。。

转载请注明:在路上 » 【未解决】android中去new生成一个伪造的UsbDevice供模拟调试使用

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
83 queries in 0.166 seconds, using 22.11MB memory