android.hardware.usb.UsbAccessory Java Examples

The following examples show how to use android.hardware.usb.UsbAccessory. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AndroidOpenAccessoryBridge.java    From android-open-accessory-bridge with Apache License 2.0 7 votes vote down vote up
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]);
    }
}
 
Example #2
Source File: ConnectRcManager.java    From FimiX8-RE with MIT License 6 votes vote down vote up
public synchronized void connectRC(Context mContext) {
    if (!this.isTryConnect) {
        this.isTryConnect = true;
        UsbManager usbManager = (UsbManager) mContext.getSystemService("usb");
        this.mPermissionIntent = PendingIntent.getBroadcast(mContext, 0, new Intent(ACTION_USB_PERMISSION), 0);
        if (usbManager != null) {
            UsbAccessory[] accessories = usbManager.getAccessoryList();
            UsbAccessory accessory = accessories == null ? null : accessories[0];
            if (accessory != null) {
                if (usbManager.hasPermission(accessory)) {
                    CommunicationManager.getCommunicationManager().setAccessory(accessory);
                    CommunicationManager.getCommunicationManager().startConnectThread(mContext, ConnectType.Aoa);
                } else if (!this.isRequestPermission) {
                    usbManager.requestPermission(accessory, this.mPermissionIntent);
                    this.isRequestPermission = true;
                }
            }
        }
        this.isTryConnect = false;
    }
}
 
Example #3
Source File: USBConnectionManager.java    From Android-Bridge-App with MIT License 6 votes vote down vote up
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");
            DJILogger.e(TAG, "NO Permission to USB Accessory");
            //mUsbManager.requestPermission(mAccessory, null);
        }
    } else {
        BridgeApplication.getInstance().getBus().post(new RCConnectionEvent(false));
        Log.d(TAG, "RC DISCONNECTED");
    }
}
 
Example #4
Source File: LEDControlFragment.java    From AndroidDemoProjects with Apache License 2.0 6 votes vote down vote up
@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;
                }
            }
        }
    }
}
 
Example #5
Source File: AOAConnect.java    From FimiX8-RE with MIT License 5 votes vote down vote up
private void openUsbAccessory(UsbAccessory usbAccessory) {
    this.parcelFileDescriptor = this.usbManager.openAccessory(usbAccessory);
    if (this.parcelFileDescriptor != null) {
        FileDescriptor fd = this.parcelFileDescriptor.getFileDescriptor();
        this.inputStream = new FileInputStream(fd);
        this.outputStream = new FileOutputStream(fd);
        try {
            this.outputStream.write(0);
            this.isAoaDeviceConecect = true;
        } catch (IOException e) {
            e.printStackTrace();
            this.isAoaDeviceConecect = false;
        }
    }
}
 
Example #6
Source File: USBTransport.java    From sdl_java_suite with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * 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");
    }
}
 
Example #7
Source File: USBTransport.java    From sdl_java_suite with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * 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) {

    if (accessory == null) {
        handleTransportError("Can't connect to null accessory", null);
    }

    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");
    }
}
 
Example #8
Source File: USBTransport.java    From sdl_java_suite with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * 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(final UsbAccessory accessory) {
    if (accessory == null) {
        return false;
    }

    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;
}
 
Example #9
Source File: USBTransport.java    From sdl_java_suite with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
   * 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);
  }
 
Example #10
Source File: UsbTransferProvider.java    From sdl_java_suite with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@SuppressLint("NewApi")
private ParcelFileDescriptor getFileDescriptor(UsbAccessory accessory, Context context) {
    if (AndroidTools.isUSBCableConnected(context)) {
        try {
            UsbManager manager = (UsbManager) context.getSystemService(Context.USB_SERVICE);

            if (manager != null) {
                return manager.openAccessory(accessory);
            }
        } catch (Exception e) {
        }
    }
    return  null;
}
 
