Esempio n. 1
0
 private void connectMiBand() {
   miBand = MiBand.getInstance(MiBandService.this);
   if (!miBand.isConnected()) {
     miBand.connect(connectionAction);
   } else {
     Log.d(TAG, "Already connected with Mi Band!");
     createOngoingNotification();
     broadcastUpdate(NotificationConstants.MI_BAND_CONNECT);
   }
 }
Esempio n. 2
0
        @Override
        public void onReceive(Context context, Intent intent) {
          Bundle b = intent.getExtras();

          String action = b.getString("type");

          // Log.i(TAG, "action received: " + action);
          // Log.i(TAG, "miBand.isConnected(): " + miBand.isConnected());

          switch (action) {
            case NotificationConstants.MI_BAND_CONNECT:
              connectMiBand();
              break;
            case NotificationConstants.MI_BAND_LIGHTS:
              if (b.containsKey(NotificationConstants.KEY_COLOR)) {

                int color = b.getInt(NotificationConstants.KEY_COLOR, 255);

                if (b.containsKey(NotificationConstants.KEY_TIMES)) {
                  // with flashing
                  int flash_time = b.getInt(NotificationConstants.KEY_TIMES, 3);
                  int pause_time = b.getInt(NotificationConstants.KEY_PAUSE_TIME, 1000);

                  miBand.setLedColor(flash_time, color, pause_time);
                } else {
                  // normal flashing
                  miBand.setLedColor(color);
                }
              }
              break;
            case NotificationConstants.MI_BAND_VIBRATE_WITH_LED:
              miBand.startVibration(VibrationMode.VIBRATION_WITH_LED);

              break;
            case NotificationConstants.MI_BAND_VIBRATE_UNTIL_CALL_STOP:
              miBand.startVibration(VibrationMode.VIBRATION_UNTIL_CALL_STOP);

              break;
            case NotificationConstants.MI_BAND_VIBRATE_WITHOUT_LED:
              miBand.startVibration(VibrationMode.VIBRATION_WITHOUT_LED);

              break;
            case NotificationConstants.MI_BAND_VIBRATE_CUSTOM:
              int times = b.getInt(NotificationConstants.KEY_TIMES, 3);
              int on_time = b.getInt(NotificationConstants.KEY_ON_TIME, 250);
              int off_time = b.getInt(NotificationConstants.KEY_PAUSE_TIME, 500);

              miBand.customVibration(times, on_time, off_time);

              break;
            case NotificationConstants.MI_BAND_NEW_NOTIFICATION:
              // if (b.containsKey(NotificationConstants.KEY_COLOR) &&
              // b.containsKey(NotificationConstants.KEY_PAUSE_TIME)) {
              int vibrate_times = -1;
              int flash_time = -1;
              int pause_time = -1;

              if (b.containsKey(NotificationConstants.KEY_TIMES))
                vibrate_times = b.getInt(NotificationConstants.KEY_TIMES, 3);

              if (b.containsKey(NotificationConstants.KEY_ON_TIME))
                flash_time = b.getInt(NotificationConstants.KEY_ON_TIME, 250);

              if (b.containsKey(NotificationConstants.KEY_PAUSE_TIME))
                pause_time = b.getInt(NotificationConstants.KEY_PAUSE_TIME, 500);

              int color = b.getInt(NotificationConstants.KEY_COLOR, 255);

              if (vibrate_times == -1 || flash_time == -1 || pause_time == -1)
                miBand.notifyBand(color);
              else miBand.notifyBand(vibrate_times, flash_time, pause_time, color);
              // }

              break;
            case NotificationConstants.MI_BAND_BATTERY:
              miBand.getBatteryInfo(
                  new ActionCallback() {
                    @Override
                    public void onSuccess(final Object data) {

                      BatteryInfo battery = (BatteryInfo) data;

                      broadcastUpdate(NotificationConstants.MI_BAND_BATTERY, battery);
                    }

                    @Override
                    public void onFail(int errorCode, String msg) {
                      Log.e(TAG, "Fail battery: " + msg);
                    }
                  });
              break;
            case NotificationConstants.MI_BAND_START_SYNC:
              miBand.startListeningSync(
                  new ActionCallback() {
                    @Override
                    public void onSuccess(Object data) {
                      if (data != null && data.equals("sync complete")) {
                        broadcastUpdate(NotificationConstants.MI_BAND_SYNC_SUCCESS);
                      } else {
                        broadcastUpdate(NotificationConstants.MI_BAND_SYNC_FAIL, "null data");
                      }
                    }

                    @Override
                    public void onFail(int errorCode, String msg) {
                      broadcastUpdate(NotificationConstants.MI_BAND_SYNC_FAIL, msg);
                    }
                  });
              break;
            case NotificationConstants.MI_BAND_STOP_SYNC:
              if (miBand.isSyncNotification()) miBand.stopListeningSync();
            case NotificationConstants.MI_BAND_REQUEST_CONNECTION:
              broadcastUpdate(
                  NotificationConstants.MI_BAND_REQUEST_CONNECTION,
                  miBand != null && miBand.isConnected());
              break;
          }
        }