Example usage for android.bluetooth BluetoothDevice BOND_BONDED

List of usage examples for android.bluetooth BluetoothDevice BOND_BONDED

Introduction

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

Prototype

int BOND_BONDED

To view the source code for android.bluetooth BluetoothDevice BOND_BONDED.

Click Source Link

Document

Indicates the remote device is bonded (paired).

Usage

From source file:Main.java

public static String formatBondState(int bondState) {
    switch (bondState) {
    case BluetoothDevice.BOND_NONE:
        return "Not Bonded";
    case BluetoothDevice.BOND_BONDING:
        return "Bonding";
    case BluetoothDevice.BOND_BONDED:
        return "Bonded";
    default://from   w  w  w.ja va2  s  .  co  m
        return "Unknown";
    }

}

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);/* www .  j  av  a  2  s. com*/
    } else {
        device.createBond();
    }
}

From source file:org.chromium.ChromeBluetooth.java

private JSONObject getBasicDeviceInfo(BluetoothDevice device) throws JSONException {
    JSONObject deviceInfo = new JSONObject();
    deviceInfo.put("address", device.getAddress());
    deviceInfo.put("name", device.getName());
    deviceInfo.put("deviceClass", device.getBluetoothClass().getDeviceClass());
    deviceInfo.put("paired", device.getBondState() == BluetoothDevice.BOND_BONDED);
    deviceInfo.put("connected", isConnected(device));
    deviceInfo.put("uuids", new JSONArray(getUuidStringsFromDevice(device)));
    return deviceInfo;
}

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);/* ww  w . 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();
    }
}

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  . ja v a 2 s . 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.polyvi.xface.extension.bluetooth.XBluetoothExt.java

/**
 * ?mac???//from  w w w  .  j  av a  2 s .c om
 *
 * @param args
 *            ?mac?json
 * @return PluginResult
 */
private PluginResult isBoundBluetooth(JSONArray args) {
    String addressDevice = null;
    try {
        addressDevice = args.getString(0);
    } catch (JSONException e) {
        e.printStackTrace();
        return new PluginResult(PluginResult.Status.ERROR, e.getMessage());
    }
    BluetoothDevice device = mBtadapter.getRemoteDevice(addressDevice);
    boolean isBound = false;

    if (device != null && device.getBondState() == BluetoothDevice.BOND_BONDED)
        isBound = true;
    else
        isBound = false;
    return new PluginResult(PluginResult.Status.OK, isBound);
}

From source file:nodomain.freeyourgadget.gadgetbridge.devices.pebble.PebblePairingActivity.java

protected void performPair(GBDevice gbDevice) {
    int bondState = mBtDevice.getBondState();
    if (bondState == BluetoothDevice.BOND_BONDED) {
        GB.toast(getString(R.string.pairing_already_bonded, mBtDevice.getName(), mBtDevice.getAddress()),
                Toast.LENGTH_SHORT, GB.INFO);
        return;// www. j a v  a  2  s  .co  m
    }

    if (bondState == BluetoothDevice.BOND_BONDING) {
        GB.toast(this, getString(R.string.pairing_in_progress, mBtDevice.getName(), macAddress),
                Toast.LENGTH_LONG, GB.INFO);
        return;
    }

    GB.toast(this, getString(R.string.pairing_creating_bond_with, mBtDevice.getName(), macAddress),
            Toast.LENGTH_LONG, GB.INFO);
    GBApplication.deviceService().disconnect(); // just to make sure...

    if (isLEPebble) {
        performConnect(gbDevice);
    } else {
        mBtDevice.createBond();
    }
}

