Android UsbManager - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Android UsbManager class.

This code example shows how to use the following methods:openAccessory
       super.onDestroy(); 
    } 
 
    private void openAccessory(UsbAccessory accessory) {
        Log.d(TAG, "openAccessory: " + accessory);
        mFileDescriptor = mUsbManager.openAccessory(accessory);
        if (mFileDescriptor != null) {
            FileDescriptor fd = mFileDescriptor.getFileDescriptor();
            mInputStream = new FileInputStream(fd);
            mOutputStream = new FileOutputStream(fd);
            Thread thread = new Thread(null, this, "AccessoryChat");
            thread.start();
            Log.d(TAG, "openAccessory succeeded");
        } else { 
            Log.d(TAG, "openAccessory fail");
        } 
    } 
 
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_DONE && mOutputStream != null) {
7
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java
This code example shows how to use the following methods:getDeviceList, openDevice
        manager = (UsbManager) context.getSystemService(Service.USB_SERVICE);
    } 
 
    @SuppressWarnings("UnusedDeclaration") 
    public static UsbDeviceConnection openDevice(String path) {
        UsbDevice device = manager.getDeviceList().get(path);
        if (device == null) return null;
        return manager.openDevice(device);
    } 
} 
6
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java
This code example shows how to use the following methods:getAccessoryList, hasPermission
            //       Note however that the IDE still thinks that Gingerbread is required 
            //       thus the use of the `@TargetAPi(12)` above. 
            Class.forName("android.hardware.usb.UsbManager");
 
            UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
            UsbAccessory[] accessoryList = manager.getAccessoryList();
 
            // Note: The permissions check here is because sometimes we end up 
            //       with spurious accessories (e.g. ^B in the device description etc) 
            //       and presumably it also stops problems if an accessory is connected 
            //       when we're open. 
            result = ((accessoryList != null) && (manager.hasPermission(accessoryList[0])));
        } catch (ClassNotFoundException e) {
            Log.d(this.getClass().getSimpleName(),
                    "No USB accessory support, 'android.hardware.usb.UsbManager' not found."); 
        } 
 
        return result;
    } 
 
8
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java
This code example shows how to use the following methods:openAccessory
       super.onDestroy(); 
    } 
 
    private void openAccessory(UsbAccessory accessory) {
        Log.d(TAG, "openAccessory: " + accessory);
        mFileDescriptor = mUsbManager.openAccessory(accessory);
        if (mFileDescriptor != null) {
            FileDescriptor fd = mFileDescriptor.getFileDescriptor();
            mInputStream = new FileInputStream(fd);
            mOutputStream = new FileOutputStream(fd);
            Thread thread = new Thread(null, this, "AccessoryChat");
            thread.start();
            Log.d(TAG, "openAccessory succeeded");
        } else { 
            Log.d(TAG, "openAccessory fail");
        } 
    } 
 
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_DONE && mOutputStream != null) {
6
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java
This code example shows how to use the following methods:openDevice
            return; 
        } 
        mDevice = device;
        mEndpointIntr = ep;
        if (device != null) {
            UsbDeviceConnection connection = mUsbManager.openDevice(device);
            if (connection != null && connection.claimInterface(intf, true)) {
                Log.d(TAG, "open SUCCESS");
                mConnection = connection;
                Thread thread = new Thread(this);
                thread.start();
 
            } else { 
                Log.d(TAG, "open FAIL");
                mConnection = null;
            } 
         } 
    } 
 
    private void sendCommand(int control) {
6
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java
This code example shows how to use the following methods:getAccessoryList, hasPermission, openAccessory
This code example shows how to use the following methods:getAccessoryList, hasPermission, openAccessory, requestPermission
	// Sets up all the requests for permission and attaches the USB accessory if 
	// permission is already granted 
	public boolean 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);
		 
		if(accessoryList != null && accessoryList[0] != null) {
			 
			mManager.requestPermission(accessoryList[0], mPermissionIntent);
			mAccessory = accessoryList[0];
			if(mManager.hasPermission(mAccessory))
			{ 
				openAccessory(mAccessory);
				return true; 

5
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java
This code example shows how to use the following methods:getAccessoryList, openAccessory
		if (mInputStream != null || mOutputStream != null) {
			Log.d(TAG, "already connected");
			return; 
		} 
 
		UsbAccessory[] accessories = usbManager.getAccessoryList();
		mAccessory = (accessories == null ? null
				: accessories[0]);
		if (mAccessory == null) {
			Toast.makeText(this, "No TinyG USB accessory devices attached!", Toast.LENGTH_SHORT).show();
			return; 
		} 
		mFileDescriptor = usbManager.openAccessory(mAccessory);
		if (mFileDescriptor != null) {
			FileDescriptor fd = mFileDescriptor.getFileDescriptor();
			if (fd.valid()) {
				mInputStream = new FileInputStream(fd);
				mOutputStream = new FileOutputStream(fd);
			} else { 
				Log.d(TAG, "FD for accessory not valid!");
5
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java
This code example shows how to use the following methods:getAccessoryList, hasPermission
 
			// TODO Honeycomb 以前は com.android.future.usb.UsbManager 
			mUsbManager = (UsbManager) activity
					.getSystemService(Context.USB_SERVICE);
 
			UsbAccessory[] accessoryList = mUsbManager.getAccessoryList();
			if (accessoryList != null && accessoryList.length == 1) {
				UsbAccessory accessory = accessoryList[0];
				if (mUsbManager.hasPermission(accessory)) {
					Accessory.setupInstance(mCallback, mUsbManager, accessory);
				} else { 
					PendingIntent pendingIntent = PendingIntent.getBroadcast(
							activity, 0, new Intent(permissionAction), 0);
					mUsbManager.requestPermission(accessory, pendingIntent);
				} 
			} 
 
			mFilter = new IntentFilter(permissionAction);
			mFilter.addAction(UsbManager.ACTION_USB_ACCESSORY_DETACHED);
			mPermissionReceiver = new AccessoryPermissionReceiver(mCallback,
5
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java
This code example shows how to use the following methods:getAccessoryList
	public boolean hasPermission(Object theAccessoryObj) {
		return mUsbManager.hasPermission((UsbAccessory) theAccessoryObj);
	} 
	 
	public Object getConnectedAccessory() {
		UsbAccessory[] accessories = mUsbManager.getAccessoryList();
		UsbAccessory accessory = (accessories == null ? null : accessories[0]);
		return accessory;
	} 
 
	public Object getAccessory() {
		return mAccessory;
	} 
	 
	 
	public void requestPermission(Object theAccessoryObj, PendingIntent permissionIntent) {
		mUsbManager.requestPermission((UsbAccessory) theAccessoryObj, permissionIntent);
	} 
} 
5
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java

Related Android UsbManager Questions & Answers:

See Code Examples for Android UsbManager Methods: