@Override
 public void onReceive(Context context, Intent intent) {
   final String action = intent.getAction();
   Log.d(TAG, "Received broadcast: " + action);
   if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action)) {
     Log.d(TAG, "Device detached");
     onUsbDeviceDetached();
   } else {
     Log.d(TAG, "Unknown action: " + action);
   }
 }
        public void onReceive(Context context, Intent intent) {
          String action = intent.getAction();
          if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action)) {
            synchronized (this) {
              UsbDevice device = (UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);

              if (device != null) {
                //
                Log.d("1", "DEATTCHED-" + device);
              }
            }
          }
          //
          if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)) {
            synchronized (this) {
              UsbDevice device = (UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
              if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {

                if (device != null) {
                  //

                  Log.d("1", "ATTACHED-" + device);
                }
              } else {
                PendingIntent mPermissionIntent;
                mPermissionIntent =
                    PendingIntent.getBroadcast(
                        MainActivity.this,
                        0,
                        new Intent(ACTION_USB_PERMISSION),
                        PendingIntent.FLAG_ONE_SHOT);
                mUsbManager.requestPermission(device, mPermissionIntent);
              }
            }
          }
          //
          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) {
                  //
                  Log.d("1", "PERMISSION-" + device);
                }
              }
            }
          }
        }
  @Override
  public void onResume() {
    super.onResume();

    Intent intent = getIntent();
    String action = intent.getAction();

    UsbDevice device = (UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
    if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)) {
      setDevice(device);
    } else if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action)) {
      if (deviceFound != null && deviceFound.equals(device)) {
        setDevice(null);
      }
    }
  }
        public void onReceive(Context context, Intent intent) {
          if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(intent.getAction())) {
            if (DEBUG_MODE) Log.d(TAG, "USB Device Detached");

            if (connected instanceof UsbConnectedThread) {
              if (((UsbConnectedThread) connected)
                  .isUsbDevice((UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE))) {
                if (DEBUG_MODE) Log.d(TAG, "Adaptronic ECU Detached");
                disconnect();
                connected = null;

                imgStatus.setBackgroundColor(Color.TRANSPARENT);
              }
            }
          }
        }
        public void onReceive(Context context, Intent intent) {
          String action = intent.getAction();

          if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)) {
            UsbDevice device = (UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
            UsbInterface intf = findAdbInterface(device);
            if (intf != null) {
              log("Found adb interface " + intf);
              setAdbInterface(device, intf);
            }
          } else if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action)) {
            UsbDevice device = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
            String deviceName = device.getDeviceName();
            if (mDevice != null && mDevice.equals(deviceName)) {
              log("adb interface removed");
              setAdbInterface(null, null);
            }
          }
        }
