當前位置: 首頁>>代碼示例>>Java>>正文


Java UsbManager.requestPermission方法代碼示例

本文整理匯總了Java中android.hardware.usb.UsbManager.requestPermission方法的典型用法代碼示例。如果您正苦於以下問題:Java UsbManager.requestPermission方法的具體用法?Java UsbManager.requestPermission怎麽用?Java UsbManager.requestPermission使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在android.hardware.usb.UsbManager的用法示例。


在下文中一共展示了UsbManager.requestPermission方法的13個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: mountDevice

import android.hardware.usb.UsbManager; //導入方法依賴的package包/類
private FileSystem mountDevice() {
    Log.d("MountTask", "opening OTG disk...");
    manager = (UsbManager) context.getSystemService(Context.USB_SERVICE);
    HashMap<String, UsbDevice> devices = manager.getDeviceList();
    Iterator<UsbDevice> deviceIterator = devices.values().iterator();
    if (devices.size() < 1) {
        Log.e("MountTask", "No device found...");
        errorMessageId = R.string.pluginDisk;
    } else if (deviceIterator.hasNext()) {
        device = deviceIterator.next();
        Log.d("MountTask", String.format("Found device: %04X:%04X, Class: %02X:%02X, at %s",
                device.getVendorId(), device.getProductId(),
                device.getDeviceClass(), device.getDeviceSubclass(),
                device.getDeviceName()));
        if (manager.hasPermission(device)) {
            return claimInterface(device);
        } else {
            Log.e("MountTask", "No permission granted to access this device, requesting...");
            manager.requestPermission(device,
                    PendingIntent.getBroadcast(context, 0, new Intent(MainActivity.ACTION_USB_PERMISSION), 0));
        }
        Log.d("MountTask", "No more devices found");
    }
    return null;
}
 
開發者ID:rostskadat,項目名稱:OTGDiskBackup,代碼行數:26,代碼來源:MountTask.java

示例2: setup

import android.hardware.usb.UsbManager; //導入方法依賴的package包/類
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);
            }
        }

    }
 
開發者ID:victordiaz,項目名稱:phonk,代碼行數:21,代碼來源:AdkPort.java

示例3: createTransceiver

import android.hardware.usb.UsbManager; //導入方法依賴的package包/類
private static ACSTransceiver createTransceiver(Reader reader, final Context context) throws IOException {
    UsbManager manager = (UsbManager) context.getSystemService(Context.USB_SERVICE);

    UsbDevice externalDevice = externalReaderAttached(manager, reader);

    ACSTransceiver transceiver;
    try {


        if(!reader.isOpened()) {
            reader.open(externalDevice);
            Log.d(TAG, "Reader opened");
        }

        transceiver = createAcsTransceiver(reader, externalDevice);
    } catch (IllegalArgumentException e) {
        Intent usbIntent = new Intent(ACTION_USB_PERMISSION);
        PendingIntent permissionIntent = PendingIntent.getBroadcast(context, 0, usbIntent, 0);
        manager.requestPermission(externalDevice, permissionIntent);
        Log.d(TAG, "could not access device, no permissions given?", e);
        throw new IOException(e);
    }

    return transceiver;
}
 
開發者ID:coinblesk,項目名稱:coinblesk-client-gui,代碼行數:26,代碼來源:NFCServerACSCLTV.java

示例4: onResume

import android.hardware.usb.UsbManager; //導入方法依賴的package包/類
@Override
protected void onResume() {
    super.onResume();
    changed=new HandleChange(3);
    calibCounter=0;
    Log.d(TAG, "Resumed, port=" + sPort);
    if (sPort == null) {
        mTitleTextView.setText("No serial device.");
    } else {
        usbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
        List<UsbSerialDriver> availableDrivers = UsbSerialProber.getDefaultProber().findAllDrivers(usbManager);
        if (availableDrivers.isEmpty()) {
            mTitleTextView.append("No drivers available\n");
            Log.d("SerialKost", "no drivers available");
            return;
        }

        UsbSerialDriver driver = availableDrivers.get(0);
        sPort = driver.getPorts().get(0);
        device=driver.getDevice();
        UsbDeviceConnection connection = usbManager.openDevice(device);
        if (connection == null) {
            mTitleTextView.setText("Opening device failed");

            PendingIntent mPermissionIntent;

            Log.i("SerialKost", "Setting PermissionIntent -> MainMenu");
            mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);
            Log.i("SerialKost", "Setting IntentFilter -> MainMenu");
            IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
            Log.i("SerialKost", "Setting registerReceiver -> MainMenu");
            registerReceiver(mUsbReceiver, filter);
            Log.i("SerialKost", "Setting requestPermission -> MainMenu");
            usbManager.requestPermission(device, mPermissionIntent);
            return;
        }
        communicate();
    }
}
 
開發者ID:kost,項目名稱:DroidMeter,代碼行數:40,代碼來源:SerialConsoleActivity.java

