Java Code Examples for android.bluetooth.BluetoothProfile#STATE_DISCONNECTING

The following examples show how to use android.bluetooth.BluetoothProfile#STATE_DISCONNECTING . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BaseMyo.java    From myolib with Apache License 2.0 6 votes vote down vote up
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
    if (newState == BluetoothProfile.STATE_CONNECTING) {
        mConnectionState = ConnectionState.CONNECTING;
    } else if (newState == BluetoothProfile.STATE_CONNECTED) {
        mConnectionState = ConnectionState.CONNECTED;
        Logy.d(TAG, "Device connected, discovering services...");
        gatt.discoverServices();
    } else if (newState == BluetoothProfile.STATE_DISCONNECTING) {
        mConnectionState = ConnectionState.DISCONNECTING;
        mWaitToken.drainPermits();
    } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
        mConnectionState = ConnectionState.DISCONNECTED;
    } else {
        throw new RuntimeException("Unknown connection state");
    }
    Logy.d(TAG, "status:" + status + ", newState:" + mConnectionState.name());
    for (ConnectionListener listener : mConnectionListeners)
        listener.onConnectionStateChanged(this, mConnectionState);
    super.onConnectionStateChange(gatt, status, newState);
}
 
Example 2
Source File: BluetoothPeripheral.java    From blessed-android with MIT License 6 votes vote down vote up
private void successfullyDisconnected(int previousState) {
    if (previousState == BluetoothProfile.STATE_CONNECTED || previousState == BluetoothProfile.STATE_DISCONNECTING) {
        Timber.i("disconnected '%s' on request", getName());
    } else if (previousState == BluetoothProfile.STATE_CONNECTING) {
        Timber.i("cancelling connect attempt");
    }

    if (bondLost) {
        completeDisconnect(false, GATT_SUCCESS);
        if (listener != null) {
            // Consider the loss of the bond a connection failure so that a connection retry will take place
            callbackHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    listener.connectFailed(BluetoothPeripheral.this, GATT_SUCCESS);
                }
            }, DELAY_AFTER_BOND_LOST); // Give the stack some time to register the bond loss internally. This is needed on most phones...
        }
    } else {
        completeDisconnect(true, GATT_SUCCESS);
    }
}
 
Example 3
Source File: BaseBleService.java    From bleYan with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
	BleLog.i(TAG, "onConnectionStateChange: State = " + BleUtils.getBleConnectStatus(status)
			+ " newState = " + BleUtils.getBleConnectStatus(newState));

	if (newState == BluetoothProfile.STATE_CONNECTED) {
		updateState(BleConnectState.CONNECTED);
		//start discoverServices
		BleLog.i(TAG, "gatt.discoverServices()");
		gatt.discoverServices();
	} else if (newState == BluetoothProfile.STATE_CONNECTING) {
		updateState(BleConnectState.CONNECTING);
	} else if (newState == BluetoothProfile.STATE_DISCONNECTING) {
		updateState(BleConnectState.DISCONNECTING);
	} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
		//disconnect
		sIsWriting = false;
		sWriteQueue.clear();
		updateState(BleConnectState.DISCONNECTED);
          }
}
 
Example 4
Source File: BluetoothPeripheral.java    From blessed-android with MIT License 6 votes vote down vote up
/**
 * Disconnect the bluetooth peripheral.
 *
 * <p>When the disconnection has been completed {@link BluetoothCentralCallback#onDisconnectedPeripheral(BluetoothPeripheral, int)} will be called.
 */
private void disconnect() {
    if (state == BluetoothProfile.STATE_CONNECTED || state == BluetoothProfile.STATE_CONNECTING) {
        this.state = BluetoothProfile.STATE_DISCONNECTING;
        mainHandler.post(new Runnable() {
            @Override
            public void run() {
                if (bluetoothGatt != null) {
                    Timber.i("force disconnect '%s' (%s)", getName(), getAddress());
                    bluetoothGatt.disconnect();
                }
            }
        });
    } else {
        if (listener != null) {
            listener.disconnected(BluetoothPeripheral.this, GATT_CONN_TERMINATE_LOCAL_HOST);
        }
    }
}
 
