Example usage for android.bluetooth BluetoothDevice createBond

List of usage examples for android.bluetooth BluetoothDevice createBond

Introduction

In this page you can find the example usage for android.bluetooth BluetoothDevice createBond.

Prototype

@RequiresPermission(Manifest.permission.BLUETOOTH_ADMIN)
public boolean createBond() 

Source Link

Document

Start the bonding (pairing) process with the remote device.

Usage

From source file:com.wolkabout.hexiwear.activity.MainActivity.java

@ItemClick(R.id.listDevices)
void bondWithDevice(final BluetoothDeviceWrapper wrapper) {
    deviceDiscoveryService.cancelScan();
    final BluetoothDevice device = wrapper.getDevice();
    if (device.getBondState() == BluetoothDevice.BOND_BONDED) {
        onBonded(device);//from ww  w .j  ava 2s  .co  m
    } else {
        device.createBond();
    }
}

From source file:co.aurasphere.bluepair.bluetooth.BluetoothController.java

/**
 * Performs the device pairing./*from w  ww .ja v a  2 s .  c om*/
 *
 * @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;
}

From source file:com.bluetooth.mwoolley.microbitbledemo.ui.MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {

    requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);
    setButtonText();/*from ww w.  j a  va 2s.  co m*/
    getSupportActionBar().setTitle(R.string.screen_title_main);
    showMsg(Utility.htmlColorGreen("Ready"));

    Settings.getInstance().restore(this);

    ble_device_list_adapter = new ListAdapter();

    ListView listView = (ListView) this.findViewById(R.id.deviceList);
    listView.setAdapter(ble_device_list_adapter);

    registerReceiver(broadcastReceiver, new IntentFilter(BluetoothDevice.ACTION_BOND_STATE_CHANGED));

    ble_scanner = BleScannerFactory.getBleScanner(this.getApplicationContext());
    ble_scanner.setDevice_name_start(DEVICE_NAME_START);
    ble_scanner.setSelect_bonded_devices_only(true);

    listView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            if (ble_scanning) {
                setScanState(false);
                ble_scanner.stopScanning();
            }

            BluetoothDevice device = ble_device_list_adapter.getDevice(position);
            if (device.getBondState() == BluetoothDevice.BOND_NONE
                    && Settings.getInstance().isFilter_unpaired_devices()) {
                device.createBond();
                showMsg(Utility.htmlColorRed("Selected micro:bit must be paired - pairing now"));
                return;
            }
            try {
                MainActivity.this.unregisterReceiver(broadcastReceiver);
            } catch (Exception e) {
                // ignore!
            }
            if (toast != null) {
                toast.cancel();
            }
            MicroBit microbit = MicroBit.getInstance();
            microbit.setBluetooth_device(device);
            Intent intent = new Intent(MainActivity.this, MenuActivity.class);
            intent.putExtra(MenuActivity.EXTRA_NAME, device.getName());
            intent.putExtra(MenuActivity.EXTRA_ID, device.getAddress());
            startActivity(intent);

        }
    });
}

From source file:com.wolkabout.hexiwear.activity.MainActivity.java

@Receiver(actions = BluetoothDevice.ACTION_BOND_STATE_CHANGED)
void onBondStateChanged(Intent intent) {
    final BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
    final int previousBondState = intent.getIntExtra(BluetoothDevice.EXTRA_PREVIOUS_BOND_STATE, -1);
    final int newBondState = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, -1);

    Log.d(TAG, device.getName() + "(" + device.getAddress() + ") changed state: " + previousBondState + " -> "
            + newBondState);//  ww  w .j a  v a 2s .  c  o m
    adapter.notifyDataSetChanged();

    if (newBondState == BluetoothDevice.BOND_BONDED) {
        onBonded(device);
    } else if (previousBondState == BluetoothDevice.BOND_BONDING && newBondState == BluetoothDevice.BOND_NONE) {
        device.createBond();
    }
}

From source file:com.wolkabout.hexiwear.service.BluetoothService.java

@Receiver(actions = BluetoothDevice.ACTION_BOND_STATE_CHANGED)
void onBondStateChanged(Intent intent) {
    final BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
    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 changed for: " + device.getAddress() + " new state: " + bondState + " previous: "
            + previousBondState);/*from w  ww .  j  ava2 s  . c  o m*/

    if (bondState == BluetoothDevice.BOND_BONDED) {
        Log.i(TAG, "Bonded");
        createGATT(device);
    } else if (bondState == BluetoothDevice.BOND_NONE) {
        device.createBond();
    }
}