Java Code Examples for android.bluetooth.BluetoothDevice#BOND_BONDING
The following examples show how to use
android.bluetooth.BluetoothDevice#BOND_BONDING .
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: BluetoothDevicePreference.java From wearmouse with Apache License 2.0 | 6 votes |
/** Request to unpair and remove the bond */ private void requestUnpair() { final int state = device.getBondState(); if (state == BluetoothDevice.BOND_BONDING) { BluetoothUtils.cancelBondProcess(device); } else if (state != BluetoothDevice.BOND_NONE) { AcceptDenyDialog diag = new AcceptDenyDialog(getContext()); diag.setTitle(R.string.pref_bluetooth_unpair); diag.setMessage(device.getName()); diag.setPositiveButton( (dialog, which) -> { if (!BluetoothUtils.removeBond(device)) { Log.w(TAG, "Unpair request rejected straight away."); } }); diag.setNegativeButton((dialog, which) -> {}); diag.show(); } }
Example 2
Source File: P_BondManager.java From AsteroidOSSync with GNU General Public License v3.0 | 6 votes |
boolean isNativelyBondingOrBonded() { //--- DRK > These asserts are here because, as far as I could discern from logs, the abstracted //--- state for bonding/bonded was true, but when we did an encrypted write, it kicked //--- off a bonding operation, implying that natively the bonding state silently changed //--- since we discovered the device. I really don't know. //--- UPDATE: Nevermind, the reason bonding wasn't happening after connection was because //--- it was using the default config option of false. Leaving asserts here anywway //--- cause they can't hurt. //--- UPDATE AGAIN: Actually, these asserts can hit if you're connected to a device, you go //--- into OS settings, unbond, which kicks off an implicit disconnect which then kicks off //--- an implicit reconnect...race condition makes it so that you can query the bond state //--- and get its updated value before the bond state callback gets sent //--- UPDATE AGAIN AGAIN: Nevermind, it seems getBondState *can* actually lie, so original comment sorta stands...wow. // m_mngr.ASSERT(m_stateTracker.checkBitMatch(BONDED, isNativelyBonded())); // m_mngr.ASSERT(m_stateTracker.checkBitMatch(BONDING, isNativelyBonding())); int bondState = m_device.m_nativeWrapper.getNativeBondState(); return bondState == BluetoothDevice.BOND_BONDED || bondState == BluetoothDevice.BOND_BONDING; }
Example 3
Source File: AvailableDevicesFragment.java From wearmouse with Apache License 2.0 | 6 votes |
/** Re-examine the device and update if necessary. */ protected void updateAvailableDevice(BluetoothDevice device) { final BluetoothDevicePreference pref = findDevicePreference(device); if (pref != null) { pref.updateBondState(); switch (device.getBondState()) { case BluetoothDevice.BOND_BONDED: pref.setEnabled(false); availableDevices.removePreference(pref); break; case BluetoothDevice.BOND_BONDING: pref.setEnabled(false); break; case BluetoothDevice.BOND_NONE: pref.setEnabled(true); addAvailableDevice(device); break; default: // fall out } } }
Example 4
Source File: DeviceListActivity.java From AndroBluetooth with Apache License 2.0 | 6 votes |
public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)) { final int state = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, BluetoothDevice.ERROR); final int prevState = intent.getIntExtra(BluetoothDevice.EXTRA_PREVIOUS_BOND_STATE, BluetoothDevice.ERROR); if (state == BluetoothDevice.BOND_BONDED && prevState == BluetoothDevice.BOND_BONDING) { showToast("Paired"); } else if (state == BluetoothDevice.BOND_NONE && prevState == BluetoothDevice.BOND_BONDED){ showToast("Unpaired"); } mAdapter.notifyDataSetChanged(); } }
Example 5
Source File: AmazonFreeRTOSDevice.java From amazon-freertos-ble-android-sdk with Apache License 2.0 | 6 votes |
@Override // New services discovered public void onServicesDiscovered(BluetoothGatt gatt, int status) { if (status == BluetoothGatt.GATT_SUCCESS) { Log.i(TAG, "Discovered Ble gatt services successfully. Bonding state: " + mBluetoothDevice.getBondState()); describeGattServices(mBluetoothGatt.getServices()); if (mBluetoothDevice.getBondState() != BluetoothDevice.BOND_BONDING) { probe(); } } else { Log.e(TAG, "onServicesDiscovered received: " + status); disconnect(); } processNextBleCommand(); }
Example 6
Source File: BleManager.java From Android-nRF-Toolbox with BSD 3-Clause "New" or "Revised" License | 6 votes |
@Override public void onReceive(final Context context, final 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); // Skip other devices if (bluetoothGatt == null || !device.getAddress().equals(bluetoothGatt.getDevice().getAddress())) return; DebugLogger.i(TAG, "Bond state changed for: " + device.getName() + " new state: " + bondState + " previous: " + previousBondState); switch (bondState) { case BluetoothDevice.BOND_BONDING: callbacks.onBondingRequired(device); break; case BluetoothDevice.BOND_BONDED: callbacks.onBonded(device); // Start initializing again. // In fact, bonding forces additional, internal service discovery (at least on Nexus devices), so this method may safely be used to start this process again. bluetoothGatt.discoverServices(); break; } }
Example 7
Source File: DexShareCollectionService.java From xDrip with GNU General Public License v3.0 | 6 votes |
public void onReceive(Context context, Intent intent) { String action = intent.getAction(); final BluetoothDevice bondDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); if (mBluetoothGatt != null && mBluetoothGatt.getDevice() != null && bondDevice != null) { if (!bondDevice.getAddress().equals(mBluetoothGatt.getDevice().getAddress())) { Log.d(TAG, "Bond state wrong device"); return; // That wasnt a device we care about!! } } if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)) { final int state = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, BluetoothDevice.ERROR); if (state == BluetoothDevice.BOND_BONDED) { Log.d(TAG, "CALLBACK RECIEVED Bonded"); authenticateConnection(); } else if (state == BluetoothDevice.BOND_NONE) { Log.d(TAG, "CALLBACK RECIEVED: Not Bonded"); } else if (state == BluetoothDevice.BOND_BONDING) { Log.d(TAG, "CALLBACK RECIEVED: Trying to bond"); } } }
Example 8
Source File: PairedDevicesFragment.java From wearmouse with Apache License 2.0 | 6 votes |
/** Examine the bond state of the device and update preference if necessary. */ private void updatePreferenceBondState(final BluetoothDevice device) { final BluetoothDevicePreference pref = findOrAllocateDevicePreference(device); pref.updateBondState(); pref.updateProfileConnectionState(); switch (device.getBondState()) { case BluetoothDevice.BOND_BONDED: pref.setEnabled(true); pref.setOrder(PREFERENCE_ORDER_NORMAL); bondedDevices.add(pref); getPreferenceScreen().addPreference(pref); break; case BluetoothDevice.BOND_NONE: pref.setEnabled(false); bondedDevices.remove(pref); getPreferenceScreen().removePreference(pref); break; case BluetoothDevice.BOND_BONDING: pref.setEnabled(false); break; default: // fall out } }
Example 9
Source File: ShareTest.java From xDrip-plus with GNU General Public License v3.0 | 6 votes |
public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)) { final int state = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, BluetoothDevice.ERROR); final int prevState = intent.getIntExtra(BluetoothDevice.EXTRA_PREVIOUS_BOND_STATE, BluetoothDevice.ERROR); if (state == BluetoothDevice.BOND_BONDED) { Log.d(TAG, "CALLBACK RECIEVED Bonded"); currentGattTask = GATT_SETUP; mBluetoothGatt.discoverServices(); } else if (state == BluetoothDevice.BOND_NONE){ Log.d(TAG, "CALLBACK RECIEVED: Not Bonded"); Toast.makeText(getApplicationContext(), "unBonded", Toast.LENGTH_LONG).show(); } else if (state == BluetoothDevice.BOND_BONDING) { Log.d(TAG, "CALLBACK RECIEVED: Trying to bond"); Toast.makeText(getApplicationContext(), "trying to bond", Toast.LENGTH_LONG).show(); } } }
Example 10
Source File: DfuBaseService.java From microbit with Apache License 2.0 | 6 votes |
@Override public void onReceive(final Context context, final Intent intent) { // Obtain the device and check it this is the one that we are connected to final BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); if (!device.getAddress().equals(mDeviceAddress)) return; // Read bond state final int bondState = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, -1); if (bondState == BluetoothDevice.BOND_BONDING) return; mRequestCompleted = true; // Notify waiting thread synchronized (mLock) { mLock.notifyAll(); } }
Example 11
Source File: ParserUtils.java From Android-BLE-Library with BSD 3-Clause "New" or "Revised" License | 5 votes |
@NonNull public static String bondStateToString(@BondState final int state) { switch (state) { case BluetoothDevice.BOND_NONE: return "BOND_NONE"; case BluetoothDevice.BOND_BONDING: return "BOND_BONDING"; case BluetoothDevice.BOND_BONDED: return "BOND_BONDED"; default: return "UNKNOWN (" + state + ")"; } }
Example 12
Source File: BluetoothLeDevice.java From BLE with Apache License 2.0 | 5 votes |
/** * Resolve bonding state. * * @param bondState the bond state * @return the string */ private static String resolveBondingState(final int bondState) { switch (bondState) { case BluetoothDevice.BOND_BONDED://已配对 return "Paired"; case BluetoothDevice.BOND_BONDING://配对中 return "Pairing"; case BluetoothDevice.BOND_NONE://未配对 return "UnBonded"; default: return "Unknown";//未知状态 } }
Example 13
Source File: DevicesAdapter.java From Android-nRF-Toolbox with BSD 3-Clause "New" or "Revised" License | 5 votes |
private String getState(final BluetoothDevice device, final int position) { if (connectingPosition == position) return connectingText; else if (device.getBondState() == BluetoothDevice.BOND_BONDED) return bondedText; else if (device.getBondState() == BluetoothDevice.BOND_BONDING) return bondingText; return availableText; }
Example 14
Source File: Nes30Connection.java From android-robocar with BSD 2-Clause "Simplified" License | 5 votes |
public void onReceive(Context context, Intent intent) { String action = intent.getAction(); // Discovery has found a device. if (BluetoothDevice.ACTION_FOUND.equals(action)) { // Get the BluetoothDevice object and its info from the Intent. BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); int bondState = device.getBondState(); String foundName = device.getName(); String foundAddress = device.getAddress(); // MAC address Timber.d("Discovery has found a device: %d/%s/%s", bondState, foundName, foundAddress); if (isSelectedDevice(foundAddress)) { createBond(device); } else { Timber.d("Unknown device, skipping bond attempt."); } } else if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)) { int state = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, -1); switch (state) { case BluetoothDevice.BOND_NONE: Timber.d("The remote device is not bonded."); break; case BluetoothDevice.BOND_BONDING: Timber.d("Bonding is in progress with the remote device."); break; case BluetoothDevice.BOND_BONDED: Timber.d("The remote device is bonded."); break; default: Timber.d("Unknown remote device bonding state."); break; } } }
Example 15
Source File: Helper.java From xDrip-plus with GNU General Public License v3.0 | 5 votes |
public static String bondStateToString(final int bs) { String bondState; if (bs == BluetoothDevice.BOND_NONE) { bondState = "Unpaired"; } else if (bs == BluetoothDevice.BOND_BONDING) { bondState = "Pairing"; } else if (bs == BluetoothDevice.BOND_BONDED) { bondState = "Paired"; } else if (bs == 0) { bondState = "Startup"; } else { bondState = "Unknown bond state: " + bs; } return bondState; }
Example 16
Source File: BleManagerHandler.java From Android-BLE-Library with BSD 3-Clause "New" or "Revised" License | 4 votes |
@Override public void onReceive(final Context context, final 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); // Skip other devices. if (bluetoothDevice == null || device == null || !device.getAddress().equals(bluetoothDevice.getAddress())) return; log(Log.DEBUG, "[Broadcast] Action received: " + BluetoothDevice.ACTION_BOND_STATE_CHANGED + ", bond state changed to: " + ParserUtils.bondStateToString(bondState) + " (" + bondState + ")"); switch (bondState) { case BluetoothDevice.BOND_NONE: if (previousBondState == BluetoothDevice.BOND_BONDING) { postCallback(c -> c.onBondingFailed(device)); postBondingStateChange(o -> o.onBondingFailed(device)); log(Log.WARN, "Bonding failed"); if (request != null) { // CREATE_BOND request request.notifyFail(device, FailCallback.REASON_REQUEST_FAILED); request = null; } } else if (previousBondState == BluetoothDevice.BOND_BONDED) { if (request != null && request.type == Request.Type.REMOVE_BOND) { // The device has already disconnected by now. log(Log.INFO, "Bond information removed"); request.notifySuccess(device); request = null; } // When the bond information has been removed (either with Remove Bond request // or in Android Settings), the BluetoothGatt object should be closed, so // the library won't reconnect to the device automatically. // See: https://github.com/NordicSemiconductor/Android-BLE-Library/issues/157 close(); } break; case BluetoothDevice.BOND_BONDING: postCallback(c -> c.onBondingRequired(device)); postBondingStateChange(o -> o.onBondingRequired(device)); return; case BluetoothDevice.BOND_BONDED: log(Log.INFO, "Device bonded"); postCallback(c -> c.onBonded(device)); postBondingStateChange(o -> o.onBonded(device)); if (request != null && request.type == Request.Type.CREATE_BOND) { request.notifySuccess(device); request = null; break; } // If the device started to pair just after the connection was // established the services were not discovered. if (!servicesDiscovered && !serviceDiscoveryRequested) { serviceDiscoveryRequested = true; post(() -> { log(Log.VERBOSE, "Discovering services..."); log(Log.DEBUG, "gatt.discoverServices()"); bluetoothGatt.discoverServices(); }); return; } // On older Android versions, after executing a command on secured attribute // of a device that is not bonded, let's say a write characteristic operation, // the system will start bonding. The BOND_BONDING and BOND_BONDED events will // be received, but the command will not be repeated automatically. // // Test results: // Devices that require repeating the last task: // - Nexus 4 with Android 5.1.1 // - Samsung S6 with 5.0.1 // - Samsung S8 with Android 7.0 // - Nexus 9 with Android 7.1.1 // Devices that repeat the request automatically: // - Pixel 2 with Android 8.1.0 // - Samsung S8 with Android 8.0.0 // if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) { if (request != null) { // Repeat the last command in that case. enqueueFirst(request); break; } } // No need to repeat the request. return; } nextRequest(true); }
Example 17
Source File: P_NativeDeviceWrapper.java From AsteroidOSSync with GNU General Public License v3.0 | 4 votes |
boolean isNativelyBonding(int bondState) { return bondState == BluetoothDevice.BOND_BONDING; }
Example 18
Source File: BtReceiver.java From EasyBluetoothFrame with Apache License 2.0 | 4 votes |
@Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); // Get the BluetoothDevice object from the Intent BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); // When discovery finds a device if (BluetoothDevice.ACTION_FOUND.equals(action)) { // Add the name and address to an array adapter to show in a ListView if (scanResultListener != null) { scanResultListener.onDeviceFound(device); } //配对请求 } else if (BluetoothDevice.ACTION_PAIRING_REQUEST.equals(action)) { // try { // // //1.确认配对 // Method setPairingConfirmation = device.getClass().getDeclaredMethod("setPairingConfirmation", boolean.class); // setPairingConfirmation.invoke(device, true); // //2.终止有序广播 // abortBroadcast();//如果没有将广播终止,则会出现一个一闪而过的配对框。 // //3.调用setPin方法进行配对... //// boolean ret = ClsUtils.setPin(device.getClass(), device, pin); // Method removeBondMethod = device.getClass().getDeclaredMethod("setPin", new Class[]{byte[].class}); // Boolean returnValue = (Boolean) removeBondMethod.invoke(device, new Object[]{pin.getBytes()}); // } catch (Exception e) { // // TODO Auto-generated catch block // e.printStackTrace(); // if (pinResultListener != null) { // pinResultListener.pairFailed(device); // } // // } } else if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)) { switch (device.getBondState()) { case BluetoothDevice.BOND_NONE: Log.d(TAG, "取消配对"); if (pinResultListener != null) pinResultListener.pairFailed(device); break; case BluetoothDevice.BOND_BONDING: Log.d(TAG, "配对中"); if (pinResultListener != null) pinResultListener.pairing(device); break; case BluetoothDevice.BOND_BONDED: Log.d(TAG, "配对成功"); if (pinResultListener != null) pinResultListener.paired(device); break; } }else if ((BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)&&intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1) == BluetoothAdapter.STATE_OFF)||BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)){ if(serverConnectResultListener!=null) serverConnectResultListener.disconnected(); if(clientConnectResultListener!=null) clientConnectResultListener.disconnected(); } }
Example 19
Source File: BluetoothEventManager.java From talkback with Apache License 2.0 | 4 votes |
@RequiresPermission(allOf = {permission.BLUETOOTH, permission.BLUETOOTH_ADMIN}) @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); BluetoothDevice bluetoothDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); if (bluetoothDevice == null) { return; } ComparableBluetoothDevice comparableBluetoothDevice = new ComparableBluetoothDevice( context, bluetoothAdapter, bluetoothDevice, bluetoothDeviceActionListener); if (BluetoothDevice.ACTION_NAME_CHANGED.equals(action) || BluetoothDevice.ACTION_FOUND.equals(action)) { String bluetoothDeviceName = intent.getStringExtra(BluetoothDevice.EXTRA_NAME); comparableBluetoothDevice.setName(bluetoothDeviceName); if (action.equals(BluetoothDevice.ACTION_FOUND)) { BluetoothClass bluetoothDeviceClass = intent.getParcelableExtra(BluetoothDevice.EXTRA_CLASS); comparableBluetoothDevice.setBluetoothClass(bluetoothDeviceClass); } /* Don't add a device if it's already been bonded (paired) to the device. */ if (bluetoothDevice.getBondState() != BluetoothDevice.BOND_BONDED) { short rssi = intent.getShortExtra(BluetoothDevice.EXTRA_RSSI, Short.MIN_VALUE); comparableBluetoothDevice.setRssi(rssi); dispatchDeviceDiscoveredEvent(comparableBluetoothDevice); // TODO: Remove available devices from adapter if they become // unavailable. This will most likely be unable to be addressed without API changes. } else { dispatchDevicePairedEvent(comparableBluetoothDevice); } } else if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) { if (bluetoothDevice.getBondState() == BluetoothDevice.BOND_BONDED) { comparableBluetoothDevice.setConnectionState(BluetoothConnectionState.CONNECTED); dispatchDeviceConnectionStateChangedEvent(comparableBluetoothDevice); } } else if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)) { if (bluetoothDevice.getBondState() == BluetoothDevice.BOND_BONDED) { comparableBluetoothDevice.setConnectionState(BluetoothConnectionState.UNKNOWN); dispatchDeviceConnectionStateChangedEvent(comparableBluetoothDevice); } } else if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)) { if (bluetoothDevice.getBondState() == BluetoothDevice.BOND_BONDED) { comparableBluetoothDevice.setConnectionState(BluetoothConnectionState.CONNECTED); dispatchDevicePairedEvent(comparableBluetoothDevice); } else if (bluetoothDevice.getBondState() == BluetoothDevice.BOND_NONE) { /* The call to #createBond has completed, but the Bluetooth device isn't bonded, so * set the connection state to unavailable. */ comparableBluetoothDevice.setConnectionState(BluetoothConnectionState.UNAVAILABLE); dispatchDeviceConnectionStateChangedEvent(comparableBluetoothDevice); } /* If we canceled discovery before beginning the pairing process, resume discovery after * {@link BluetoothDevice#createBond} finishes. */ if (bluetoothDevice.getBondState() != BluetoothDevice.BOND_BONDING && !bluetoothAdapter.isDiscovering()) { bluetoothAdapter.startDiscovery(); } } }
Example 20
Source File: P_NativeDeviceWrapper.java From AsteroidOSSync with GNU General Public License v3.0 | 4 votes |
boolean isNativelyBonding() { return getNativeBondState() == BluetoothDevice.BOND_BONDING; }