/** * 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; } }
@Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) { if (mBtAdapter.getState() == BluetoothAdapter.STATE_OFF) { mLoggerListener.log("BLUETOOTH: bluetooth was turned off -> re-enable it"); // Cancel any thread and timer cancelTimer(); cancelThreads(); // Re-enable bluetooth mBtAdapter.enable(); } else if (mBtAdapter.getState() == BluetoothAdapter.STATE_ON) { mLoggerListener.log("BLUETOOTH: bluetooth is on"); if (mDeviceAddress != null) { connect(mDeviceAddress); } } } }