Example 5
Source File: BleUtils.java    From bleYan with GNU General Public License v2.0 6 votes vote down vote up
public static String getBleConnectStatus(int status) {
    switch (status) {
        case BluetoothProfile.STATE_DISCONNECTED:
            return "STATE_DISCONNECTED";

        case BluetoothProfile.STATE_CONNECTING:
            return "STATE_CONNECTING";

        case BluetoothProfile.STATE_CONNECTED:
            return "STATE_CONNECTED";

        case BluetoothProfile.STATE_DISCONNECTING:
            return "STATE_DISCONNECTING";

        default:
            return "STATE_UNKNOWN: " + status;
    }
}
 
Example 6
Source File: NukiCallback.java    From trigger with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
    //Log.i(TAG, "status: " + getGattStatus(status) + ", newState: " + newState);

    if (status == GATT_SUCCESS) {
        switch (newState) {
            case BluetoothProfile.STATE_CONNECTED:
                gatt.discoverServices();
                break;
            case BluetoothProfile.STATE_DISCONNECTED:
                closeConnection(gatt);
                break;
            case BluetoothProfile.STATE_CONNECTING:
            case BluetoothProfile.STATE_DISCONNECTING:
                break;
        }
    } else {
        closeConnection(gatt);
        this.listener.onTaskResult(
            setup_id, ReplyCode.REMOTE_ERROR, "Connection error: " + NukiRequestHandler.getGattStatus(status)
        );
    }
}
 
Example 7
Source File: MyBleService.java    From science-journal with Apache License 2.0 6 votes vote down vote up
public void disconnectDevice(String address) {
  BluetoothGatt bluetoothGatt = addressToGattClient.get(address);
  if (btAdapter == null || address == null || bluetoothGatt == null) {
    // Broadcast the disconnect so BleFlow doesn't hang waiting for it; something else
    // already disconnected us in this case.
    sendGattBroadcast(address, BleEvents.GATT_DISCONNECT, null);
    return;
  }
  BluetoothDevice device = btAdapter.getRemoteDevice(address);
  int bleState = bluetoothManager.getConnectionState(device, BluetoothProfile.GATT);
  if (bleState != BluetoothProfile.STATE_DISCONNECTED
      && bleState != BluetoothProfile.STATE_DISCONNECTING) {
    bluetoothGatt.disconnect();
  } else {
    bluetoothGatt.close();
    addressToGattClient.remove(address);
    sendGattBroadcast(address, BleEvents.GATT_DISCONNECT, null);
  }
}
 
Example 8
Source File: ParserUtils.java    From Android-BLE-Library with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Converts the connection state to String value.
 *
 * @param state the connection state
 * @return state as String
 */
@NonNull
public static String stateToString(@ConnectionState final int state) {
	switch (state) {
		case BluetoothProfile.STATE_CONNECTED:
			return "CONNECTED";
		case BluetoothProfile.STATE_CONNECTING:
			return "CONNECTING";
		case BluetoothProfile.STATE_DISCONNECTING:
			return "DISCONNECTING";
		case BluetoothProfile.STATE_DISCONNECTED:
			return "DISCONNECTED";
		default:
			return "UNKNOWN (" + state + ")";
	}
}
 
Example 9
Source File: PBluetoothLEClientBak.java    From PHONK with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {

    if (newState == BluetoothProfile.STATE_DISCONNECTED) {
        mConnectionStatus = DISCONNECTED;
    } else if (newState == BluetoothProfile.STATE_CONNECTING) {
        mConnectionStatus = CONNECTING;
    } else if (newState == BluetoothProfile.STATE_CONNECTED) {
        mConnectionStatus = CONNECTED;
    } else if (newState == BluetoothProfile.STATE_DISCONNECTING) {
        mConnectionStatus = DISCONNECTING;
    }

    MLog.d(TAG, "connected " + mConnectionStatus);

    if (mCallbackGattConnection != null) {
        ReturnObject o = new ReturnObject();
        o.put("status", mConnectionStatus);
        o.put("gatt", gatt);
        mCallbackGattConnection.event(o);
    }
}
 
