2 * This file is part of the sigrok-androidutils project.
4 * Copyright (C) 2014 Marcus Comstedt <marcus@mc.pp.se>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 package org.sigrok.androidutils;
22 import android.content.Context;
23 import android.hardware.usb.UsbDevice;
24 import android.hardware.usb.UsbDeviceConnection;
25 import android.hardware.usb.UsbManager;
26 import android.util.Log;
27 import java.util.HashMap;
29 public final class UsbHelper
31 private static UsbManager manager;
33 public static void setContext(Context ctx)
38 manager = (UsbManager) ctx.getSystemService(Context.USB_SERVICE);
41 private static int open(UsbManager manager, String name, int mode)
43 if (manager == null) {
44 Log.w("UsbHelper", "no manager");
47 HashMap<String,UsbDevice> devlist = manager.getDeviceList();
48 UsbDevice dev = (devlist == null ? null : devlist.get(name));
51 if (!manager.hasPermission(dev))
53 UsbDeviceConnection conn = manager.openDevice(dev);
54 return (conn == null ? -1 : conn.getFileDescriptor());
57 public static int open(String name, int mode)
60 return open(manager, name, mode);
61 } catch (Exception e) {
62 Log.w("UsbHelper", "caught exception " + e);