public void setup(Context context) { mManager = (UsbManager) context.getSystemService(Context.USB_SERVICE); UsbAccessory[] accessoryList = mManager.getAccessoryList(); PendingIntent mPermissionIntent = PendingIntent.getBroadcast(context, 0, new Intent(ACTION_USB_PERMISSION), 0); IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION); context.registerReceiver(mUsbReceiver, filter); mManager.requestPermission(accessoryList[0], mPermissionIntent); if (accessoryList[0] != null) { mAccessory = accessoryList[0]; if (mManager.hasPermission(mAccessory)) { openAccessory(mAccessory); } } }
public void checkForDJIAccessory() { mUsbManager = (UsbManager) BridgeApplication.getInstance().getSystemService(Context.USB_SERVICE); UsbAccessory[] accessoryList = mUsbManager.getAccessoryList(); if (accessoryList != null && accessoryList.length > 0 && !TextUtils.isEmpty(accessoryList[0].getManufacturer()) && accessoryList[0].getManufacturer().equals("DJI")) { BridgeApplication.getInstance().getBus().post(new RCConnectionEvent(true)); //Check permission mAccessory = accessoryList[0]; if (mUsbManager.hasPermission(mAccessory)) { Log.d(TAG, "RC CONNECTED"); } else { Log.d(TAG, "NO Permission to USB Accessory"); DJIRemoteLogger.e(TAG, "NO Permission to USB Accessory"); //mUsbManager.requestPermission(mAccessory, null); } } else { BridgeApplication.getInstance().getBus().post(new RCConnectionEvent(false)); Log.d(TAG, "RC DISCONNECTED"); } }
public FREObject call(FREContext ctx, FREObject[] args){ UsbContext usbCtx = (UsbContext) ctx; ArrayList<UsbAccessory> accessories = usbCtx.usbAccessories(); Iterator<UsbAccessory> accessoryIterator = accessories.iterator(); JSONArray jsonArray = new JSONArray(); while (accessoryIterator.hasNext()) { UsbAccessory accessory = accessoryIterator.next(); JSONObject jsonObject = new JSONObject(); UsbEvent.addUsbToJson(accessory, jsonObject); jsonArray.put(jsonObject); } try{ return FREObject.newObject(jsonArray.toString()); }catch(FREWrongThreadException e){ } return null; }
@Override public void onResume() { super.onResume(); if( mOutputStream != null ) { return; } UsbAccessory[] accessories = mUsbManager.getAccessoryList(); UsbAccessory accessory = ( accessories == null ? null : accessories[0] ); if( accessory != null ) { if( mUsbManager.hasPermission( accessory ) ) { openAccessory( accessory ); } else { synchronized( mUsbReceiver ) { if( !mPermissionRequestPending ) { mUsbManager.requestPermission( accessory, mPermissionIntent ); mPermissionRequestPending = true; } } } } }
private void attachUsbReceiver() { // Broadcast Receiver mUsbReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (action.equals(UsbManager.ACTION_USB_ACCESSORY_DETACHED)) { UsbAccessory usbAccessory = (UsbAccessory) intent.getParcelableExtra(UsbManager.EXTRA_ACCESSORY); if (usbAccessory != null && usbAccessory.equals(mUsbAccessory)) { closeAccessory(); } } } }; }
private void detectAccessory() { while (!mIsAttached) { if (mIsShutdown) { mHandler.sendEmptyMessage(STOP_THREAD); return; } try { Thread.sleep(CONNECT_COOLDOWN_MS); } catch (InterruptedException exception) { // pass } final UsbAccessory[] accessoryList = mUsbManager.getAccessoryList(); if (accessoryList == null || accessoryList.length == 0) { continue; } if (accessoryList.length > 1) { Log.w(TAG, "Multiple accessories attached!? Using first one..."); } maybeAttachAccessory(accessoryList[0]); } }
public USBControl(Context main, Handler ui) { super("USBControlSender"); UIHandler = ui; context = main; mManager = (UsbManager) context.getSystemService(Context.USB_SERVICE); UsbAccessory[] accessoryList = mManager.getAccessoryList(); PendingIntent mPermissionIntent = PendingIntent.getBroadcast(context, 0, new Intent(ACTION_USB_PERMISSION), 0); IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION); context.registerReceiver(mUsbReceiver, filter); UsbAccessory mAccessory = (accessoryList == null ? null : accessoryList[0]); if (mAccessory != null) { while (!mManager.hasPermission(mAccessory)) { mManager.requestPermission(mAccessory, mPermissionIntent); } openAccessory(mAccessory); } }
/** * Attempts to connect to the specified accessory. * * If the permission is already granted, opens the accessory. Otherwise, * requests permission to use it. * * @param accessory Accessory to connect to */ private void connectToAccessory(UsbAccessory accessory) { final State state = getState(); switch (state) { case LISTENING: UsbManager usbManager = getUsbManager(); if (usbManager.hasPermission(accessory)) { logI("Already have permission to use " + accessory); openAccessory(accessory); } else { logI("Requesting permission to use " + accessory); PendingIntent permissionIntent = PendingIntent .getBroadcast(getContext(), 0, new Intent(ACTION_USB_PERMISSION), 0); usbManager.requestPermission(accessory, permissionIntent); } break; default: logW("connectToAccessory() called from state " + state + "; doing nothing"); } }
public String[] getList(Context context) { mManager = (UsbManager) context.getSystemService(Context.USB_SERVICE); UsbAccessory[] accessoryList = mManager.getAccessoryList(); String[] accessoryLists = new String[1]; for (int i = 0; i < accessoryLists.length; i++) { accessoryLists[i] = accessoryList[i].getModel() + " v" + accessoryList[i].getVersion() + " by " + accessoryList[i].getManufacturer(); } return accessoryLists; }
private void openAccessory(UsbAccessory accessory) { mAccessory = accessory; mFileDescriptor = mManager.openAccessory(accessory); if (mFileDescriptor != null) { FileDescriptor fd = mFileDescriptor.getFileDescriptor(); mInputStream = new FileInputStream(fd); mOutputStream = new FileOutputStream(fd); } isOpen = true; }
/** * Connect to a device. * @return Status of the UsbConnection. */ public Status connectToDevice(){ if(status != Status.PERMISSION_REQUESTED && status != Status.CONNECTED){ UsbAccessory[] accessoryList = usbManager.getAccessoryList(); if(accessoryList == null) { setStatus(Status.DISCONNECTED); return status; } accessory = accessoryList[0]; usbManager.requestPermission(accessory, permissionIntent); setStatus(Status.PERMISSION_REQUESTED); } return status; }
public static void addUsbToJson(UsbAccessory usbAccessory, JSONObject json){ try { json.put("serial", usbAccessory.getSerial()); json.put("uri", usbAccessory.getUri()); json.put("hashCode", usbAccessory.hashCode()); }catch(JSONException e){ } }
public ArrayList<UsbAccessory> usbAccessories(){ if(_usbAccessories==null){ _usbAccessories = new ArrayList<UsbAccessory>(); UsbManager usbManager = getUsbManager(); UsbAccessory accessories[] = usbManager.getAccessoryList(); if(accessories==null){ return _usbAccessories; } for(Integer i=0;i<accessories.length;i++){ _usbAccessories.add(accessories[i]); } } return _usbAccessories; }
public static SubjectFactory<UsbAccessorySubject, UsbAccessory> type() { return new SubjectFactory<UsbAccessorySubject, UsbAccessory>() { @Override public UsbAccessorySubject getSubject(FailureStrategy fs, UsbAccessory that) { return new UsbAccessorySubject(fs, that); } }; }
public boolean OpenAccessory(UsbAccessory accessory) { fileDescriptor = usbmanager.openAccessory(accessory); if(fileDescriptor != null) { PodEmuLog.debug("FT31xD: file descriptor successfully opened"); usbAccessory = accessory; FileDescriptor fd = fileDescriptor.getFileDescriptor(); inputStream = new FileInputStream(fd); outputStream = new FileOutputStream(fd); /* check if any of them are null */ if(inputStream == null || outputStream==null) { PodEmuLog.error("FT31xD: sth went wrong. In or Out descriptor is null!"); return false; } } else { PodEmuLog.error("FT31xD: fileDescriptor is null!"); return false; } return true; }
private void openAccessory(UsbAccessory accessory) { if( accessory == null ) return; mFileDescriptor = mUsbManager.openAccessory( accessory ); if (mFileDescriptor != null ) { mUsbAccessory = accessory; FileDescriptor fileDescriptor = mFileDescriptor.getFileDescriptor(); mOutputStream = new FileOutputStream( fileDescriptor ); } }
protected void openAccessory(UsbAccessory usbAccessory) { mParcelFileDescriptor = mUsbManager.openAccessory(usbAccessory); if (mParcelFileDescriptor != null) { mUsbAccessory = usbAccessory; FileDescriptor fileDescriptor = mParcelFileDescriptor.getFileDescriptor(); if (fileDescriptor != null) { mFileInputStream = new FileInputStream(fileDescriptor); mFileOutputStream = new FileOutputStream(fileDescriptor); } } }
@Override public void open() { if (mFileInputStream == null || mFileOutputStream == null) { UsbAccessory[] usbAccessoryList = mUsbManager.getAccessoryList(); if (usbAccessoryList != null && usbAccessoryList.length > 0) { openAccessory(usbAccessoryList[0]); } } }
@Override protected void setUp() throws Exception { super.setUp(); // Workaround to solve dexmaker bug. See https://code.google.com/p/dexmaker/issues/detail?id=2 System.setProperty("dexmaker.dexcache", getInstrumentation().getTargetContext().getCacheDir().getPath()); // Mock read/write streams so serial communication with accessory is mocked with a temporary file File mockMemoryReadWrite = File.createTempFile("read_write_memory_mock", null, getInstrumentation().getTargetContext().getCacheDir()); mockFileOutputStream = new FileOutputStream(mockMemoryReadWrite); mockFileInputStream = new FileInputStream(mockMemoryReadWrite); // Launch unit test activity Intent mLaunchIntent = new Intent(getInstrumentation().getTargetContext(), MockActivity.class); startActivity(mLaunchIntent, null, null); mockActivity = getActivity(); // Spy objects UsbManager usbManager = (UsbManager) mockActivity.getSystemService(Context.USB_SERVICE); UsbManager spyUsbManager = spy(usbManager); mockAccessory = mock(UsbAccessory.class); mockFileDescriptor = mock(ParcelFileDescriptor.class); doReturn(new UsbAccessory[]{mockAccessory}).when(spyUsbManager).getAccessoryList(); doReturn(mockFileDescriptor).when(spyUsbManager).openAccessory(mockAccessory); adkManager = new AdkManager(spyUsbManager); adkManager.open(); // Mock Input stream with in memory write adkManager.setFileInputStream(mockFileInputStream); adkManager.setFileOutputStream(mockFileOutputStream); }
private void maybeAttachAccessory(final UsbAccessory accessory) { final ParcelFileDescriptor parcelFileDescriptor = mUsbManager.openAccessory(accessory); if (parcelFileDescriptor != null) { final FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor(); mIsAttached = true; mOutputStream = new FileOutputStream(fileDescriptor); mInputStream = new FileInputStream(fileDescriptor); mParcelFileDescriptor = parcelFileDescriptor; mHandler.sendEmptyMessage(MAYBE_READ); } }
private void openAccessory(UsbAccessory accessory) { mAccessory = accessory; mFileDescriptor = mManager.openAccessory(accessory); if (mFileDescriptor != null) { FileDescriptor fd = mFileDescriptor.getFileDescriptor(); input = new FileInputStream(fd); } this.start(); onConnected(); }
/** * Looks for an already connected compatible accessory and connect to it. */ private void initializeAccessory() { UsbAccessory acc = mConfig.getUsbAccessory(); if (!mConfig.getQueryUsbAcc() && acc == null){ logI("Query for accessory is disabled and accessory in config was null."); return; } logI("Looking for connected accessories"); if( acc == null || !isAccessorySupported(acc)){ //Check to see if our config included an accessory and that it is supported. If not, see if there are any other accessories connected. UsbManager usbManager = getUsbManager(); UsbAccessory[] accessories = usbManager.getAccessoryList(); if (accessories != null) { logD("Found total " + accessories.length + " accessories"); for (UsbAccessory accessory : accessories) { if (isAccessorySupported(accessory)) { acc = accessory; break; } } } else { logI("No connected accessories found"); return; } } connectToAccessory(acc); }
/** * Checks if the specified connected USB accessory is what we expect. * * @param accessory Accessory to check * @return true if the accessory is right */ public static boolean isAccessorySupported(UsbAccessory accessory) { boolean manufacturerMatches = ACCESSORY_MANUFACTURER.equals(accessory.getManufacturer()); boolean modelMatches = ACCESSORY_MODEL.equals(accessory.getModel()); boolean versionMatches = ACCESSORY_VERSION.equals(accessory.getVersion()); return manufacturerMatches && modelMatches && versionMatches; }
/** * Opens a connection to the accessory. * * When this function is called, the permission to use it must have already * been granted. * * @param accessory Accessory to open connection to */ private void openAccessory(UsbAccessory accessory) { final State state = getState(); switch (state) { case LISTENING: synchronized (this) { logI("Opening accessory " + accessory); mAccessory = accessory; mReaderThread = new Thread(new USBTransportReader()); mReaderThread.setDaemon(true); mReaderThread .setName(USBTransportReader.class.getSimpleName()); mReaderThread.start(); // Initialize the SiphonServer if (SiphonServer.getSiphonEnabledStatus()) { SiphonServer.init(); } } break; default: logW("openAccessory() called from state " + state + "; doing nothing"); } }
public void set_usbEnd(UsbAccessory end){ usbAccessory = end; usbDevice = null; }
public UsbAccessory getAccessoryByIndex(Integer index){ return usbAccessories().get(index); }
protected UsbAccessorySubject(FailureStrategy failureStrategy, UsbAccessory subject) { super(failureStrategy, subject); }
public int ResumeAccessory() { if (inputStream != null && outputStream != null) { return 1; } UsbAccessory[] accessories = usbmanager.getAccessoryList(); if(accessories != null) { PodEmuLog.debug("FT31xD: Accessory Attached"); } else { accessory_attached = false; return 2; } UsbAccessory accessory = (accessories.length==0 ? null : accessories[0]); if (accessory != null) { PodEmuLog.debug("FT31xD: Accessory info: " + accessory.toString()); if( !accessory.getManufacturer().contains(ManufacturerString)) { PodEmuLog.debug("FT31xD: Manufacturer is not matched! Found manufacturer: " + accessory.getManufacturer()); return 1; } if(!deviceList.containsKey(accessory.getModel())) { PodEmuLog.debug("FT31xD: Model is not matched. Found model: " + accessory.getModel()); return 1; } if( !accessory.getVersion().contains(VersionString)) { PodEmuLog.debug("FT31xD: Version is not matched. Version found: " + accessory.getVersion()); return 1; } PodEmuLog.debug("FT31xD: Manufacturer, Model & Version are matched!"); // we don't need to request permission as we are using intent filter // but we are checking if we have them anyway if (usbmanager.hasPermission(accessory)) { if(OpenAccessory(accessory)) { accessory_attached = true; } else { accessory_attached = false; return 3; } } else { PodEmuLog.error("FT31xD: no permission for accessory"); accessory_attached = false; } } return 0; }
public USBTransportConfig (Context mainActivity, UsbAccessory usbAccessory) { this.mainActivity = mainActivity; this.usbAccessory = usbAccessory; }
public USBTransportConfig (Context mainActivity, UsbAccessory usbAccessory, boolean shareConnection, boolean queryUsbAcc) { this.mainActivity = mainActivity; this.queryUsbAcc = queryUsbAcc; this.usbAccessory = usbAccessory; super.shareConnection = shareConnection; }
public UsbAccessory getUsbAccessory () { return usbAccessory; }
public UsbAccessoryAssert(UsbAccessory actual) { super(actual, UsbAccessoryAssert.class); }
public void setUsbAccessory (UsbAccessory value) { usbAccessory = value; }