Java Code Examples for android.bluetooth.BluetoothDevice#equals()

The following examples show how to use android.bluetooth.BluetoothDevice#equals() . 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: HidDataSender.java    From wearmouse with Apache License 2.0 6 votes vote down vote up
/**
 * Initiate connection sequence for the specified HID Host. If another device is already
 * connected, it will be disconnected first. If the parameter is {@code null}, then the service
 * will only disconnect from the current device.
 *
 * @param device New HID Host to connect to or {@code null} to disconnect.
 */
@MainThread
public void requestConnect(BluetoothDevice device) {
    synchronized (lock) {
        waitingForDevice = device;
        if (!isAppRegistered) {
            // Request will be fulfilled as soon the as app becomes registered.
            return;
        }

        connectedDevice = null;
        updateDeviceList();

        if (device != null && device.equals(connectedDevice)) {
            for (ProfileListener listener : listeners) {
                listener.onConnectionStateChanged(device, BluetoothProfile.STATE_CONNECTED);
            }
        }
    }
}
 
Example 2
Source File: SerialSocket.java    From SimpleBluetoothLeTerminal with MIT License 6 votes vote down vote up
private void onPairingBroadcastReceive(Context context, Intent intent) {
    // for ARM Mbed, Microbit, ... use pairing from Android bluetooth settings
    // for HM10-clone, ... pairing is initiated here
    BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
    if(device==null || !device.equals(this.device))
        return;
    switch (intent.getAction()) {
        case BluetoothDevice.ACTION_PAIRING_REQUEST:
            final int pairingVariant = intent.getIntExtra(BluetoothDevice.EXTRA_PAIRING_VARIANT, -1);
            Log.d(TAG, "pairing request " + pairingVariant);
            onSerialConnectError(new IOException(context.getString(R.string.pairing_request)));
            // pairing dialog brings app to background (onPause), but it is still partly visible (no onStop), so there is no automatic disconnect()
            break;
        case BluetoothDevice.ACTION_BOND_STATE_CHANGED:
            final int bondState = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, -1);
            final int previousBondState = intent.getIntExtra(BluetoothDevice.EXTRA_PREVIOUS_BOND_STATE, -1);
            Log.d(TAG, "bond state " + previousBondState + "->" + bondState);
            break;
        default:
            Log.d(TAG, "unknown broadcast " + intent.getAction());
            break;
    }
}
 
Example 3
Source File: EddystoneGattServer.java    From beacons-android with Apache License 2.0 6 votes vote down vote up
@Override
public void onConnectionStateChange(BluetoothDevice device, int status, int newState) {
    super.onConnectionStateChange(device, status, newState);

    if (newState == BluetoothGatt.STATE_DISCONNECTED) {
        log(device + " has disconnected");
        if (device.equals(mEddystoneGattService.getConnectedOwner())) {
            log("Owner disconnected, stopping GATT server");
            mEddystoneGattService.onOwnerDisconnected();
            close();
        }
    }
    else if (newState == BluetoothGatt.STATE_CONNECTED) {
        log(device + " has connected");
        if (mEddystoneGattService.getConnectedOwner() != null) {
            // don't allow a second client to connect at the same time
            log(device + " tried to connect, but owner is active. Disconnecting.");
            mGattServer.cancelConnection(device);
        }
    }
}
 
Example 4
Source File: HidDataSender.java    From wearmouse with Apache License 2.0 5 votes vote down vote up
@MainThread
private void updateDeviceList() {
    synchronized (lock) {
        BluetoothDevice connected = null;

        // If we are connected to some device, but want to connect to another (or disconnect
        // completely), then we should disconnect all other devices first.
        for (BluetoothDevice device : hidDeviceProfile.getConnectedDevices()) {
            if (device.equals(waitingForDevice) || device.equals(connectedDevice)) {
                connected = device;
            } else {
                hidDeviceProfile.disconnect(device);
            }
        }

        // If there is nothing going on, and we want to connect, then do it.
        if (hidDeviceProfile
                        .getDevicesMatchingConnectionStates(
                                new int[] {
                                    BluetoothProfile.STATE_CONNECTED,
                                    BluetoothProfile.STATE_CONNECTING,
                                    BluetoothProfile.STATE_DISCONNECTING
                                })
                        .isEmpty()
                && waitingForDevice != null) {
            hidDeviceProfile.connect(waitingForDevice);
        }

        if (connectedDevice == null && connected != null) {
            connectedDevice = connected;
            waitingForDevice = null;
        } else if (connectedDevice != null && connected == null) {
            connectedDevice = null;
        }
        hidDeviceApp.setDevice(connectedDevice);
    }
}
 
Example 5
Source File: BleServerManager.java    From Android-BLE-Library with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Nullable
private BleManagerHandler getRequestHandler(@NonNull final BluetoothDevice device) {
	for (final BleManager manager : managers) {
		if (device.equals(manager.getBluetoothDevice())) {
			return manager.requestHandler;
		}
	}
	return null;
}
 
Example 6
Source File: EddystoneGattServer.java    From beacons-android with Apache License 2.0 5 votes vote down vote up
void disconnectAll(BluetoothDevice allowedDevice) {
    for (BluetoothDevice device : mBluetoothManager.getConnectedDevices(BluetoothProfile.GATT)) {
        if (!device.equals(allowedDevice)) {
            log(String.format("Disconnecting %s", device));
            mGattServer.cancelConnection(device);
        }
    }
}
 
Example 7
Source File: BleProfileServiceReadyActivity.java    From Android-nRF-Toolbox with BSD 3-Clause "New" or "Revised" License 2 votes vote down vote up
/**
 * Checks the {@link BleProfileService#EXTRA_DEVICE} in the given intent and compares it with the connected BluetoothDevice object.
 * @param intent intent received via a broadcast from the service
 * @return true if the data in the intent apply to the connected device, false otherwise
 */
protected boolean isBroadcastForThisDevice(final Intent intent) {
	final BluetoothDevice bluetoothDevice = intent.getParcelableExtra(BleProfileService.EXTRA_DEVICE);
	return bluetoothDevice != null && bluetoothDevice.equals(bluetoothDevice);
}