Beispiel #6
0
  @Override
  /**
   * This function checks for which intent started the app and checks whether a usb device is
   * attached or not
   */
  public void onResume() {
    super.onResume();
    Intent intent = getIntent();
    Log.d(TAG, "onResume(" + intent + ")");
    String action = intent.getAction();
    deviceConnected(false);
    if (action.equals("android.intent.action.MAIN")) {
      // Toast.makeText(getApplicationContext(),
      // "Reconnect the USB devices.", Toast.LENGTH_LONG).show();
      // finish();
    }

    if (action.equals("android.hardware.usb.action.USB_DEVICE_ATTACHED")
        | action.equals("android.intent.action.MAIN")) {
      UsbDevice device = (UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
      if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)) {
        Log.d(TAG, "INTENT DEVICE ATTACHED=" + device.toString());
        RoboCatActivity.device = device;
        deviceConnected(true);

      } else if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action)) {
        Log.d(TAG, "INTENT DEVICE DETACHED=" + device.toString());
        RoboCatActivity.device = device;
        deviceConnected(false);
      } else {
        Log.d(TAG, "Unexpected Action=" + action.toString());
      }
    } else {
      // Toast.makeText(getApplicationContext(),
      // "Invalid USB device or USB device not found.", Toast.LENGTH_LONG).show();

    }
    Log.d("About to initialize", "Initializing " + RoboCatActivity.deviceConnected);
    RoboCatActivity.initializeMaestro((UsbManager) getSystemService(USB_SERVICE));
  }
        public void onReceive(Context context, Intent intent) {
          if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(intent.getAction())) {
            SharedPreferences settings =
                PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
            Boolean exitPrefDefault =
                Boolean.parseBoolean(getString(R.string.scantool_detach_disconnect));
            Boolean exitPrefValue =
                settings.getBoolean("scantool_detach_disconnect", exitPrefDefault);

            if (exitPrefValue) {
              Intent exitIntent =
                  new Intent(getBaseContext(), SteeringWheelInterfaceActivity.class);
              exitIntent.setAction(Intent.ACTION_DELETE);
              exitIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
              startActivity(exitIntent);

              // TODO: make this and future toast notifications enabled/disabled via a setting
              // Toast.makeText(getApplicationContext(),
              // getString(R.string.msg_device_disconnected), Toast.LENGTH_SHORT).show();
            } else {
              carInterfaceStop();
            }
          }
        }
        @Override
        public void onReceive(Context context, Intent intent) {
          String action = intent.getAction();
          if (DEBUG) Toast.makeText(context, "Broadcast Receiver", Toast.LENGTH_SHORT).show();

          if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)) { // will intercept by system
            if (DEBUG) Toast.makeText(context, "USB Attached", Toast.LENGTH_SHORT).show();
            UsbDevice device = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
            mUsbCommunication.setUsbInterface(mManager, device);
            setUsbState(true);

          } else if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action)) {
            if (DEBUG) Toast.makeText(context, "USB Detached", Toast.LENGTH_SHORT).show();
            mUsbCommunication.setUsbInterface(null, null);
            setUsbState(false);
            //				getReaderSn(false);

          } else if (ACTION_USB_PERMISSION.equals(action)) {
            if (DEBUG) Toast.makeText(context, "USB Permission", Toast.LENGTH_SHORT).show();
            Log.d(UsbCommunication.TAG, "permission");
            synchronized (this) {
              UsbDevice device = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
              if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
                mUsbCommunication.setUsbInterface(mManager, device);
                setUsbState(true);
                sleep(400);
                if (bSavedInst) getReaderSn(true);
                setPowerLevel();
                setPowerState();
                if (iMenu == R.menu.menu_option_xlarge) insertFragTag();
              } else {
                finish();
              }
            }
          }
        }
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    tv1 = (TextView) findViewById(R.id.tv1);
    tv2 = (TextView) findViewById(R.id.tv2);
    tv3 = (TextView) findViewById(R.id.tv3);
    tv4 = (TextView) findViewById(R.id.tv4);
    tv5 = (TextView) findViewById(R.id.tv5);
    tv6 = (TextView) findViewById(R.id.tv6);
    button1 = (Button) findViewById(R.id.button1);
    button2 = (Button) findViewById(R.id.button2);
    button3 = (Button) findViewById(R.id.button3);
    button4 = (Button) findViewById(R.id.button4);
    ////////////////// OnResume

    mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
    Intent intent = getIntent();
    String action = intent.getAction();

    UsbDevice device = (UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
    mDevice = device;

    if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)) {
      session = new Session();
      setDevice(device);

    } else if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action)) {
      if (mDevice != null && mDevice.equals(device)) {
        // setDevice(null);
      }
    }

    ///////////////////////////
    button1.setOnClickListener(
        new View.OnClickListener() {
          @Override
          public void onClick(View v) {
            // TODO Auto-generated method stub
            clearTV();
          }
        });

    button2.setOnClickListener(
        new View.OnClickListener() {
          @Override
          public void onClick(View v) {
            clearTV();

            Command command = new Command(Command.OpenSession, session, session.getNextSessionID());
            mConnection.bulkTransfer(mEndpointBulkOut, command.data, command.length, 1000);
            session.open();
            byte buf[] = new byte[inMaxPS];
            mConnection.bulkTransfer(mEndpointBulkIn, buf, inMaxPS, 1000);

            tv1.setText("Starting. ");
            command = new Command(Command.GetDeviceInfo, session);
            mConnection.bulkTransfer(mEndpointBulkOut, command.data, command.length, 1000);
            tv1.append(" Sent:");

            StringBuffer hexString1 = new StringBuffer();
            for (int i = 0; i < command.data.length; i++) {
              hexString1.append(Integer.toHexString(0xFF & command.data[i]));
            }
            tv1.append(hexString1);

            mConnection.bulkTransfer(mEndpointBulkIn, buf, inMaxPS, 1000);

            tv1.append(" Receiving:");
            StringBuffer hexString = new StringBuffer();
            for (int i = 0; i < buf.length; i++) {
              hexString.append(Integer.toHexString(0xFF & buf[i]));
            }
            tv1.append(hexString);

            DeviceInfo info = new DeviceInfo(factory);
            info.data = buf;
            info.length = info.getLength();
            info.parse();
            if (info.vendorExtensionId != 0) {
              factory = factory.updateFactory(info.vendorExtensionId);
              info.factory = factory;
            }
            info.showInTextView(tv2);

            command = new Command(Command.CloseSession, session);
            mConnection.bulkTransfer(mEndpointBulkOut, command.data, command.length, 1000);
            session.close();
            mConnection.bulkTransfer(mEndpointBulkIn, buf, inMaxPS, 1000);
            Response response = new Response(buf, inMaxPS, factory);
            tv6.setText("Type:" + response.getBlockType() + "\n");
            tv6.append("Name:" + response.getCodeName(response.getCode()) + "\n");
            tv6.append("CodeString:" + response.getCodeString() + "\n");
            tv6.append("Length:" + response.getLength() + "\n");
            tv6.append("String:" + response.toString());
          }
        });

    button3.setOnClickListener(
        new View.OnClickListener() {
          @Override
          public void onClick(View v) {
            clearTV();
            tv1.setText("Starting. ");

            Command command = new Command(Command.OpenSession, session, session.getNextSessionID());
            mConnection.bulkTransfer(mEndpointBulkOut, command.data, command.length, 1000);
            session.open();
            byte buf[] = new byte[inMaxPS];
            mConnection.bulkTransfer(mEndpointBulkIn, buf, inMaxPS, 1000);
            /*
            tv1.append(" Receiving:");
            StringBuffer hexString = new StringBuffer();
            for (int i=0;i<buf.length;i++) {
                hexString.append(Integer.toHexString(0xFF & buf[i]));
                }
            tv1.append(hexString);
            */
            Response response = new Response(buf, inMaxPS, factory);
            tv1.setText("Type:" + response.getBlockType() + "\n");
            tv1.append("Name:" + response.getCodeName(response.getCode()) + "\n");
            tv1.append("CodeString:" + response.getCodeString() + "\n");
            tv1.append("Length:" + response.getLength() + "\n");
            tv1.append("String:" + response.toString());

            command = new Command(Command.EOS_OC_Capture, session);
            mConnection.bulkTransfer(mEndpointBulkOut, command.data, command.length, 1000);

            mConnection.bulkTransfer(mEndpointBulkIn, buf, inMaxPS, 1000);
            response = new Response(buf, inMaxPS, factory);

            tv2.setText("Type:" + response.getBlockType() + "\n");
            tv2.append("Name:" + response.getCodeName(response.getCode()) + "\n");
            tv2.append("CodeString:" + response.getCodeString() + "\n");
            tv2.append("Length:" + response.getLength() + "\n");
            tv2.append("String:" + response.toString());

            mConnection.bulkTransfer(mEndpointIntr, buf, inMaxPS, 1000);
            tv3.setText(" Event:");
            StringBuffer hexString = new StringBuffer();
            for (int i = 0; i < buf.length; i++) {
              hexString.append(Integer.toHexString(0xFF & buf[i]));
            }
            tv3.append(hexString);
            Event event = new Event(buf, factory);

            tv3.append(" Event Code:" + event.getCodeString());
            mConnection.bulkTransfer(mEndpointIntr, buf, inMaxPS, 1000);
            tv4.setText(" Event:");
            hexString = new StringBuffer();
            for (int i = 0; i < buf.length; i++) {
              hexString.append(Integer.toHexString(0xFF & buf[i]));
            }
            tv4.append(hexString);
            event = new Event(buf, factory);

            tv4.append(" Event Code:" + event.getCodeString());

            command = new Command(Command.CloseSession, session);
            mConnection.bulkTransfer(mEndpointBulkOut, command.data, command.length, 1000);
            session.close();
            mConnection.bulkTransfer(mEndpointBulkIn, buf, inMaxPS, 1000);
            response = new Response(buf, inMaxPS, factory);
            tv6.setText("Type:" + response.getBlockType() + "\n");
            tv6.append("Name:" + response.getCodeName(response.getCode()) + "\n");
            tv6.append("CodeString:" + response.getCodeString() + "\n");
            tv6.append("Length:" + response.getLength() + "\n");
            tv6.append("String:" + response.toString());
          }
        });

    button4.setOnClickListener(
        new View.OnClickListener() {
          @Override
          public void onClick(View v) {
            // mConnection.controlTransfer(CLASS_GET_DEVICE_STATUS, CLASS_GET_DEVICE_STATUS,
            //		CLASS_GET_DEVICE_STATUS, RESULT_OK, null, inMaxPS, 1000);
            // http://developer.android.com/reference/android/hardware/usb/UsbDeviceConnection.html#controlTransfer(int, int, int, int, byte[], int, int)
            // http://www.beyondlogic.org/usbnutshell/usb6.shtml#SetupPacket
            // startActivity(new Intent("com.ash.beta.CONTROLACTIVITY"));

          }
        });
  }