Java 类android.hardware.usb.UsbAccessory 实例源码

项目:phonk    文件:AdkPort.java   
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);
            }
        }

    }
项目:Android-Bridge-App    文件:USBConnectionManager.java   
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");
    }
}
项目:UsbExtension    文件:GetAccessoriesFunction.java   
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;
}
项目:AndroidDemoProjects    文件:LEDControlFragment.java   
@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;
                }
            }
        }
    }
}
项目:adk-toolkit    文件:AdkManager.java   
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();
                }
            }
        }
    };
}
项目:android-open-accessory-bridge    文件:AndroidOpenAccessoryBridge.java   
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]);
    }
}
项目:bard-droid    文件:USBControl.java   
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);

    }

}
项目:sdl_android    文件:USBTransport.java   
/**
 * 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");
    }
}
项目:phonk    文件:AdkPort.java   
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;
    }
项目:phonk    文件:AdkPort.java   
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;
}
项目:Android-Raspberry-Pi-Keyboard    文件:UsbConnection.java   
/**
 * 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;
}
项目:UsbExtension    文件:UsbEvent.java   
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){
    }
}
项目:UsbExtension    文件:UsbContext.java   
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;
}
项目:truth-android    文件:UsbAccessorySubject.java   
public static SubjectFactory<UsbAccessorySubject, UsbAccessory> type() {
  return new SubjectFactory<UsbAccessorySubject, UsbAccessory>() {
    @Override
    public UsbAccessorySubject getSubject(FailureStrategy fs, UsbAccessory that) {
      return new UsbAccessorySubject(fs, that);
    }
  };
}
项目:PodEmu    文件:SerialInterface_FT31xD.java   
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;
}
项目:AndroidDemoProjects    文件:LEDControlFragment.java   
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 );
    }
}
项目:adk-toolkit    文件:AdkManager.java   
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);
        }
    }
}
项目:adk-toolkit    文件:AdkManager.java   
@Override
public void open() {
    if (mFileInputStream == null || mFileOutputStream == null) {
        UsbAccessory[] usbAccessoryList = mUsbManager.getAccessoryList();
        if (usbAccessoryList != null && usbAccessoryList.length > 0) {
            openAccessory(usbAccessoryList[0]);
        }
    }
}
项目:adk-toolkit    文件:TestAdkManager.java   
@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);
}
项目:android-open-accessory-bridge    文件:AndroidOpenAccessoryBridge.java   
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);
    }
}
项目:bard-droid    文件:USBControl.java   
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();
}
项目:sdl_android    文件:USBTransport.java   
/**
   * 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);
  }
项目:sdl_android    文件:USBTransport.java   
/**
 * 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;
}
项目:sdl_android    文件:USBTransport.java   
/**
 * 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");
    }
}
项目:UsbExtension    文件:UsbEvent.java   
public void set_usbEnd(UsbAccessory end){
    usbAccessory = end;
    usbDevice = null;
}
项目:UsbExtension    文件:UsbContext.java   
public UsbAccessory getAccessoryByIndex(Integer index){
    return usbAccessories().get(index);
}
项目:truth-android    文件:UsbAccessorySubject.java   
protected UsbAccessorySubject(FailureStrategy failureStrategy, UsbAccessory subject) {
  super(failureStrategy, subject);
}
项目:PodEmu    文件:SerialInterface_FT31xD.java   
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;
}
项目:sdl_android    文件:USBTransportConfig.java   
public USBTransportConfig (Context mainActivity, UsbAccessory usbAccessory) {
    this.mainActivity = mainActivity;
    this.usbAccessory = usbAccessory;
}
项目:sdl_android    文件:USBTransportConfig.java   
public USBTransportConfig (Context mainActivity, UsbAccessory usbAccessory, boolean shareConnection, boolean queryUsbAcc) {
    this.mainActivity = mainActivity;
    this.queryUsbAcc = queryUsbAcc;
    this.usbAccessory = usbAccessory;
    super.shareConnection = shareConnection;
}
项目:sdl_android    文件:USBTransportConfig.java   
public UsbAccessory getUsbAccessory () {
    return usbAccessory;
}
项目:assertj-android    文件:UsbAccessoryAssert.java   
public UsbAccessoryAssert(UsbAccessory actual) {
  super(actual, UsbAccessoryAssert.class);
}
项目:sdl_android    文件:USBTransportConfig.java   
public void setUsbAccessory (UsbAccessory value) { usbAccessory = value; }