How can I make Android not request for permission each time I reconnect a USB device? I want to make it to remember "Use by default" checkmark for the USB devices so that I don't have to give permission every time to the same device.
I programatically detect when USB devices (android phones) are attached to my host device (android phone) so that I can switch them to AOA mode and use them as accessories. Basically I have two android phones and an OTG cable and I want them to communicate between eachother.
I have a thread which constantly enumerates the attached USB devices:
UsbManager manager = (UsbManager)
context.getSystemService(Context.USB_SERVICE);
while (!m_stopRequested) {
boolean shouldNotify = false;
HashMap<String, UsbDevice> deviceMap = m_usbManager.getDeviceList();
for (Entry<String, UsbDevice> entry : deviceMap) {
UsbDevice device = entry.getValue();
if (m_usbManager.hasPermission(device)) {
int pid = device.getProductId();
if (device.getVendorId() == VID_GOOGLE(0x18D1) && (pid == ACCESSORY_PID(0x2D01) || pid == ACCESSORY_PID_ALT(0x2D00))) {
switchDeviceToAOAMode(device);
}
} else {
m_usbManager.requestPermission(device);
}
}
Thread.sleep(1000);
}
I also have a BroadcastReceiver registered to receive USB_PERMISSION intents:
private final class USBReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
MCSLogger.log(TAG, "Received permission result!");
String action = intent.getAction();
UsbDevice device = (UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
if (ACTION_USB_PERMISSION.equals(action)) {
boolean res = intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false);
MCSLogger.log(TAG, "permission action for dev=" + device + " received " + res);
int pid = device.getProductId();
if (res && device.getVendorId() == VID_GOOGLE(0x18D1) && (pid == ACCESSORY_PID(0x2D01) || pid == ACCESSORY_PID_ALT(0x2D00))) {
connectAccessory()
}
}
}
};
This is how I switch to AOA mode:
private boolean switchDeviceToAOAMode(UsbDeviceConnection connection) {
byte ioBuffer[] = new byte[2];
int devVersion;
int response;
enter code here
response = connection.controlTransfer(0xC0, 51, 0, 0, ioBuffer, 2, 0);
if (response < 0) {
MCSLogger.log(TAG, "Error starting transfer control " + response);
return false;
}
devVersion = ioBuffer[1] << 8 | ioBuffer[0];
// sometimes hangs on the next transfer :( //WIN32 libusb only
// SystemClock.sleep(1000);
byte manufacturer[] = m_manufacturer.getBytes();
response = connection.controlTransfer(0x40, 52, 0, 0, manufacturer, manufacturer.length, 0);
if (response < 0) {
MCSLogger.log(TAG, "Error transfering manufacturer " + response);
return false;
}
byte modelName[] = m_modelName.getBytes();
response = connection.controlTransfer(0x40, 52, 0, 1, modelName, modelName.length, 0);
if (response < 0) {
MCSLogger.log(TAG, "Error transfering modelName " + response);
return false;
}
byte description[] = m_description.getBytes();
response = connection.controlTransfer(0x40, 52, 0, 2, description, description.length, 0);
if (response < 0) {
MCSLogger.log(TAG, "Error transfering description " + response);
return false;
}
byte version[] = m_version.getBytes();
response = connection.controlTransfer(0x40, 52, 0, 3, version, version.length, 0);
if (response < 0) {
MCSLogger.log(TAG, "Error transfering version " + response);
return false;
}
byte uri[] = m_uri.getBytes();
response = connection.controlTransfer(0x40, 52, 0, 4, uri, uri.length, 0);
if (response < 0) {
MCSLogger.log(TAG, "Error transfering uri " + response);
return false;
}
byte serialNumber[] = m_serialNumber.getBytes();
response = connection.controlTransfer(0x40, 52, 0, 5, serialNumber, serialNumber.length, 0);
if (response < 0) {
MCSLogger.log(TAG, "Error transfering serialNumber " + response);
return false;
}
MCSLogger.log(TAG, "Accessory Identification sent " + devVersion);
response = connection.controlTransfer(0x40, 53, 0, 0, null, 0, 0);
if (response < 0) {
MCSLogger.log(TAG, "Error ending transfer control " + response);
return false;
}
return true;
}