Exemplo n.º 1
0
 /** Stop all threads */
 public synchronized void stop() {
   Log.d(TAG, "stop");
   disconnect(false);
   if (mBtAdapter != null) {
     mContext.getServiceInstance().unregisterReceiver(mReceiver);
   }
 }
Exemplo n.º 2
0
  /**
   * Constructor. Prepares a new BluetoothChat session.
   *
   * @param context TruckMixContext
   * @param connectionStateListener listener for conntection state
   * @param loggerListener listener for logs
   */
  public BluetoothChatService(
      TruckMixService.TruckMixContext context,
      ConnectionStateListener connectionStateListener,
      LoggerListener loggerListener) {
    mBtAdapter = BluetoothAdapter.getDefaultAdapter();
    mState = STATE_NONE;

    mContext = context;
    mConnectionStateListener = connectionStateListener;
    mLoggerListener = loggerListener;

    if (mBtAdapter == null) {
      Log.e(TAG, "Bluetooth is not supported on this device");
    } else {
      if (!mBtAdapter.isEnabled()) {
        mBtAdapter.enable();
      }
      mContext
          .getServiceInstance()
          .registerReceiver(mReceiver, new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED));
    }
  }
Exemplo n.º 3
0
  /**
   * Set the current state of the chat connection
   *
   * @param state An integer defining the current connection state
   */
  private synchronized void setState(int state, boolean postOnCommunicatorQueue) {
    Log.d(TAG, "setState() " + mState + " -> " + state);
    mState = state;

    // Give the new state to the Handler so the UI Activity can update
    switch (state) {
      case STATE_NONE:
        mLoggerListener.log("BLUETOOTH: disconnected");
        mConnectionStateListener.onCalculatorDisconnected();
        mContext.getServiceInstance().displayNotification(false);
        if (postOnCommunicatorQueue) {
          mContext.post(
              new Runnable() {
                @Override
                public void run() {
                  mContext.getCommunicatorInstance().setConnected(false);
                }
              });
        } else {
          mContext.getCommunicatorInstance().setConnected(false);
        }
        break;
      case STATE_CONNECTING:
        mLoggerListener.log("BLUETOOTH: connecting to " + mDeviceAddress);
        mConnectionStateListener.onCalculatorConnecting();
        break;
      case STATE_CONNECTED:
        mLoggerListener.log("BLUETOOTH: connected");
        mConnectionStateListener.onCalculatorConnected();
        mContext.getServiceInstance().displayNotification(true);
        if (postOnCommunicatorQueue) {
          mContext.post(
              new Runnable() {
                @Override
                public void run() {
                  mContext.getCommunicatorInstance().setConnected(true);
                }
              });
        } else {
          mContext.getCommunicatorInstance().setConnected(true);
        }
        break;
    }
  }