Example 10
Source File: BtPairStateMachine.java    From apollo-DuerOS with Apache License 2.0 6 votes vote down vote up
@Override
public boolean processMessage(Message msg) {
    switch(msg.what) {
        case EVENT_MD_READY:
            int connState = getHfpConnectionState();
            if (connState == BluetoothProfile.STATE_DISCONNECTED) {
                transitionTo(mPairDisconnectedState);
            } else if (connState == BluetoothProfile.STATE_CONNECTED) {
                deferMessage(obtainMessage(EVENT_CHECK_HFP_CONNECTION));
                transitionTo(mPairConnectedState);
            } else if (connState == BluetoothProfile.STATE_DISCONNECTING) {
                sendMessageDelayed(EVENT_MD_READY, 2000);
            } else if (connState == BluetoothProfile.STATE_CONNECTING) {
                sendMessageDelayed(EVENT_MD_READY, 2000);
            } else {
                deferMessage(obtainMessage(EVENT_ON_ERROR, ERROR_CHECK_CONNECTION, 0));
                transitionTo(mPairErrorState);
            }
            return HANDLED;
        default:
            return NOT_HANDLED;
    }
}
 
Example 11
Source File: SerialInterface_BLE.java    From PodEmu with GNU General Public License v3.0 5 votes vote down vote up
@Override
@TargetApi(21)
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState)
{
    PodEmuLog.debug("SIBLE: GATT onConnectionStateChange");
    switch (newState)
    {
        case BluetoothProfile.STATE_CONNECTED:
            setState(STATE_CONNECTED);
            gatt.discoverServices();
            synchronized (SerialInterface_BLE.this)
            {
                SerialInterface_BLE.this.notifyAll();
            }
            break;
        case BluetoothProfile.STATE_CONNECTING:
            setState(STATE_CONNECTING);
            break;
        case BluetoothProfile.STATE_DISCONNECTING:
            setState(STATE_LOST);
            break;
        case BluetoothProfile.STATE_DISCONNECTED:
            setState(STATE_NONE);
            break;
    }
    //PodEmuService.communicateSerialStatusChange();
}
 
Example 12
Source File: NotificationService.java    From wearmouse with Apache License 2.0 5 votes vote down vote up
private String getStateName(int state) {
    switch (state) {
        case BluetoothProfile.STATE_CONNECTED:
            return getString(R.string.pref_bluetooth_connected);
        case BluetoothProfile.STATE_CONNECTING:
            return getString(R.string.pref_bluetooth_connecting);
        case BluetoothProfile.STATE_DISCONNECTING:
            return getString(R.string.pref_bluetooth_disconnecting);
        case BluetoothProfile.STATE_DISCONNECTED:
            return getString(R.string.pref_bluetooth_disconnected);
        default:
            return getString(R.string.pref_bluetooth_unavailable);
    }
}
 
Example 13
Source File: MultipleBleService.java    From BleLib with Apache License 2.0 5 votes vote down vote up
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
    if (mOnConnectionStateChangeListener != null) {
        mOnConnectionStateChangeListener.onConnectionStateChange(gatt, status, newState);
    }
    String intentAction;
    String tmpAddress = gatt.getDevice().getAddress();
    if (newState == BluetoothProfile.STATE_DISCONNECTED) {
        intentAction = ACTION_GATT_DISCONNECTED;
        Log.i(TAG, "Disconnected from GATT server.");
        broadcastUpdate(intentAction, tmpAddress);
        close(tmpAddress);
    } else if (newState == BluetoothProfile.STATE_CONNECTING) {
        intentAction = ACTION_GATT_CONNECTING;
        Log.i(TAG, "Connecting to GATT server.");
        broadcastUpdate(intentAction, tmpAddress);
    } else if (newState == BluetoothProfile.STATE_CONNECTED) {
        mConnectedAddressList.add(tmpAddress);
        intentAction = ACTION_GATT_CONNECTED;
        broadcastUpdate(intentAction, tmpAddress);
        Log.i(TAG, "Connected to GATT server.");
        // Attempts to discover services after successful connection.
        Log.i(TAG, "Attempting to start service discovery:" +
                mBluetoothGattMap.get(tmpAddress).discoverServices());
    } else if (newState == BluetoothProfile.STATE_DISCONNECTING) {
        mConnectedAddressList.remove(tmpAddress);
        intentAction = ACTION_GATT_DISCONNECTING;
        Log.i(TAG, "Disconnecting from GATT server.");
        broadcastUpdate(intentAction, tmpAddress);
    }
}
 