Example #11
Source File: UsbTransferProvider.java    From sdl_java_suite with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public UsbTransferProvider(Context context, ComponentName service, UsbAccessory usbAccessory, UsbTransferCallback callback){
    if(context == null || service == null || usbAccessory == null){
        throw new IllegalStateException("Supplied params are not correct. Context == null? "+ (context==null) + " ComponentName == null? " + (service == null) + " Usb Accessory == null? " + usbAccessory);
    }
    usbPfd = getFileDescriptor(usbAccessory, context);
    if(usbPfd != null && usbPfd.getFileDescriptor() != null && usbPfd.getFileDescriptor().valid()){
        this.context = context;
        this.routerService = service;
        this.callback = callback;
        this.clientMessenger = new Messenger(new ClientHandler(this));

        usbInfoBundle = new Bundle();
        usbInfoBundle.putString(MultiplexUsbTransport.MANUFACTURER, usbAccessory.getManufacturer());
        usbInfoBundle.putString(MultiplexUsbTransport.MODEL, usbAccessory.getModel());
        usbInfoBundle.putString(MultiplexUsbTransport.VERSION, usbAccessory.getVersion());
        usbInfoBundle.putString(MultiplexUsbTransport.URI, usbAccessory.getUri());
        usbInfoBundle.putString(MultiplexUsbTransport.SERIAL, usbAccessory.getSerial());
        usbInfoBundle.putString(MultiplexUsbTransport.DESCRIPTION, usbAccessory.getDescription());
        checkIsConnected();
    }else{
        Log.e(TAG, "Unable to open accessory");
        clientMessenger = null;
        if(callback != null){
            callback.onUsbTransferUpdate(false);
        }
    }

}
 
Example #12
Source File: USBAccessoryAttachmentActivity.java    From sdl_java_suite with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void attemptLegacyUsbConnection(UsbAccessory usbAccessory){
    if(usbAccessory != null) {
        DebugTool.logInfo("Attempting to send USB connection intent using legacy method");
        Intent usbAccessoryAttachedIntent = new Intent(USBTransport.ACTION_USB_ACCESSORY_ATTACHED);
        usbAccessoryAttachedIntent.putExtra(UsbManager.EXTRA_ACCESSORY, usbAccessory);
        usbAccessoryAttachedIntent.putExtra(UsbManager.EXTRA_PERMISSION_GRANTED, permissionGranted);
        AndroidTools.sendExplicitBroadcast(getApplicationContext(), usbAccessoryAttachedIntent, null);
    }else{
        DebugTool.logError("Unable to start legacy USB mode as the accessory was null");
    }
    finish();
}
 
Example #13
Source File: LEDControlFragment.java    From AndroidDemoProjects with Apache License 2.0 5 votes vote down vote up
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 );
    }
}
 
Example #14
Source File: AndroidOpenAccessoryBridge.java    From android-open-accessory-bridge with Apache License 2.0 5 votes vote down vote up
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);
    }
}
 
Example #15
Source File: SerialInterface_FT31xD.java    From PodEmu with GNU General Public License v3.0 5 votes vote down vote up
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;
}
 
Example #16
Source File: ConnectAOAManager.java    From FimiX8-RE with MIT License 5 votes vote down vote up
public void conectAOA() {
    UsbAccessory accessory = null;
    UsbAccessory[] accessories;
    if (this.mConnected) {
        if (this.usbManager != null) {
            accessories = this.usbManager.getAccessoryList();
            if (accessories != null) {
                accessory = accessories[0];
            }
            if (accessory == null) {
                this.mConnected = false;
                this.callback.onConnectionClosed();
                CommunicationManager.getCommunicationManager().stopConnectThread();
            }
        }
    } else if (this.usbManager != null) {
        accessories = this.usbManager.getAccessoryList();
        if (accessories != null) {
            accessory = accessories[0];
        }
        if (accessory == null) {
            return;
        }
        if (this.usbManager.hasPermission(accessory)) {
            CommunicationManager.getCommunicationManager().setAccessory(accessory);
            CommunicationManager.getCommunicationManager().startConnectThread(this.mContext, ConnectType.Aoa);
            this.callback.onConnected();
            this.mConnected = true;
        } else if (!this.isRequestPermission) {
            this.usbManager.requestPermission(accessory, this.mPermissionIntent);
            this.callback.onConnectionEstablished();
            this.isRequestPermission = true;
        }
    }
}
 
