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

The following examples show how to use android.bluetooth.BluetoothDevice#createBond() . 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: BleManager.java    From EasyBluetoothFrame with Apache License 2.0 7 votes vote down vote up
@Override
public void pin(BluetoothDevice device, PinResultListener resultListener) {
    if (device == null) {
        return;
    }
    mReceiver.setPinResultListener(resultListener);
    if (device.getBondState() == BluetoothDevice.BOND_NONE) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            device.createBond();
        } else {
            try {
                Method method = device.getClass().getMethod("createBond");
                method.invoke(device);
            } catch (Exception e) {
                e.printStackTrace();
                resultListener.pairFailed(device);
            }
        }
    }
}
 
Example 2
Source File: BluetoothController.java    From blue-pair with MIT License 6 votes vote down vote up
/**
 * Performs the device pairing.
 *
 * @param device the device to pair with.
 * @return true if the pairing was successful, false otherwise.
 */
public boolean pair(BluetoothDevice device) {
    // Stops the discovery and then creates the pairing.
    if (bluetooth.isDiscovering()) {
        Log.d(TAG, "Bluetooth cancelling discovery.");
        bluetooth.cancelDiscovery();
    }
    Log.d(TAG, "Bluetooth bonding with device: " + deviceToString(device));
    boolean outcome = device.createBond();
    Log.d(TAG, "Bounding outcome : " + outcome);

    // If the outcome is true, we are bounding with this device.
    if (outcome == true) {
        this.boundingDevice = device;
    }
    return outcome;
}
 
Example 3
Source File: InPenService.java    From xDrip with GNU General Public License v3.0 6 votes vote down vote up
@TargetApi(Build.VERSION_CODES.KITKAT)
private void bondAsRequired(final boolean wait) {
    final BluetoothDevice device = I.bleDevice.getBluetoothDevice();
    final int bondState = device.getBondState();
    if (bondState == BOND_NONE) {
        final boolean bondResultCode = device.createBond();
        UserError.Log.d(TAG, "Attempted create bond: result: " + bondResultCode);
    } else {
        UserError.Log.d(TAG, "Device is already in bonding state: " + Helper.bondStateToString(bondState));
    }

    if (wait) {
        for (int c = 0; c < 10; c++) {
            if (device.getBondState() == BOND_BONDED) {
                UserError.Log.d(TAG, "Bond created!");
                changeNextState();
                break;
            } else {
                UserError.Log.d(TAG, "Sleeping waiting for bond: " + c);
                JoH.threadSleep(1000);
            }
        }
    }
}
 
Example 4
Source File: InPenService.java    From xDrip-plus with GNU General Public License v3.0 6 votes vote down vote up
@TargetApi(Build.VERSION_CODES.KITKAT)
private void bondAsRequired(final boolean wait) {
    final BluetoothDevice device = I.bleDevice.getBluetoothDevice();
    final int bondState = device.getBondState();
    if (bondState == BOND_NONE) {
        final boolean bondResultCode = device.createBond();
        UserError.Log.d(TAG, "Attempted create bond: result: " + bondResultCode);
    } else {
        UserError.Log.d(TAG, "Device is already in bonding state: " + Helper.bondStateToString(bondState));
    }

    if (wait) {
        for (int c = 0; c < 10; c++) {
            if (device.getBondState() == BOND_BONDED) {
                UserError.Log.d(TAG, "Bond created!");
                changeNextState();
                break;
            } else {
                UserError.Log.d(TAG, "Sleeping waiting for bond: " + c);
                JoH.threadSleep(1000);
            }
        }
    }
}
 
Example 5
Source File: EasyBLE.java    From easyble-x with Apache License 2.0 5 votes vote down vote up
/**
 * 开始配对
 *
 * @param address 设备地址
 */
public boolean createBond(@NonNull String address) {
    checkStatus();
    try {
        BluetoothDevice remoteDevice = bluetoothAdapter.getRemoteDevice(address);
        return remoteDevice.getBondState() != BluetoothDevice.BOND_NONE || remoteDevice.createBond();
    } catch (Exception ignore) {
        return false;
    }
}
 
Example 6
Source File: DfuBaseService.java    From microbit with Apache License 2.0 5 votes vote down vote up
@SuppressLint("NewApi")
private boolean createBond(final BluetoothDevice device) {
    if (device.getBondState() == BluetoothDevice.BOND_BONDED)
        return true;

    boolean result;
    mRequestCompleted = false;

    sendLogBroadcast(LOG_LEVEL_VERBOSE, "Starting pairing...");
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        sendLogBroadcast(LOG_LEVEL_DEBUG, "gatt.getDevice().createBond()");
        result = device.createBond();
    } else {
        result = createBondApi18(device);
    }

    // We have to wait until device is bounded
    try {
        synchronized (mLock) {
            while (!mRequestCompleted && !mAborted)
                mLock.wait();
        }
    } catch (final InterruptedException e) {
        loge("Sleeping interrupted", e);
    }

    return result;
}
 
Example 7
Source File: BleManager.java    From Android-nRF-Toolbox with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Creates a bond with the device. The device must be first set using {@link #connect(BluetoothDevice)} which will
 * try to connect to the device. If you need to pair with a device before connecting to it you may do it without
 * the use of BleManager object and connect after bond is established.
 * @return true if pairing has started, false if it was already paired or an immediate error occur.
 */
private boolean internalCreateBond() {
	final BluetoothDevice device = bluetoothDevice;
	if (device == null)
		return false;

	if (device.getBondState() == BluetoothDevice.BOND_BONDED)
		return false;

	return device.createBond();
}
 
Example 8
Source File: Nes30Connection.java    From android-robocar with BSD 2-Clause "Simplified" License 4 votes vote down vote up
/**
 * Pair with the specific device.
 */
public boolean createBond(BluetoothDevice device) {
  boolean result = device.createBond();
  Timber.d("Creating bond with: %s/%s/%b", device.getName(), device.getAddress(), result);
  return result;
}
 
Example 9
Source File: K_Util.java    From AsteroidOSSync with GNU General Public License v3.0 4 votes vote down vote up
public static boolean createBond(BluetoothDevice device)
{
    return device.createBond();
}
 
Example 10
Source File: K_Util.java    From SweetBlue with GNU General Public License v3.0 4 votes vote down vote up
public static boolean createBond(BluetoothDevice device)
{
    return device.createBond();
}
 
Example 11
Source File: BtUtils.java    From apollo-DuerOS with Apache License 2.0 3 votes vote down vote up
/**
 * Start the bonding (pairing) process with the remote device.
 * <p>
 * This is an asynchronous call, it will return immediately. Register for
 * {@link #ACTION_BOND_STATE_CHANGED} intents to be notified when the
 * bonding process completes, and its result.
 * <p>
 * Android system services will handle the necessary user interactions to
 * confirm and complete the bonding process.
 * <p>
 * Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN}.
 *
 * @return false on immediate error, true if bonding will begin
 */
@SuppressLint("NewApi")
public static boolean createBond(Class<?> btClass, BluetoothDevice btDevice) throws Exception {
    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        return btDevice.createBond();
    } else {
        Method createBondMethod = btClass.getDeclaredMethod("createBond");
        Boolean returnValue = (Boolean) createBondMethod.invoke(btDevice);
        return returnValue.booleanValue();
    }

}