I'm trying to write a Windows application to act as a host for Android devices using libusb and the Android Open Accessory protocol.
However, I've been finding that I can't even initiate a handshake with the phone unless it is already in MTP mode (I'm testing against a Pixel XL, which reverts back to "charge mode" every time you unplug it).
I can't find any answers online, so I thought I'd ask here. How can I connect to the phone without having to manually place it in MTP mode each time?
Windows libusb code below:
std::vector<AndroidDevice> devices;
libusb_device *** listptr = new libusb_device **;
libusb_device ** list;
libusb_init(NULL);
int deviceCount = libusb_get_device_list(NULL, listptr);
list = *listptr;
delete listptr;
for (int i = 0; i < deviceCount; i++) {
libusb_device * device = list[i];
libusb_device_descriptor desc;
int result = libusb_get_device_descriptor(device, &desc);
if (desc.idVendor == GOOGLE_VENDOR_ID) {
libusb_device_handle ** handleptr = new libusb_device_handle *;
result = libusb_open(device, handleptr); //Returns "not supported" error unless phone is in MTP
libusb_device_handle * handle = *handleptr;
delete handleptr;
if (!result) {
devices.emplace_back(device, handle);
}
else {
printf("Error opening device: %s\n", errorDescription(result).c_str());
}
}
As far as I remember, it is security feature of Android, so the device won't connect to host until user order it to do so. Selecting the option on Android device change USB gadget driver configuration, allowing for connection.
There is an option in Android developer options that should be able to change this behavior, as described here.