@Override
  protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);

    if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(intent.getAction())) {
      UsbDevice usbDevice = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
      if (usbDevice != null) { // paranoia
        // We might get attachment notifications before the event loop is set up, so
        // we hold on to them and pass them along only when we're good and ready.
        if (receivedUsbAttachmentNotifications != null) { // *total* paranoia
          receivedUsbAttachmentNotifications.add(usbDevice);
          passReceivedUsbAttachmentsToEventLoop();
        }
      }
    }
  }
        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);
      }
    }
  }
  @Override
  protected void onResume() {
    super.onResume();

    Intent intent = getIntent();
    if (intent == null) return;

    String action = intent.getAction();
    if (!UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)) return;

    Parcelable usbDeviceParcel = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);

    Intent broadcastIntent = new Intent(ACTION_USB_DEVICE_ATTACHED);
    broadcastIntent.putExtra(UsbManager.EXTRA_DEVICE, usbDeviceParcel);

    sendBroadcast(broadcastIntent);
  }
        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);
            }
          }
        }
Example #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));
  }
        @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();
              }
            }
          }
        }
  @Override
  protected void onResume() {
    super.onResume();

    // initialize our preferences
    tempUomPref = Integer.parseInt(prefs.getString("prefs_uom_temp", "1"));
    afrNotEqualTargetPref = prefs.getBoolean("prefs_afrnottarget_pref", false);
    afrNotEqualTargetTolerance = prefs.getFloat("prefs_afrnottarget_tolerance_pref", 5f);
    waterTempPref = prefs.getBoolean("prefs_watertemp_pref", false);
    remoteName = prefs.getString("prefs_remote_name", "");
    remoteMacAddr = prefs.getString("prefs_remote_mac", "");
    autoConnect = prefs.getBoolean("prefs_auto_connect", false);

    // need to set gauge scale based on uom selected
    switch (tempUomPref) {
      case 0:
        minimumWaterTemp =
            prefs.getFloat("prefs_min_water_temp", AdaptivePreferences.MIN_WATER_TEMP_CELCIUS);
        maximumWaterTemp =
            prefs.getFloat("prefs_max_water_temp", AdaptivePreferences.MAX_WATER_TEMP_CELCIUS);

        iatNeedle.setPivotPoint(.5f);
        iatNeedle.setMinValue(0);
        iatNeedle.setMaxValue(100);
        iatNeedle.setMinDegrees(-180);
        iatNeedle.setMaxDegrees(90);

        imgIat.setImageResource(R.drawable.iatgauge_celcius);
        break;

      case 1:
        minimumWaterTemp =
            prefs.getFloat("prefs_min_water_temp", AdaptivePreferences.MIN_WATER_TEMP_FAHRENHEIT);
        maximumWaterTemp =
            prefs.getFloat("prefs_max_water_temp", AdaptivePreferences.MAX_WATER_TEMP_FAHRENHEIT);

        iatNeedle.setPivotPoint(.5f);
        iatNeedle.setMinValue(0);
        iatNeedle.setMaxValue(200);
        iatNeedle.setMinDegrees(-180);
        iatNeedle.setMaxDegrees(90);

        imgIat.setImageResource(R.drawable.iatgauge);
        break;
    }

    afrAlarmLogging = prefs.getBoolean("prefs_afr_alarm_logging", false);
    if (menuShareLog != null) {
      menuShareLog.setVisible(afrAlarmLogging && !afrAlarmLogItems.getItems().isEmpty());
    }

    // ensure both fragments are hidden
    FragmentTransaction ft = getFragmentManager().beginTransaction();
    ft.hide(adaptiveFragment);
    ft.hide(gaugesFragment);
    ft.hide(fuelFragment);
    ft.commit();

    ActionBar bar = getActionBar();
    bar.selectTab(bar.getTabAt(prefs.getInt("prefs_last_tab", 1)));

    // EVAN
    String action = getIntent().getAction();

    if (Intent.ACTION_MAIN.equals(action) || UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)) {
      if (DEBUG_MODE) Log.d(TAG, "USB Device Attached");

      connected = UsbConnectedThread.checkConnectedUsbDevice(this, connectionHandler);

      if (connected != null) {
        lastUpdateInMillis = System.currentTimeMillis();
      }
    }
    // END EVAN

    if (connected != null && connected.isAlive()) {
      refreshHandler.postDelayed(RefreshRunnable, LONG_PAUSE);
    } else {
      if (autoConnect && remoteMacAddr.length() > 0) {
        disconnect();
        new Handler()
            .postDelayed(
                (new Runnable() {
                  public void run() {
                    connect(remoteName, remoteMacAddr);
                  }
                }),
                1000);
        //        		connect(remoteName,  remoteMacAddr);
      }
    }
  }
  /** 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"));

          }
        });
  }