Example #1
0
 @Override
 public void onReceive(Context arg0, Intent intent) {
   String action = intent.getAction();
   int iLoop = 0;
   if (BluetoothDevice.ACTION_UUID.equals(action)) {
     Parcelable[] uuidExtra =
         intent.getParcelableArrayExtra("android.bluetooth.device.extra.UUID");
     if (null != uuidExtra) iLoop = uuidExtra.length;
     /*uuidExtra should contain my service's UUID among his files, but it doesn't!!*/
     for (int i = 0; i < iLoop; i++) mslUuidList.add(uuidExtra[i].toString());
   }
 }
        @Override
        public void onReceive(Context context, Intent intent) {
          String action = intent.getAction();

          if (BluetoothDevice.ACTION_FOUND.equals(action)) {
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

            if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
              String entry =
                  device.getName()
                      + " ("
                      + getDeviceMajorClassName(device.getBluetoothClass().getMajorDeviceClass())
                      + ")"
                      + "\n"
                      + device.getAddress();

              if (mNewDevicesArrayAdapter.getPosition(entry) == -1)
                mNewDevicesArrayAdapter.add(entry);
            }
          } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
            setProgressBarIndeterminateVisibility(false);
            setTitle(R.string.text_select_device);
            findViewById(R.id.button_scan).setVisibility(View.VISIBLE);

            if (mNewDevicesArrayAdapter.getCount() == 0) {
              String noDevices = getResources().getText(R.string.text_no_new_devices).toString();
              mNewDevicesArrayAdapter.add(noDevices);
            }
          } else if (BluetoothDevice.ACTION_PAIRING_REQUEST.equals(action)) {
            int pairingVariant = intent.getIntExtra(BluetoothDevice.EXTRA_PAIRING_VARIANT, -1);

            if (pairingVariant == BluetoothDevice.PAIRING_VARIANT_PASSKEY_CONFIRMATION) {
              String pairingPasskey = intent.getStringExtra(BluetoothDevice.EXTRA_PAIRING_KEY);

              if (DBG) Log.d(TAG, "Passkey :" + pairingPasskey);

            } else if (pairingVariant == BluetoothDevice.PAIRING_VARIANT_PIN) {

              if (DBG) Log.d(TAG, "Pairing via PIN");
            }
          } else if (BluetoothDevice.ACTION_UUID.equals(action)) {

          } else if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)) {
            int prevBondState = intent.getIntExtra(BluetoothDevice.EXTRA_PREVIOUS_BOND_STATE, -1);
            int currBondState = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, -1);

            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

            // Unpaired
            if (prevBondState == BluetoothDevice.BOND_BONDED
                && currBondState == BluetoothDevice.BOND_NONE) {

              Toast.makeText(
                      getApplicationContext(), "Unpaired " + device.getName(), Toast.LENGTH_SHORT)
                  .show();

            } else if (prevBondState == BluetoothDevice.BOND_BONDING
                && currBondState == BluetoothDevice.BOND_BONDED) {

              Toast.makeText(
                      getApplicationContext(),
                      "Paired with " + device.getName(),
                      Toast.LENGTH_SHORT)
                  .show();

              String entry =
                  device.getName()
                      + " ("
                      + getDeviceMajorClassName(device.getBluetoothClass().getMajorDeviceClass())
                      + ")"
                      + "\n"
                      + device.getAddress();

              mNewDevicesArrayAdapter.remove(entry);
            }

            getPairedDevices();
          }
        }
  @Override
  public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub
    final String action = intent.getAction();

    DatabaseHandler dataEvent = DatabaseHandler.getInstance();
    dataEvent.setContext(context);

    btAdapter = BluetoothAdapter.getDefaultAdapter();
    String myDeviceMac = btAdapter.getAddress();

    if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) {
      final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);
      switch (state) {
        case BluetoothAdapter.STATE_OFF:
          Log.i(TAG, "Bluetooth OFF");

          break;
        case BluetoothAdapter.STATE_TURNING_OFF:
          Log.i(TAG, "Turning OFF Bluetooth...");

          break;
        case BluetoothAdapter.STATE_ON:
          Log.i(TAG, "Bluetooth ON");

          break;
        case BluetoothAdapter.STATE_TURNING_ON:
          Log.i(TAG, "Turning ON Bluetooth...");
          break;
      }
    } else {
      if (BluetoothDevice.ACTION_FOUND.equals(action)) {
        BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
        Log.d(TAG, "\n  Device: " + device.getName() + ", " + device);

        dataEvent
            .getInstance()
            .getDatabaseManager()
            .saveData(
                System.currentTimeMillis(),
                myDeviceMac,
                device.getName(),
                device.getAddress(),
                0,
                1,
                0,
                0,
                0);

      } else {
        if (BluetoothDevice.ACTION_UUID.equals(action)) {
          BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
          Parcelable[] uuidExtra = intent.getParcelableArrayExtra(BluetoothDevice.EXTRA_UUID);
          Log.d(TAG, "\n  UUID Device: " + device.getName() + ", " + device);
          if (uuidExtra != null) {}

        } else {
          if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {
            Log.d(TAG, "\nDiscovery Started");

          } else {
            if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
              Log.d(TAG, "\nDiscovery Finished");
            }
          }
        }
      }
    }
  }