示例5: connect

import android.hardware.usb.UsbManager; //導入方法依賴的package包/類
@Override
public void connect() {
	Log.d(LOG_TAG, "connect:");
	if (deviceConnection != null) {
		Log.e(LOG_TAG, "connect: Already connected!");
		return;
	} else if (device.getInterfaceCount() == 0) {
		// 2016.12.17 [email protected]
		// If you got here and you don't know why, the following link might be helpful.
		// https://code.google.com/p/android/issues/detail?id=159529
		throw new UsbDeviceHasNoInterfaceException("The USB device(" + device.getDeviceName() + ") has no USB interface!");
	}
	registerReceiver();

	final UsbManager usbManager = (UsbManager) context.getSystemService(Context.USB_SERVICE);
	if (usbManager.hasPermission(device)) {
		startConnecting();
	} else {
		// Ask permission for USB connection
		PendingIntent intent = PendingIntent.getBroadcast(context, 0, new Intent(ACTION_USB_PERMISSION), 0);
		Log.d(LOG_TAG, "Requesting permission for USB device '" + device.getDeviceName() + "'...");
		usbManager.requestPermission(device, intent);
	}
}
 
開發者ID:Neofect,項目名稱:Communicator,代碼行數:25,代碼來源:UsbConnection.java

示例6: registerUsbPermission

import android.hardware.usb.UsbManager; //導入方法依賴的package包/類
/**
 * 注冊操作usb設備需要的權限
 * @param usbDevice
 */
void registerUsbPermission( UsbDevice usbDevice ){
    log("請求USB設備訪問權限");
    UsbManager usbManager = (UsbManager)getSystemService(Context.USB_SERVICE);
    IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
    registerReceiver(usbPermissionReceiver, filter);
    PendingIntent mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);
    usbManager.requestPermission(usbDevice, mPermissionIntent);
}
 
開發者ID:iyundong,項目名稱:InstantUpload,代碼行數:13,代碼來源:ControllerActivity.java

示例7: obtainFdFor

import android.hardware.usb.UsbManager; //導入方法依賴的package包/類
public static Future<UsbDeviceConnection> obtainFdFor(Context context, UsbDevice usbDevice) {
    UsbManager manager = (UsbManager) context.getSystemService(Context.USB_SERVICE);
    if (!manager.hasPermission(usbDevice)) {
        AsyncFuture<UsbDeviceConnection> task = new AsyncFuture<>();
        registerNewBroadcastReceiver(context, usbDevice, task);
        manager.requestPermission(usbDevice, PendingIntent.getBroadcast(context, 0, new Intent(ACTION_USB_PERMISSION), 0));
        return task;
    } else {
        return new CompletedFuture<>(manager.openDevice(usbDevice));
    }
}
 
開發者ID:martinmarinov,項目名稱:AndroidDvbDriver,代碼行數:12,代碼來源:UsbPermissionObtainer.java

示例8: initReader

import android.hardware.usb.UsbManager; //導入方法依賴的package包/類
private void initReader() {
    UsbManager manager = (UsbManager) getContext().getSystemService(Context.USB_SERVICE);
    UsbDevice externalDevice = externalReaderAttached(manager, reader);

    Intent usbIntent = new Intent(ACTION_USB_PERMISSION);
    PendingIntent permissionIntent = PendingIntent.getBroadcast(getContext(), 0, usbIntent, 0);
    manager.requestPermission(externalDevice, permissionIntent);
}
 
開發者ID:coinblesk,項目名稱:coinblesk-client-gui,代碼行數:9,代碼來源:NFCServerACSCLTV.java

示例9: requestUsbPermission

import android.hardware.usb.UsbManager; //導入方法依賴的package包/類
private void requestUsbPermission() {
    if (!hasUsbPermission()) {
        UsbManager usbManager = (UsbManager) this.getSystemService(Context.USB_SERVICE);
        UsbDevice cnlDevice = UsbHidDriver.getUsbDevice(usbManager, MedtronicCnlIntentService.USB_VID, MedtronicCnlIntentService.USB_PID);
        PendingIntent permissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(MedtronicCnlIntentService.Constants.ACTION_USB_PERMISSION), 0);
        usbManager.requestPermission(cnlDevice, permissionIntent);
    }
}
 
開發者ID:pazaan,項目名稱:600SeriesAndroidUploader,代碼行數:9,代碼來源:MainActivity.java

示例10: requestUsbPermission

import android.hardware.usb.UsbManager; //導入方法依賴的package包/類
private void requestUsbPermission(UsbManager manager,
                                  UsbDevice device,
                                  Promise p) {

    try {
        ReactApplicationContext rAppContext = getReactApplicationContext();
        PendingIntent permIntent = PendingIntent.getBroadcast(rAppContext, 0, new Intent(ACTION_USB_PERMISSION), 0);

        registerBroadcastReceiver(p);

        manager.requestPermission(device, permIntent);
    } catch (Exception e) {
        p.reject(e);
    }
}
 
