Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- manager = (UsbManager) getSystemService(Context.USB_SERVICE);
- // ------------------------------------------------------------------
- mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(
- ACTION_USB_PERMISSION), 0);
- IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
- registerReceiver(mUsbReceiver, filter);
- // -------------------------------------------------------------------
- HashMap<String, UsbDevice> deviceList = manager.getDeviceList();
- Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
- String i = "";
- while (deviceIterator.hasNext()) {
- device = deviceIterator.next();
- manager.requestPermission(device, mPermissionIntent);
- i += "n" + "DeviceID: " + device.getDeviceId() + "n"
- + "DeviceName: " + device.getDeviceName() + "n"
- + "DeviceClass: " + device.getDeviceClass() + " - "
- + "DeviceSubClass: " + device.getDeviceSubclass() + "n"
- + "VendorID: " + device.getVendorId() + "n"
- + "ProductID: " + device.getProductId() + "n"
- + "DeviceProtocol: " + device.getDeviceProtocol() + "n"
- + "Interface: " + device.getInterface(0) + "n";
- textInfo.setText(i);
- UsbInterface usbInterface = device.getInterface(0);
- UsbEndpoint usbEndpoint = usbInterface.getEndpoint(0);
- UsbDeviceConnection connection = manager.openDevice(device);
- connection.claimInterface(usbInterface, true);
- String setData = "AT";
- byte[] bytesVar = setData.getBytes();
- connection.bulkTransfer(endpoint, bytesVar, bytesVar.length, TIMEOUT);
- private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {
- public void onReceive(Context context, Intent intent) {
- String action = intent.getAction();
- if (ACTION_USB_PERMISSION.equals(action)) {
- synchronized (this) {
- UsbDevice device = (UsbDevice) intent
- .getParcelableExtra(UsbManager.EXTRA_DEVICE);
- if (intent.getBooleanExtra(
- UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
- if (device != null) {
- // call method to set up device communication
- }
- } else {
- Log.d("ERROR", "permission denied for device " + device);
- }
- }
- }
- }
- };
Add Comment
Please, Sign In to add comment