Example 14
Source File: ConnectionStateChangeLog.java    From EFRConnect-android with Apache License 2.0 5 votes vote down vote up
private String parseNewState(int newState) {
    switch (newState) {
        case BluetoothProfile.STATE_DISCONNECTED:
            return "State Disconnected";
        case BluetoothProfile.STATE_CONNECTING:
            return "State Connecting";
        case BluetoothProfile.STATE_CONNECTED:
            return "Successful connect to device";
        case BluetoothProfile.STATE_DISCONNECTING:
            return "State Disconnecting";
        default:
            return String.valueOf(newState);
    }
}
 
Example 15
Source File: DeviceActivity.java    From BLERW with Apache License 2.0 5 votes vote down vote up
@Override
protected void onDestroy() {
	super.onDestroy();
	if (mConnGatt != null) {
		if ((mStatus != BluetoothProfile.STATE_DISCONNECTING)
				&& (mStatus != BluetoothProfile.STATE_DISCONNECTED)) {
			mConnGatt.disconnect();
		}
		mConnGatt.close();
		mConnGatt = null;
	}
}
 
Example 16
Source File: BluetoothPeripheral.java    From blessed-android with MIT License 5 votes vote down vote up
private String stateToString(final int state) {
    switch (state) {
        case BluetoothProfile.STATE_CONNECTED:
            return "CONNECTED";
        case BluetoothProfile.STATE_CONNECTING:
            return "CONNECTING";
        case BluetoothProfile.STATE_DISCONNECTING:
            return "DISCONNECTING";
        default:
            return "DISCONNECTED";
    }
}
 
Example 17
Source File: BluetoothPeripheral.java    From blessed-android with MIT License 5 votes vote down vote up
/**
 * Cancel an active or pending connection.
 * <p>
 * This operation is asynchronous and you will receive a callback on onDisconnectedPeripheral.
 */
public void cancelConnection() {
    // Check if we have a Gatt object
    if (bluetoothGatt == null) {
        return;
    }

    // Check if we are not already disconnected or disconnecting
    if (state == BluetoothProfile.STATE_DISCONNECTED || state == BluetoothProfile.STATE_DISCONNECTING) {
        return;
    }

    // Cancel the connection timer
    cancelConnectionTimer();

    // Check if we are in the process of connecting
    if (state == BluetoothProfile.STATE_CONNECTING) {
        // Cancel the connection by calling disconnect
        disconnect();

        // Since we will not get a callback on onConnectionStateChange for this, we complete the disconnect ourselves
        mainHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                completeDisconnect(true, GATT_SUCCESS);
            }
        }, 50);
    } else {
        // Cancel active connection
        disconnect();
    }
}
 
Example 18
Source File: BluetoothDevicePreference.java    From wearmouse with Apache License 2.0 5 votes vote down vote up
/**
 * Update the preference summary with the profile connection state
 *
 * <p>However, if no profiles are supported from the target device we indicate that this target
 * device is unavailable.
 */
