本文整理汇总了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: 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));
}
}
示例7: 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);
}
示例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!");
}
}