Example #17
Source File: AOAConnect.java    From FimiX8-RE with MIT License 5 votes vote down vote up
public AOAConnect(Context mContext, UsbAccessory usbAccessory, ResultListener resultListener, IUSBStatusListener mIAoaConnectListener) {
    this.mContext = mContext;
    this.resultListener = resultListener;
    this.usbManager = (UsbManager) mContext.getSystemService("usb");
    this.mAccessory = usbAccessory;
    this.mIAoaConnectListener = mIAoaConnectListener;
    this.mX8JsonCmdDeque = new X8JsonCmdDeque(this);
    this.isAoaDeviceConecect = false;
    if (usbAccessory != null) {
        openUsbAccessory(this.mAccessory);
    }
}
 
Example #18
Source File: USBTransportConfig.java    From sdl_java_suite with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public UsbAccessory getUsbAccessory () {
	return usbAccessory;
}
 
Example #19
Source File: SerialInterface_FT31xD.java    From PodEmu with GNU General Public License v3.0 4 votes vote down vote up
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;
        }

        PodEmuService.communicateSerialStatusChange();

    }

    return 0;
}
 
Example #20
Source File: UsbConnectThread.java    From FimiX8-RE with MIT License 4 votes vote down vote up
public UsbConnectThread(Context mContext, UsbAccessory accessory, IUSBStatusListener mIAoaConnectListener) {
    this.mContext = mContext;
    this.accessory = accessory;
    this.mIAoaConnectListener = mIAoaConnectListener;
    start();
}
 
Example #21
Source File: CommunicationManager.java    From FimiX8-RE with MIT License 4 votes vote down vote up
public void setAccessory(UsbAccessory accessory) {
    this.accessory = accessory;
}
 
Example #22
Source File: UsbAccessoryAssert.java    From assertj-android with Apache License 2.0 4 votes vote down vote up
public UsbAccessoryAssert(UsbAccessory actual) {
  super(actual, UsbAccessoryAssert.class);
}
 
Example #23
Source File: USBTransportConfig.java    From sdl_java_suite with BSD 3-Clause "New" or "Revised" License 3 votes vote down vote up
/**
 * <b>NOTE: </b> This should no longer be used. See the MultplexTransportConfig and guides to
 * understand how to implement USB multiplexing. This class and method of USB connection will be
 * removed in the next major release. If a router service is available to handle multiplexing of the
 * usb transport it will be used, and this app will connect to whatever router service hosts the USB
 * connection.
 * @param mainActivity context used to start USB transport
 * @param usbAccessory the accessory that was given to this app
 * @param shareConnection enable other sessions on this app to use this USB connection
 * @param queryUsbAcc attempt to query the USB accessory if none is provided
 * @see MultiplexTransportConfig
 */
public USBTransportConfig (Context mainActivity, UsbAccessory usbAccessory, boolean shareConnection, boolean queryUsbAcc) {
	this.mainActivity = mainActivity;
	this.queryUsbAcc = queryUsbAcc;
	this.usbAccessory = usbAccessory;
	super.shareConnection = shareConnection;
}
 
Example #24
Source File: USBTransportConfig.java    From sdl_java_suite with BSD 3-Clause "New" or "Revised" License 2 votes vote down vote up
/**
 * <b>NOTE: </b> This should no longer be used. See the MultplexTransportConfig and guides to
 * understand how to implement USB multiplexing. This class and method of USB connection will be
 * removed in the next major release. If a router service is available to handle multiplexing of the
 * usb transport it will be used, and this app will connect to whatever router service hosts the USB
 * connection.
 * @param mainActivity context used to start USB transport
 * @param usbAccessory the accessory that was given to this app
 * @see MultiplexTransportConfig
 */
public USBTransportConfig (Context mainActivity, UsbAccessory usbAccessory) {
	this.mainActivity = mainActivity;
	this.usbAccessory = usbAccessory;
}
 
Example #25
Source File: USBTransportConfig.java    From sdl_java_suite with BSD 3-Clause "New" or "Revised" License votes vote down vote up
public void setUsbAccessory (UsbAccessory value) { usbAccessory = value; }