public String getServiceName() { if (mName != null) return mName; IBluetooth service = BluetoothDevice.getService(); try { return service.getGattServiceName(mObjPath); } catch (RemoteException e) { Log.e(TAG, "", e); } return null; }
public synchronized void onCharacteristicsDiscovered(String[] paths) { Log.d(TAG, "onCharacteristicsDiscovered: " + paths); if (paths == null) return; int count = paths.length; Log.d( TAG, "Discovered " + count + " characteristics for service " + mObjPath + " ( " + mName + " )"); characteristicPaths = paths; for (int i = 0; i < count; i++) { String[] properties = null; try { properties = mService.getCharacteristicProperties(paths[i]); } catch (RemoteException e) { Log.e(TAG, "", e); } if (properties != null) { addCharacteristicProperties(paths[i], properties); } } discoveryDone = true; this.notify(); }
/** * Returns the hardware address of the local Bluetooth adapter. * * <p>For example, "00:11:22:AA:BB:CC". * * <p>Requires {@link android.Manifest.permission#BLUETOOTH} * * @return Bluetooth hardware address as string */ public String getAddress() { try { return mService.getAddress(); } catch (RemoteException e) { Log.e(TAG, "", e); } return null; }
/** * Turn off the local Bluetooth adapter—do not use without explicit user action to turn off * Bluetooth. * * <p>This gracefully shuts down all Bluetooth connections, stops Bluetooth system services, and * powers down the underlying Bluetooth hardware. * * <p class="caution"><strong>Bluetooth should never be disabled without direct user * consent</strong>. The {@link #disable()} method is provided only for applications that include * a user interface for changing system settings, such as a "power manager" app. * * <p>This is an asynchronous call: it will return immediately, and clients should listen for * {@link #ACTION_STATE_CHANGED} to be notified of subsequent adapter state changes. If this call * returns true, then the adapter state will immediately transition from {@link #STATE_ON} to * {@link #STATE_TURNING_OFF}, and some time later transition to either {@link #STATE_OFF} or * {@link #STATE_ON}. If this call returns false then there was an immediate problem that will * prevent the adapter from being turned off - such as the adapter already being turned off. * * <p>Requires the {@link android.Manifest.permission#BLUETOOTH_ADMIN} permission * * @return true to indicate adapter shutdown has begun, or false on immediate error */ public boolean disable() { try { return mService.disable(true); } catch (RemoteException e) { Log.e(TAG, "", e); } return false; }
/** * Remove bond (pairing) with the remote device. * * <p>Delete the link key associated with the remote device, and immediately terminate connections * to that device that require authentication and encryption. * * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN}. * * @return true on success, false on error * @hide */ public boolean removeBond() { try { return sService.removeBond(mAddress); } catch (RemoteException e) { Log.e(TAG, "", e); } return false; }
/** * Get trust state of a remote device. * * <p>Requires {@link android.Manifest.permission#BLUETOOTH}. * * @hide */ public boolean getTrustState() { try { return sService.getTrustState(mAddress); } catch (RemoteException e) { Log.e(TAG, "", e); } return false; }
/** * Return true if Bluetooth is currently enabled and ready for use. * * <p>Equivalent to: <code>getBluetoothState() == STATE_ON</code> * * <p>Requires {@link android.Manifest.permission#BLUETOOTH} * * @return true if the local adapter is turned on */ public boolean isEnabled() { try { return mService.isEnabled(); } catch (RemoteException e) { Log.e(TAG, "", e); } return false; }
/** @hide */ public boolean isBluetoothDock() { try { return sService.isBluetoothDock(mAddress); } catch (RemoteException e) { Log.e(TAG, "", e); } return false; }
/** * Set trust state for a remote device. * * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN}. * * @param value the trust state value (true or false) * @hide */ public boolean setTrust(boolean value) { try { return sService.setTrust(mAddress, value); } catch (RemoteException e) { Log.e(TAG, "", e); } return false; }
/** * Get the bond state of the remote device. * * <p>Possible values for the bond state are: {@link #BOND_NONE}, {@link #BOND_BONDING}, {@link * #BOND_BONDED}. * * <p>Requires {@link android.Manifest.permission#BLUETOOTH}. * * @return the bond state */ public int getBondState() { try { return sService.getBondState(mAddress); } catch (RemoteException e) { Log.e(TAG, "", e); } return BOND_NONE; }
/** * Cancel an in-progress bonding request started with {@link #createBond}. * * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN}. * * @return true on success, false on error * @hide */ public boolean cancelBondProcess() { try { return sService.cancelBondProcess(mAddress); } catch (RemoteException e) { Log.e(TAG, "", e); } return false; }
/** * Get the current state of the local Bluetooth adapter. * * <p>Possible return values are {@link #STATE_OFF}, {@link #STATE_TURNING_ON}, {@link #STATE_ON}, * {@link #STATE_TURNING_OFF}. * * <p>Requires {@link android.Manifest.permission#BLUETOOTH} * * @return current state of Bluetooth adapter */ public int getState() { try { return mService.getBluetoothState(); } catch (RemoteException e) { Log.e(TAG, "", e); } return STATE_OFF; }
/** * Set the Out Of Band data for a remote device to be used later in the pairing mechanism. Users * can obtain this data through other trusted channels * * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN}. * * @param hash Simple Secure pairing hash * @param randomizer The random key obtained using OOB * @return false on error; true otherwise * @hide */ public boolean setDeviceOutOfBandData(byte[] hash, byte[] randomizer) { try { return sService.setDeviceOutOfBandData(mAddress, hash, randomizer); } catch (RemoteException e) { Log.e(TAG, "", e); } return false; }
/** * Start the bonding (pairing) process with the remote device using the Out Of Band mechanism. * * <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}. * * @param hash - Simple Secure pairing hash * @param randomizer - The random key obtained using OOB * @return false on immediate error, true if bonding will begin * @hide */ public boolean createBondOutOfBand(byte[] hash, byte[] randomizer) { try { return sService.createBondOutOfBand(mAddress, hash, randomizer); } catch (RemoteException e) { Log.e(TAG, "", e); } return false; }
/** * Get the friendly Bluetooth name of the remote device. * * <p>The local adapter will automatically retrieve remote names when performing a device scan, * and will cache them. This method just returns the name for this device from the cache. * * <p>Requires {@link android.Manifest.permission#BLUETOOTH} * * @return the Bluetooth name, or null if there was a problem. */ public String getName() { try { return sService.getRemoteName(mAddress); } catch (RemoteException e) { Log.e(TAG, "", e); } return null; }
/** @hide */ public boolean cancelPairingUserInput() { try { return sService.cancelPairingUserInput(mAddress); } catch (RemoteException e) { Log.e(TAG, "", e); } return false; }
/** @hide */ public boolean setRemoteOutOfBandData() { try { return sService.setRemoteOutOfBandData(mAddress); } catch (RemoteException e) { Log.e(TAG, "", e); } return false; }
/** @hide */ public void setDiscoverableTimeout(int timeout) { if (getState() != STATE_ON) return; try { mService.setDiscoverableTimeout(timeout); } catch (RemoteException e) { Log.e(TAG, "", e); } }
/** @hide */ public boolean setPasskey(int passkey) { try { return sService.setPasskey(mAddress, passkey); } catch (RemoteException e) { Log.e(TAG, "", e); } return false; }
/** @hide */ public boolean setPairingConfirmation(boolean confirm) { try { return sService.setPairingConfirmation(mAddress, confirm); } catch (RemoteException e) { Log.e(TAG, "", e); } return false; }
/** @hide */ public boolean setPin(byte[] pin) { try { return sService.setPin(mAddress, pin); } catch (RemoteException e) { Log.e(TAG, "", e); } return false; }
/** @hide */ public int getServiceChannel(ParcelUuid uuid) { try { return sService.getRemoteServiceChannel(mAddress, uuid); } catch (RemoteException e) { Log.e(TAG, "", e); } return BluetoothDevice.ERROR; }
/** * Perform a SDP query on the remote device to get the UUIDs supported. This API is asynchronous * and an Intent is sent, with the UUIDs supported by the remote end. If there is an error in * getting the SDP records or if the process takes a long time, an Intent is sent with the UUIDs * that is currently present in the cache. Clients should use the {@link #getUuids} to get UUIDs * is SDP is not to be performed. * * @return False if the sanity check fails, True if the process of initiating an ACL connection to * the remote device was started. * @hide */ public boolean fetchUuidsWithSdp() { try { return sService.fetchRemoteUuids(mAddress, null, null); } catch (RemoteException e) { Log.e(TAG, "", e); } return false; }
/** @hide */ public ParcelUuid[] getUuids() { try { return sService.getRemoteUuids(mAddress); } catch (RemoteException e) { Log.e(TAG, "", e); } return null; }
/** * Set the friendly Bluetooth name of the local Bluetooth adapter. * * <p>This name is visible to remote Bluetooth devices. * * <p>Valid Bluetooth names are a maximum of 248 bytes using UTF-8 encoding, although many remote * devices can only display the first 40 characters, and some may be limited to just 20. * * <p>If Bluetooth state is not {@link #STATE_ON}, this API will return false. After turning on * Bluetooth, wait for {@link #ACTION_STATE_CHANGED} with {@link #STATE_ON} to get the updated * value. * * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} * * @param name a valid Bluetooth name * @return true if the name was set, false otherwise */ public boolean setName(String name) { if (getState() != STATE_ON) return false; try { return mService.setName(name); } catch (RemoteException e) { Log.e(TAG, "", e); } return false; }
/** * Return true if the local Bluetooth adapter is currently in the device discovery process. * * <p>Device discovery is a heavyweight procedure. New connections to remote Bluetooth devices * should not be attempted while discovery is in progress, and existing connections will * experience limited bandwidth and high latency. Use {@link #cancelDiscovery()} to cancel an * ongoing discovery. * * <p>Applications can also register for {@link #ACTION_DISCOVERY_STARTED} or {@link * #ACTION_DISCOVERY_FINISHED} to be notified when discovery starts or completes. * * <p>If Bluetooth state is not {@link #STATE_ON}, this API will return false. After turning on * Bluetooth, wait for {@link #ACTION_STATE_CHANGED} with {@link #STATE_ON} to get the updated * value. * * <p>Requires {@link android.Manifest.permission#BLUETOOTH}. * * @return true if discovering */ public boolean isDiscovering() { if (getState() != STATE_ON) return false; try { return mService.isDiscovering(); } catch (RemoteException e) { Log.e(TAG, "", e); } return false; }
/** * Set the Bluetooth scan mode of the local Bluetooth adapter. * * <p>The Bluetooth scan mode determines if the local adapter is connectable and/or discoverable * from remote Bluetooth devices. * * <p>For privacy reasons, discoverable mode is automatically turned off after <code>duration * </code> seconds. For example, 120 seconds should be enough for a remote device to initiate and * complete its discovery process. * * <p>Valid scan mode values are: {@link #SCAN_MODE_NONE}, {@link #SCAN_MODE_CONNECTABLE}, {@link * #SCAN_MODE_CONNECTABLE_DISCOVERABLE}. * * <p>If Bluetooth state is not {@link #STATE_ON}, this API will return false. After turning on * Bluetooth, wait for {@link #ACTION_STATE_CHANGED} with {@link #STATE_ON} to get the updated * value. * * <p>Requires {@link android.Manifest.permission#WRITE_SECURE_SETTINGS} * * <p>Applications cannot set the scan mode. They should use <code>startActivityForResult( * BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE}) * </code>instead. * * @param mode valid scan mode * @param duration time in seconds to apply scan mode, only used for {@link * #SCAN_MODE_CONNECTABLE_DISCOVERABLE} * @return true if the scan mode was set, false otherwise * @hide */ public boolean setScanMode(int mode, int duration) { if (getState() != STATE_ON) return false; try { return mService.setScanMode(mode, duration); } catch (RemoteException e) { Log.e(TAG, "", e); } return false; }
/** * Cancel the current device discovery process. * * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN}. * * <p>Because discovery is a heavyweight procedure for the Bluetooth adapter, this method should * always be called before attempting to connect to a remote device with {@link * android.bluetooth.BluetoothSocket#connect()}. Discovery is not managed by the Activity, but is * run as a system service, so an application should always call cancel discovery even if it did * not directly request a discovery, just to be sure. * * <p>If Bluetooth state is not {@link #STATE_ON}, this API will return false. After turning on * Bluetooth, wait for {@link #ACTION_STATE_CHANGED} with {@link #STATE_ON} to get the updated * value. * * @return true on success, false on error */ public boolean cancelDiscovery() { if (getState() != STATE_ON) return false; try { mService.cancelDiscovery(); } catch (RemoteException e) { Log.e(TAG, "", e); } return false; }
/** * Get the current Bluetooth scan mode of the local Bluetooth adapter. * * <p>The Bluetooth scan mode determines if the local adapter is connectable and/or discoverable * from remote Bluetooth devices. * * <p>Possible values are: {@link #SCAN_MODE_NONE}, {@link #SCAN_MODE_CONNECTABLE}, {@link * #SCAN_MODE_CONNECTABLE_DISCOVERABLE}. * * <p>If Bluetooth state is not {@link #STATE_ON}, this API will return {@link #SCAN_MODE_NONE}. * After turning on Bluetooth, wait for {@link #ACTION_STATE_CHANGED} with {@link #STATE_ON} to * get the updated value. * * <p>Requires {@link android.Manifest.permission#BLUETOOTH} * * @return scan mode */ public int getScanMode() { if (getState() != STATE_ON) return SCAN_MODE_NONE; try { return mService.getScanMode(); } catch (RemoteException e) { Log.e(TAG, "", e); } return SCAN_MODE_NONE; }
/** @hide */ public int getDiscoverableTimeout() { if (getState() != STATE_ON) return -1; try { return mService.getDiscoverableTimeout(); } catch (RemoteException e) { Log.e(TAG, "", e); } return -1; }