開發者ID:mapaiva,項目名稱:react-native-usbserial,代碼行數:16,代碼來源:ReactUsbSerialModule.java

示例11: connectToDevice

import android.hardware.usb.UsbManager; //導入方法依賴的package包/類
private void connectToDevice(UsbManager manager, int vendorId,
        int productId) throws DataSourceResourceException {
    UsbDevice device = findDevice(manager, vendorId, productId);
    if(manager.hasPermission(device)) {
        Log.d(TAG, "Already have permission to use " + device);
        openConnection(device);
    } else {
        Log.d(TAG, "Requesting permission for " + device);
        manager.requestPermission(device, mPermissionIntent);
    }
}
 
開發者ID:bibhrajit,項目名稱:openxc-androidStudio,代碼行數:12,代碼來源:UsbVehicleInterface.java

示例12: onCreate

import android.hardware.usb.UsbManager; //導入方法依賴的package包/類
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    if (AndProxApplication.hasUsbHostSupport()) {
        UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
        PendingIntent mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);
        IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
        registerReceiver(mUsbReceiver, filter);

        dumpUsbDeviceInfo(manager);
        List<UsbSerialDriver> availableDrivers = UsbSerialProber.getDefaultProber().findAllDrivers(manager);
        if (availableDrivers.isEmpty()) {
            Log.d(TAG, "onCreate: no devices present");
            return;
        }

        // Open a connection to the first available driver.
        UsbSerialDriver driver = availableDrivers.get(0);
        UsbDevice device = driver.getDevice();
        manager.requestPermission(device, mPermissionIntent);
    } else {
        Log.e(TAG, "no USB host support!");
        findViewById(R.id.btnConnect).setEnabled(false);
        ((TextView) findViewById(R.id.tvIntroText)).setText(R.string.no_usb_host);

        if (savedInstanceState == null) {
            AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
            builder.setMessage(R.string.no_usb_host)
                    .setTitle(R.string.no_usb_host_title);
            builder.show();
        }
    }

    // Request permission for storage right away, as requesting this when a file is explicitly
    // opened is tedious.
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, STORAGE_PERMISSION_CALLBACK);
    } else {
        // We already have permissions
        CopyTask t = new CopyTask();
        t.execute();
    }
}
 
開發者ID:AndProx,項目名稱:AndProx,代碼行數:46,代碼來源:MainActivity.java

示例13: deviceConnect

import android.hardware.usb.UsbManager; //導入方法依賴的package包/類
public void deviceConnect() {

        log("connect");

        if (connection == null) {

            // Find all available drivers from attached devices.
            manager = (UsbManager) getSystemService(Context.USB_SERVICE);

            HashMap<String, UsbDevice> devs = manager.getDeviceList();

            if (devs.isEmpty()) {
                log("devs.isEmpty()");
                return;
            } else {
                for (String d: devs.keySet()) {
                    UsbDevice dev = (UsbDevice) devs.get(d);
                    log(
                            "  dev (" + d + "): " +
                                    Integer.toHexString(dev.getVendorId()) +
                                    ":" +
                                    Integer.toHexString(dev.getProductId()));
                    log("  permission: " + manager.hasPermission(dev));

                    if (dev.getVendorId() == 0x16c0 && dev.getProductId() == 0x05df) {
                        device = dev;
                    }
                }
            }

            if (device == null) {
                log("device not found... :(");
                return;
            }

            // Ask USB Permission from user
            // http://arduino-er.blogspot.com.br/2013/03/request-permission-to-access-usb.html
            Intent intent_UsbPermission = new Intent("permission");
            PendingIntent PendingIntent_UsbPermission = PendingIntent.getBroadcast(
                    this,      //context
                    0, // RQS_USB_PERMISSION,  //request code
                    intent_UsbPermission, //intent
                    0);      //flags
            IntentFilter intentFilter_UsbPermission = new IntentFilter("permission");
            BroadcastReceiver myUsbPermissionReceiver = new BroadcastReceiver(){

                @Override
                public void onReceive(Context context, Intent intent) {
                    String action = intent.getAction();
                    if(action.equals("permission")){

                        synchronized(this) {
                            connection = manager.openDevice(device);
                            log("permission granted!");
                            log("connection: " + connection);
                        }
                    }
                }

            };
            registerReceiver(myUsbPermissionReceiver, intentFilter_UsbPermission);
            manager.requestPermission(device, PendingIntent_UsbPermission);
            // end permission request

            log("interface counter:" + device.getInterfaceCount());
            if (connection == null) {
                log("but connection is null :(");
            } else {
                log("connection: " + connection);
            }

        } else {
            log("using same connection!");
        }

    }
 
開發者ID:rodolfo3,項目名稱:android-vusb-arduino,代碼行數:77,代碼來源:MainActivity.java


注:本文中的android.hardware.usb.UsbManager.requestPermission方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。