int processState() {
    int result = 0;
    switch (connectionState) {
      case ConnectionState.DISCONNECTED:
        if (Preferences.logging) Log.d(MetaWatch.TAG, "state: disconnected");
        break;
      case ConnectionState.CONNECTING:
        if (Preferences.logging) Log.d(MetaWatch.TAG, "state: connecting");
        // create initial connection or reconnect
        updateNotification();
        connect(MetaWatchService.this);
        if (powerManager.isScreenOn()) {
          result = 10000; // try to reconnect in 10s
        } else {
          result = 30000; // try to reconnect in 30s		
        }
        break;
      case ConnectionState.CONNECTED:
        if (Preferences.logging) Log.d(MetaWatch.TAG, "state: connected");
        // read from input stream
        readFromDevice();
        break;
      case ConnectionState.DISCONNECTING:
        if (Preferences.logging) Log.d(MetaWatch.TAG, "state: disconnecting");
        // exit
        result = -1;
        break;
    }

    return result;
  }
 void disconnectExit() {
   connectionState = ConnectionState.DISCONNECTING;
   updateNotification();
   disconnect();
 }
  @TargetApi(10)
  void connect(Context context) {

    try {

      MetaWatchService.fakeWatch = false;
      if (Preferences.watchMacAddress.equals("DIGITAL")) {
        MetaWatchService.fakeWatch = true;
        MetaWatchService.watchType = MetaWatchService.WatchType.DIGITAL;
      }
      if (Preferences.watchMacAddress.equals("ANALOG")) {
        MetaWatchService.fakeWatch = true;
        MetaWatchService.watchType = MetaWatchService.WatchType.ANALOG;
      }

      if (Preferences.logging)
        Log.d(MetaWatch.TAG, "Remote device address: " + Preferences.watchMacAddress);
      if (!Preferences.loaded) loadPreferences(context);

      if (!MetaWatchService.fakeWatch) {

        if (bluetoothAdapter == null) {
          sendToast(getResources().getString(R.string.error_bluetooth_not_supported));
          return;
        } else if (!bluetoothAdapter.isEnabled()) {
          sendToast(getResources().getString(R.string.error_bluetooth_not_enabled));
          return;
        }

        wakeLock.acquire(5000);

        BluetoothDevice bluetoothDevice =
            bluetoothAdapter.getRemoteDevice(Preferences.watchMacAddress);

        if (Preferences.skipSDP) {
          Method method =
              bluetoothDevice.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
          bluetoothSocket = (BluetoothSocket) method.invoke(bluetoothDevice, 1);
        } else {
          UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");

          int currentapiVersion = android.os.Build.VERSION.SDK_INT;

          if (Preferences.insecureBtSocket
              && currentapiVersion >= android.os.Build.VERSION_CODES.GINGERBREAD_MR1) {
            bluetoothSocket = bluetoothDevice.createInsecureRfcommSocketToServiceRecord(uuid);
          } else {
            bluetoothSocket = bluetoothDevice.createRfcommSocketToServiceRecord(uuid);
          }
        }

        bluetoothAdapter.cancelDiscovery();
        bluetoothSocket.connect();

        inputStream = bluetoothSocket.getInputStream();
        outputStream = bluetoothSocket.getOutputStream();
      }

      connectionState = ConnectionState.CONNECTED;
      updateNotification();

      Protocol.startProtocolSender();

      // RM: This is disabled for now, as it seems to confuse the watch fw (3.1.0S tested)
      // and get it into a state where it won't accept any date/time format updates :-S

      // if( Preferences.autoClockFormat )
      //	Protocol.setTimeDateFormat(this);

      Protocol.getRealTimeClock();
      Protocol.getDeviceType();

      Notification.startNotificationSender(this);

      Idle.updateIdle(this, true);

    } catch (IOException ioexception) {
      if (Preferences.logging) Log.d(MetaWatch.TAG, ioexception.toString());
    } catch (SecurityException e) {
      if (Preferences.logging) Log.d(MetaWatch.TAG, e.toString());
    } catch (NoSuchMethodException e) {
      if (Preferences.logging) Log.d(MetaWatch.TAG, e.toString());
    } catch (IllegalArgumentException e) {
      if (Preferences.logging) Log.d(MetaWatch.TAG, e.toString());
    } catch (IllegalAccessException e) {
      if (Preferences.logging) Log.d(MetaWatch.TAG, e.toString());
    } catch (InvocationTargetException e) {
      if (Preferences.logging) Log.d(MetaWatch.TAG, e.toString());
    } catch (NullPointerException e) {
      if (Preferences.logging) Log.d(MetaWatch.TAG, e.toString());
    } finally {
      if (wakeLock != null && wakeLock.isHeld()) {
        wakeLock.release();
      }
    }

    return;
  }