本文整理匯總了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;
}
示例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);
}
}
}
示例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;
}
示例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();
}
}
示例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);
}
}
示例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);
}
示例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));
}
}
示例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);
}
示例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);
}
}
示例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);
}
}
示例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);
}
}
示例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();
}
}
示例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!");
}
}