From source file:com.piusvelte.taplock.client.core.TapLockService.java

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    if (intent != null) {
        String action = intent.getAction();
        if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) {
            int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.STATE_OFF);
            if (state == BluetoothAdapter.STATE_ON) {
                if (mStartedBT) {
                    if (mUIInterface != null) {
                        try {
                            mUIInterface.setMessage("Bluetooth enabled");
                        } catch (RemoteException e) {
                            Log.e(TAG, e.getMessage());
                        }/*w  ww . ja  va2  s.  co  m*/
                    }
                    if ((mQueueAddress != null) && (mQueueState != null))
                        requestWrite(mQueueAddress, mQueueState, mQueuePassphrase);
                    else if (mRequestDiscovery && !mBtAdapter.isDiscovering())
                        mBtAdapter.startDiscovery();
                    else if (mUIInterface != null) {
                        try {
                            mUIInterface.setBluetoothEnabled();
                        } catch (RemoteException e) {
                            Log.e(TAG, e.getMessage());
                        }
                    }
                }
            } else if (state == BluetoothAdapter.STATE_TURNING_OFF) {
                if (mUIInterface != null) {
                    try {
                        mUIInterface.setMessage("Bluetooth disabled");
                    } catch (RemoteException e) {
                        Log.e(TAG, e.getMessage());
                    }
                }
                stopThreads();
            }
        } else if (BluetoothDevice.ACTION_FOUND.equals(action)) {
            // Get the BluetoothDevice object from the Intent
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            if (device.getBondState() == BluetoothDevice.BOND_BONDED) {
                // connect if configured
                String address = device.getAddress();
                for (JSONObject deviceJObj : mDevices) {
                    try {
                        if (deviceJObj.getString(KEY_ADDRESS).equals(address)) {
                            // if queued
                            mDeviceFound = (mQueueAddress != null) && mQueueAddress.equals(address)
                                    && (mQueueState != null);
                            break;
                        }
                    } catch (JSONException e) {
                        Log.e(TAG, e.getMessage());
                    }
                }
            } else if (mRequestDiscovery && (mUIInterface != null)) {
                String unpairedDevice = TapLock
                        .createDevice(device.getName(), device.getAddress(), DEFAULT_PASSPHRASE).toString();
                try {
                    mUIInterface.setUnpairedDevice(unpairedDevice);
                } catch (RemoteException e) {
                    Log.e(TAG, e.getMessage());
                }
            }
        } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
            if (mDeviceFound) {
                requestWrite(mQueueAddress, mQueueState, mQueuePassphrase);
                mDeviceFound = false;
            } else if (mRequestDiscovery) {
                mRequestDiscovery = false;
                if (mUIInterface != null) {
                    try {
                        mUIInterface.setDiscoveryFinished();
                    } catch (RemoteException e) {
                        Log.e(TAG, e.toString());
                    }
                }
            }
        } else if (ACTION_TOGGLE.equals(action) && intent.hasExtra(EXTRA_DEVICE_ADDRESS)) {
            String address = intent.getStringExtra(EXTRA_DEVICE_ADDRESS);
            requestWrite(address, ACTION_TOGGLE, null);
        } else if (AppWidgetManager.ACTION_APPWIDGET_UPDATE.equals(action)) {
            // create widget
            if (intent.hasExtra(AppWidgetManager.EXTRA_APPWIDGET_ID)) {
                int appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
                        AppWidgetManager.INVALID_APPWIDGET_ID);
                if (intent.hasExtra(EXTRA_DEVICE_NAME)) {
                    // add a widget
                    String deviceName = intent.getStringExtra(EXTRA_DEVICE_NAME);
                    for (int i = 0, l = mDevices.size(); i < l; i++) {
                        String name = null;
                        try {
                            name = mDevices.get(i).getString(KEY_NAME);
                        } catch (JSONException e1) {
                            e1.printStackTrace();
                        }
                        if ((name != null) && name.equals(deviceName)) {
                            JSONObject deviceJObj = mDevices.remove(i);
                            JSONArray widgetsJArr;
                            if (deviceJObj.has(KEY_WIDGETS)) {
                                try {
                                    widgetsJArr = deviceJObj.getJSONArray(KEY_WIDGETS);
                                } catch (JSONException e) {
                                    widgetsJArr = new JSONArray();
                                }
                            } else
                                widgetsJArr = new JSONArray();
                            widgetsJArr.put(appWidgetId);
                            try {
                                deviceJObj.put(KEY_WIDGETS, widgetsJArr);
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                            mDevices.add(i, deviceJObj);
                            TapLock.storeDevices(this, getSharedPreferences(KEY_PREFS, MODE_PRIVATE), mDevices);
                            break;
                        }
                    }
                }
                buildWidget(appWidgetId);
            } else if (intent.hasExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS)) {
                int[] appWidgetIds = intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS);
                if (appWidgetIds != null) {
                    for (int appWidgetId : appWidgetIds)
                        buildWidget(appWidgetId);
                }
            }
        } else if (AppWidgetManager.ACTION_APPWIDGET_DELETED.equals(action)) {
            int appWidgetId = intent.getExtras().getInt(AppWidgetManager.EXTRA_APPWIDGET_ID,
                    AppWidgetManager.INVALID_APPWIDGET_ID);
            Log.d(TAG, "delete appWidgetId: " + appWidgetId);
            for (int i = 0, l = mDevices.size(); i < l; i++) {
                JSONObject deviceJObj = mDevices.get(i);
                if (deviceJObj.has(KEY_WIDGETS)) {
                    JSONArray widgetsJArr = null;
                    try {
                        widgetsJArr = deviceJObj.getJSONArray(KEY_WIDGETS);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    if (widgetsJArr != null) {
                        boolean wasUpdated = false;
                        JSONArray newWidgetsJArr = new JSONArray();
                        for (int widgetIdx = 0, wdigetsLen = widgetsJArr
                                .length(); widgetIdx < wdigetsLen; widgetIdx++) {
                            int widgetId;
                            try {
                                widgetId = widgetsJArr.getInt(widgetIdx);
                            } catch (JSONException e) {
                                widgetId = AppWidgetManager.INVALID_APPWIDGET_ID;
                                e.printStackTrace();
                            }
                            Log.d(TAG, "eval widgetId: " + widgetId);
                            if ((widgetId != AppWidgetManager.INVALID_APPWIDGET_ID)
                                    && (widgetId == appWidgetId)) {
                                Log.d(TAG, "skip: " + widgetId);
                                wasUpdated = true;
                            } else {
                                Log.d(TAG, "include: " + widgetId);
                                newWidgetsJArr.put(widgetId);
                            }
                        }
                        if (wasUpdated) {
                            try {
                                deviceJObj.put(KEY_WIDGETS, newWidgetsJArr);
                                mDevices.remove(i);
                                mDevices.add(i, deviceJObj);
                                TapLock.storeDevices(this, getSharedPreferences(KEY_PREFS, MODE_PRIVATE),
                                        mDevices);
                                Log.d(TAG, "stored: " + deviceJObj.toString());
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                } else {
                    JSONArray widgetsJArr = new JSONArray();
                    try {
                        deviceJObj.put(KEY_WIDGETS, widgetsJArr);
                        mDevices.remove(i);
                        mDevices.add(i, deviceJObj);
                        TapLock.storeDevices(this, getSharedPreferences(KEY_PREFS, MODE_PRIVATE), mDevices);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
    return START_STICKY;
}

From source file:com.rosterloh.moodring.profile.BleProfileService.java

@Override
public void onBonded() {
    Log.i(TAG, "Bond state: Bonded");
    showToast(R.string.bonded);/*from www .  j a  v a 2 s .  co m*/

    final Intent broadcast = new Intent(BROADCAST_BOND_STATE);
    broadcast.putExtra(EXTRA_BOND_STATE, BluetoothDevice.BOND_BONDED);
    LocalBroadcastManager.getInstance(this).sendBroadcast(broadcast);
}

From source file:com.diy.blelib.profile.BleProfileService.java

@Override
public void onBonded() {
    Log.i(TAG, "Bond state: Bonded");
    //      showToast(R.string.bonded);

    final Intent broadcast = new Intent(BROADCAST_BOND_STATE);
    broadcast.putExtra(EXTRA_BOND_STATE, BluetoothDevice.BOND_BONDED);
    LocalBroadcastManager.getInstance(this).sendBroadcast(broadcast);
}