use of android.hardware.usb.UsbDeviceConnection in project android_frameworks_base by ResurrectionRemix.
the class SinkActivity method connect.
private void connect(UsbDevice device) {
if (mConnected) {
disconnect();
}
// Check whether we have permission to access the device.
if (!mUsbManager.hasPermission(device)) {
mLogger.log("Prompting the user for access to the device.");
Intent intent = new Intent(ACTION_USB_DEVICE_PERMISSION);
intent.setPackage(getPackageName());
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
mUsbManager.requestPermission(device, pendingIntent);
return;
}
// Claim the device.
UsbDeviceConnection conn = mUsbManager.openDevice(device);
if (conn == null) {
mLogger.logError("Could not obtain device connection.");
return;
}
UsbInterface iface = device.getInterface(0);
UsbEndpoint controlEndpoint = iface.getEndpoint(0);
if (!conn.claimInterface(iface, true)) {
mLogger.logError("Could not claim interface.");
return;
}
try {
// If already in accessory mode, then connect to the device.
if (isAccessory(device)) {
mLogger.log("Connecting to accessory...");
int protocolVersion = getProtocol(conn);
if (protocolVersion < 1) {
mLogger.logError("Device does not support accessory protocol.");
return;
}
mLogger.log("Protocol version: " + protocolVersion);
// Setup bulk endpoints.
UsbEndpoint bulkIn = null;
UsbEndpoint bulkOut = null;
for (int i = 0; i < iface.getEndpointCount(); i++) {
UsbEndpoint ep = iface.getEndpoint(i);
if (ep.getDirection() == UsbConstants.USB_DIR_IN) {
if (bulkIn == null) {
mLogger.log(String.format("Bulk IN endpoint: %d", i));
bulkIn = ep;
}
} else {
if (bulkOut == null) {
mLogger.log(String.format("Bulk OUT endpoint: %d", i));
bulkOut = ep;
}
}
}
if (bulkIn == null || bulkOut == null) {
mLogger.logError("Unable to find bulk endpoints");
return;
}
mLogger.log("Connected");
mConnected = true;
mDevice = device;
mProtocolVersion = protocolVersion;
mAccessoryInterface = iface;
mAccessoryConnection = conn;
mControlEndpoint = controlEndpoint;
mTransport = new UsbAccessoryBulkTransport(mLogger, conn, bulkIn, bulkOut);
if (mProtocolVersion >= 2) {
registerHid();
}
startServices();
mTransport.startReading();
return;
}
// Do accessory negotiation.
mLogger.log("Attempting to switch device to accessory mode...");
// Send get protocol.
int protocolVersion = getProtocol(conn);
if (protocolVersion < 1) {
mLogger.logError("Device does not support accessory protocol.");
return;
}
mLogger.log("Protocol version: " + protocolVersion);
// Send identifying strings.
sendString(conn, UsbAccessoryConstants.ACCESSORY_STRING_MANUFACTURER, MANUFACTURER);
sendString(conn, UsbAccessoryConstants.ACCESSORY_STRING_MODEL, MODEL);
sendString(conn, UsbAccessoryConstants.ACCESSORY_STRING_DESCRIPTION, DESCRIPTION);
sendString(conn, UsbAccessoryConstants.ACCESSORY_STRING_VERSION, VERSION);
sendString(conn, UsbAccessoryConstants.ACCESSORY_STRING_URI, URI);
sendString(conn, UsbAccessoryConstants.ACCESSORY_STRING_SERIAL, SERIAL);
// Send start.
// The device should re-enumerate as an accessory.
mLogger.log("Sending accessory start request.");
int len = conn.controlTransfer(UsbConstants.USB_DIR_OUT | UsbConstants.USB_TYPE_VENDOR, UsbAccessoryConstants.ACCESSORY_START, 0, 0, null, 0, 10000);
if (len != 0) {
mLogger.logError("Device refused to switch to accessory mode.");
} else {
mLogger.log("Waiting for device to re-enumerate...");
}
} finally {
if (!mConnected) {
conn.releaseInterface(iface);
}
}
}
use of android.hardware.usb.UsbDeviceConnection in project android_frameworks_base by ResurrectionRemix.
the class TestUtil method findMtpDevice.
private static UsbDevice findMtpDevice(UsbManager usbManager, MtpManager manager) throws IOException {
final HashMap<String, UsbDevice> devices = usbManager.getDeviceList();
if (devices.size() == 0) {
throw new IOException("Device not found.");
}
final UsbDevice device = devices.values().iterator().next();
// Tries to get ownership of the device in case that another application use it.
if (usbManager.hasPermission(device)) {
final UsbDeviceConnection connection = usbManager.openDevice(device);
for (int i = 0; i < device.getInterfaceCount(); i++) {
// Since the test runs real environment, we need to call claim interface with
// force = true to rob interfaces from other applications.
connection.claimInterface(device.getInterface(i), true);
connection.releaseInterface(device.getInterface(i));
}
connection.close();
}
manager.openDevice(device.getDeviceId());
return device;
}
use of android.hardware.usb.UsbDeviceConnection in project android_frameworks_base by DirtyUnicorns.
the class SinkActivity method connect.
private void connect(UsbDevice device) {
if (mConnected) {
disconnect();
}
// Check whether we have permission to access the device.
if (!mUsbManager.hasPermission(device)) {
mLogger.log("Prompting the user for access to the device.");
Intent intent = new Intent(ACTION_USB_DEVICE_PERMISSION);
intent.setPackage(getPackageName());
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
mUsbManager.requestPermission(device, pendingIntent);
return;
}
// Claim the device.
UsbDeviceConnection conn = mUsbManager.openDevice(device);
if (conn == null) {
mLogger.logError("Could not obtain device connection.");
return;
}
UsbInterface iface = device.getInterface(0);
UsbEndpoint controlEndpoint = iface.getEndpoint(0);
if (!conn.claimInterface(iface, true)) {
mLogger.logError("Could not claim interface.");
return;
}
try {
// If already in accessory mode, then connect to the device.
if (isAccessory(device)) {
mLogger.log("Connecting to accessory...");
int protocolVersion = getProtocol(conn);
if (protocolVersion < 1) {
mLogger.logError("Device does not support accessory protocol.");
return;
}
mLogger.log("Protocol version: " + protocolVersion);
// Setup bulk endpoints.
UsbEndpoint bulkIn = null;
UsbEndpoint bulkOut = null;
for (int i = 0; i < iface.getEndpointCount(); i++) {
UsbEndpoint ep = iface.getEndpoint(i);
if (ep.getDirection() == UsbConstants.USB_DIR_IN) {
if (bulkIn == null) {
mLogger.log(String.format("Bulk IN endpoint: %d", i));
bulkIn = ep;
}
} else {
if (bulkOut == null) {
mLogger.log(String.format("Bulk OUT endpoint: %d", i));
bulkOut = ep;
}
}
}
if (bulkIn == null || bulkOut == null) {
mLogger.logError("Unable to find bulk endpoints");
return;
}
mLogger.log("Connected");
mConnected = true;
mDevice = device;
mProtocolVersion = protocolVersion;
mAccessoryInterface = iface;
mAccessoryConnection = conn;
mControlEndpoint = controlEndpoint;
mTransport = new UsbAccessoryBulkTransport(mLogger, conn, bulkIn, bulkOut);
if (mProtocolVersion >= 2) {
registerHid();
}
startServices();
mTransport.startReading();
return;
}
// Do accessory negotiation.
mLogger.log("Attempting to switch device to accessory mode...");
// Send get protocol.
int protocolVersion = getProtocol(conn);
if (protocolVersion < 1) {
mLogger.logError("Device does not support accessory protocol.");
return;
}
mLogger.log("Protocol version: " + protocolVersion);
// Send identifying strings.
sendString(conn, UsbAccessoryConstants.ACCESSORY_STRING_MANUFACTURER, MANUFACTURER);
sendString(conn, UsbAccessoryConstants.ACCESSORY_STRING_MODEL, MODEL);
sendString(conn, UsbAccessoryConstants.ACCESSORY_STRING_DESCRIPTION, DESCRIPTION);
sendString(conn, UsbAccessoryConstants.ACCESSORY_STRING_VERSION, VERSION);
sendString(conn, UsbAccessoryConstants.ACCESSORY_STRING_URI, URI);
sendString(conn, UsbAccessoryConstants.ACCESSORY_STRING_SERIAL, SERIAL);
// Send start.
// The device should re-enumerate as an accessory.
mLogger.log("Sending accessory start request.");
int len = conn.controlTransfer(UsbConstants.USB_DIR_OUT | UsbConstants.USB_TYPE_VENDOR, UsbAccessoryConstants.ACCESSORY_START, 0, 0, null, 0, 10000);
if (len != 0) {
mLogger.logError("Device refused to switch to accessory mode.");
} else {
mLogger.log("Waiting for device to re-enumerate...");
}
} finally {
if (!mConnected) {
conn.releaseInterface(iface);
}
}
}
use of android.hardware.usb.UsbDeviceConnection in project android_frameworks_base by DirtyUnicorns.
the class MtpManager method openDevice.
synchronized MtpDeviceRecord openDevice(int deviceId) throws IOException {
UsbDevice rawDevice = null;
for (final UsbDevice candidate : mManager.getDeviceList().values()) {
if (candidate.getDeviceId() == deviceId) {
rawDevice = candidate;
break;
}
}
ensureNotNull(rawDevice, "Not found USB device: " + deviceId);
if (!mManager.hasPermission(rawDevice)) {
mManager.grantPermission(rawDevice);
if (!mManager.hasPermission(rawDevice)) {
throw new IOException("Failed to grant a device permission.");
}
}
final MtpDevice device = new MtpDevice(rawDevice);
final UsbDeviceConnection connection = ensureNotNull(mManager.openDevice(rawDevice), "Failed to open a USB connection.");
if (!device.open(connection)) {
// We cannot open connection when another application use the device.
throw new BusyDeviceException();
}
// Handle devices that fail to obtain storages just after opening a MTP session.
final int[] storageIds = ensureNotNull(device.getStorageIds(), "Not found MTP storages in the device.");
mDevices.put(deviceId, device);
return createDeviceRecord(rawDevice);
}
use of android.hardware.usb.UsbDeviceConnection in project android_frameworks_base by DirtyUnicorns.
the class TestUtil method findMtpDevice.
private static UsbDevice findMtpDevice(UsbManager usbManager, MtpManager manager) throws IOException {
final HashMap<String, UsbDevice> devices = usbManager.getDeviceList();
if (devices.size() == 0) {
throw new IOException("Device not found.");
}
final UsbDevice device = devices.values().iterator().next();
// Tries to get ownership of the device in case that another application use it.
if (usbManager.hasPermission(device)) {
final UsbDeviceConnection connection = usbManager.openDevice(device);
for (int i = 0; i < device.getInterfaceCount(); i++) {
// Since the test runs real environment, we need to call claim interface with
// force = true to rob interfaces from other applications.
connection.claimInterface(device.getInterface(i), true);
connection.releaseInterface(device.getInterface(i));
}
connection.close();
}
manager.openDevice(device.getDeviceId());
return device;
}
Aggregations