void updateProfileConnectionState() {
    connectionState = hidDeviceProfile.getConnectionState(device);
    if (!hidDeviceProfile.isProfileSupported(device)) {
        setEnabled(false);
        setSummary(R.string.pref_bluetooth_unavailable);
    } else {
        switch (connectionState) {
            case BluetoothProfile.STATE_CONNECTED:
                setEnabled(true);
                setSummary(R.string.pref_bluetooth_connected);
                break;
            case BluetoothProfile.STATE_CONNECTING:
                setEnabled(false);
                setSummary(R.string.pref_bluetooth_connecting);
                break;
            case BluetoothProfile.STATE_DISCONNECTING:
                setEnabled(false);
                setSummary(R.string.pref_bluetooth_disconnecting);
                break;
            case BluetoothProfile.STATE_DISCONNECTED:
                setEnabled(true);
                setSummary(R.string.pref_bluetooth_disconnected);
                break;
            default: // fall out
        }
    }
    notifyChanged();
}
 
Example 19
Source File: BleService.java    From BleLib with Apache License 2.0 4 votes vote down vote up
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
    if (mOnConnectionStateChangeListener != null) {
        mOnConnectionStateChangeListener.onConnectionStateChange(gatt, status, newState);
    }
    String intentAction;
    String address = gatt.getDevice().getAddress();
    if (newState == BluetoothProfile.STATE_DISCONNECTED) {
        Log.i(TAG, "onConnectionStateChange: DISCONNECTED: " + getConnectDevices().size());
        intentAction = ACTION_GATT_DISCONNECTED;
        isConnect = false;
        mConnState = STATE_DISCONNECTED;
        Log.i(TAG, "Disconnected from GATT server.");
        broadcastUpdate(intentAction, address);
        close();
    } else if (newState == BluetoothProfile.STATE_CONNECTING) {
        Log.i(TAG, "onConnectionStateChange: CONNECTING: " + getConnectDevices().size());
        isConnect = false;
        intentAction = ACTION_GATT_CONNECTING;
        mConnState = STATE_CONNECTING;
        Log.i(TAG, "Connecting to GATT server.");
        broadcastUpdate(intentAction, address);
    } else if (newState == BluetoothProfile.STATE_CONNECTED) {
        Log.i(TAG, "onConnectionStateChange: CONNECTED: " + getConnectDevices().size());
        intentAction = ACTION_GATT_CONNECTED;
        isConnect = true;
        mConnState = STATE_CONNECTED;
        broadcastUpdate(intentAction, address);
        Log.i(TAG, "Connected to GATT server.");
        // Attempts to discover services after successful connection.
        Log.i(TAG, "Attempting to start service discovery:" +
                mBluetoothGatt.discoverServices());
    } else if (newState == BluetoothProfile.STATE_DISCONNECTING) {
        Log.i(TAG, "onConnectionStateChange: DISCONNECTING: " + getConnectDevices().size());
        isConnect = false;
        intentAction = ACTION_GATT_DISCONNECTING;
        mConnState = STATE_DISCONNECTING;
        Log.i(TAG, "Disconnecting from GATT server.");
        broadcastUpdate(intentAction, address);
    }
}
 
Example 20
Source File: BluetoothPeripheral.java    From blessed-android with MIT License 4 votes vote down vote up
@Override
public void onConnectionStateChange(final BluetoothGatt gatt, final int status, final int newState) {
    long timePassed = SystemClock.elapsedRealtime() - connectTimestamp;
    cancelConnectionTimer();
    final int previousState = state;
    state = newState;

    if (status == GATT_SUCCESS) {
        switch (newState) {
            case BluetoothProfile.STATE_CONNECTED:
                successfullyConnected(device.getBondState(), timePassed);
                break;
            case BluetoothProfile.STATE_DISCONNECTED:
                successfullyDisconnected(previousState);
                break;
            case BluetoothProfile.STATE_DISCONNECTING:
                Timber.i("peripheral is disconnecting");
                break;
            case BluetoothProfile.STATE_CONNECTING:
                Timber.i("peripheral is connecting");
            default:
                Timber.e("unknown state received");
                break;
        }
    } else {
        connectionStateChangeUnsuccessful(status, previousState, newState, timePassed);
    }
}