@Override
    public synchronized void stop() {
        if (D) Log.d(TAG, "stop");

        for (ConnectThread ct : connectionAttempts.values()) ct.cancel();
        connectionAttempts.clear();

        for (ConnectedThread ct : remoteConnections.values()) ct.cancel();
        remoteConnections.clear();

        if (mSecureBluetoothServer != null) {
            mSecureBluetoothServer.cancel();
            mSecureBluetoothServer = null;
        }

        if (mInsecureBluetoothServer != null) {
            mInsecureBluetoothServer.cancel();
            mInsecureBluetoothServer = null;
        }
        setState(STATE_NONE);
    }