diff options
Diffstat (limited to 'framework/java/android/bluetooth/BluetoothGatt.java')
-rw-r--r-- | framework/java/android/bluetooth/BluetoothGatt.java | 1038 |
1 files changed, 546 insertions, 492 deletions
diff --git a/framework/java/android/bluetooth/BluetoothGatt.java b/framework/java/android/bluetooth/BluetoothGatt.java index 678159b712..759d772920 100644 --- a/framework/java/android/bluetooth/BluetoothGatt.java +++ b/framework/java/android/bluetooth/BluetoothGatt.java @@ -118,18 +118,21 @@ public final class BluetoothGatt implements BluetoothProfile { /** * No authentication required. + * * @hide */ /*package*/ static final int AUTHENTICATION_NONE = 0; /** * Authentication requested; no man-in-the-middle protection required. + * * @hide */ /*package*/ static final int AUTHENTICATION_NO_MITM = 1; /** * Authentication with man-in-the-middle protection requested. + * * @hide */ /*package*/ static final int AUTHENTICATION_MITM = 2; @@ -138,498 +141,536 @@ public final class BluetoothGatt implements BluetoothProfile { * Bluetooth GATT callbacks. Overrides the default BluetoothGattCallback implementation. */ private final IBluetoothGattCallback mBluetoothGattCallback = - new IBluetoothGattCallback.Stub() { - /** - * Application interface registered - app is ready to go - * @hide - */ - @Override - public void onClientRegistered(int status, int clientIf) { - if (DBG) Log.d(TAG, "onClientRegistered() - status=" + status - + " clientIf=" + clientIf); - if (VDBG) { - synchronized(mStateLock) { - if (mConnState != CONN_STATE_CONNECTING) { - Log.e(TAG, "Bad connection state: " + mConnState); + new IBluetoothGattCallback.Stub() { + /** + * Application interface registered - app is ready to go + * @hide + */ + @Override + public void onClientRegistered(int status, int clientIf) { + if (DBG) { + Log.d(TAG, "onClientRegistered() - status=" + status + + " clientIf=" + clientIf); + } + if (VDBG) { + synchronized (mStateLock) { + if (mConnState != CONN_STATE_CONNECTING) { + Log.e(TAG, "Bad connection state: " + mConnState); + } } } + mClientIf = clientIf; + if (status != GATT_SUCCESS) { + runOrQueueCallback(new Runnable() { + @Override + public void run() { + if (mCallback != null) { + mCallback.onConnectionStateChange(BluetoothGatt.this, + GATT_FAILURE, + BluetoothProfile.STATE_DISCONNECTED); + } + } + }); + + synchronized (mStateLock) { + mConnState = CONN_STATE_IDLE; + } + return; + } + try { + mService.clientConnect(mClientIf, mDevice.getAddress(), + !mAutoConnect, mTransport, mOpportunistic, + mPhy); // autoConnect is inverse of "isDirect" + } catch (RemoteException e) { + Log.e(TAG, "", e); + } } - mClientIf = clientIf; - if (status != GATT_SUCCESS) { + + /** + * Phy update callback + * @hide + */ + @Override + public void onPhyUpdate(String address, int txPhy, int rxPhy, int status) { + if (DBG) { + Log.d(TAG, "onPhyUpdate() - status=" + status + + " address=" + address + " txPhy=" + txPhy + " rxPhy=" + rxPhy); + } + if (!address.equals(mDevice.getAddress())) { + return; + } + runOrQueueCallback(new Runnable() { @Override public void run() { if (mCallback != null) { - mCallback.onConnectionStateChange(BluetoothGatt.this, GATT_FAILURE, - BluetoothProfile.STATE_DISCONNECTED); + mCallback.onPhyUpdate(BluetoothGatt.this, txPhy, rxPhy, status); } } }); + } - synchronized(mStateLock) { - mConnState = CONN_STATE_IDLE; + /** + * Phy read callback + * @hide + */ + @Override + public void onPhyRead(String address, int txPhy, int rxPhy, int status) { + if (DBG) { + Log.d(TAG, "onPhyRead() - status=" + status + + " address=" + address + " txPhy=" + txPhy + " rxPhy=" + rxPhy); + } + if (!address.equals(mDevice.getAddress())) { + return; } - return; - } - try { - mService.clientConnect(mClientIf, mDevice.getAddress(), - !mAutoConnect, mTransport, mOpportunistic, mPhy); // autoConnect is inverse of "isDirect" - } catch (RemoteException e) { - Log.e(TAG,"",e); - } - } - /** - * Phy update callback - * @hide - */ - @Override - public void onPhyUpdate(String address, int txPhy, int rxPhy, int status) { - if (DBG) Log.d(TAG, "onPhyUpdate() - status=" + status - + " address=" + address + " txPhy=" + txPhy + " rxPhy=" + rxPhy); - if (!address.equals(mDevice.getAddress())) { - return; + runOrQueueCallback(new Runnable() { + @Override + public void run() { + if (mCallback != null) { + mCallback.onPhyRead(BluetoothGatt.this, txPhy, rxPhy, status); + } + } + }); } - runOrQueueCallback(new Runnable() { - @Override - public void run() { - if (mCallback != null) { - mCallback.onPhyUpdate(BluetoothGatt.this, txPhy, rxPhy, status); - } + /** + * Client connection state changed + * @hide + */ + @Override + public void onClientConnectionState(int status, int clientIf, + boolean connected, String address) { + if (DBG) { + Log.d(TAG, "onClientConnectionState() - status=" + status + + " clientIf=" + clientIf + " device=" + address); } - }); - } - - /** - * Phy read callback - * @hide - */ - @Override - public void onPhyRead(String address, int txPhy, int rxPhy, int status) { - if (DBG) Log.d(TAG, "onPhyRead() - status=" + status - + " address=" + address + " txPhy=" + txPhy + " rxPhy=" + rxPhy); - if (!address.equals(mDevice.getAddress())) { - return; - } + if (!address.equals(mDevice.getAddress())) { + return; + } + int profileState = connected ? BluetoothProfile.STATE_CONNECTED : + BluetoothProfile.STATE_DISCONNECTED; - runOrQueueCallback(new Runnable() { - @Override - public void run() { - if (mCallback != null) { - mCallback.onPhyRead(BluetoothGatt.this, txPhy, rxPhy, status); + runOrQueueCallback(new Runnable() { + @Override + public void run() { + if (mCallback != null) { + mCallback.onConnectionStateChange(BluetoothGatt.this, status, + profileState); + } } - } - }); - } + }); - /** - * Client connection state changed - * @hide - */ - @Override - public void onClientConnectionState(int status, int clientIf, - boolean connected, String address) { - if (DBG) Log.d(TAG, "onClientConnectionState() - status=" + status - + " clientIf=" + clientIf + " device=" + address); - if (!address.equals(mDevice.getAddress())) { - return; - } - int profileState = connected ? BluetoothProfile.STATE_CONNECTED : - BluetoothProfile.STATE_DISCONNECTED; - - runOrQueueCallback(new Runnable() { - @Override - public void run() { - if (mCallback != null) { - mCallback.onConnectionStateChange(BluetoothGatt.this, status, - profileState); + synchronized (mStateLock) { + if (connected) { + mConnState = CONN_STATE_CONNECTED; + } else { + mConnState = CONN_STATE_IDLE; } } - }); - synchronized(mStateLock) { - if (connected) { - mConnState = CONN_STATE_CONNECTED; - } else { - mConnState = CONN_STATE_IDLE; + synchronized (mDeviceBusy) { + mDeviceBusy = false; } } - synchronized(mDeviceBusy) { - mDeviceBusy = false; - } - } - - /** - * Remote search has been completed. - * The internal object structure should now reflect the state - * of the remote device database. Let the application know that - * we are done at this point. - * @hide - */ - @Override - public void onSearchComplete(String address, List<BluetoothGattService> services, - int status) { - if (DBG) Log.d(TAG, "onSearchComplete() = Device=" + address + " Status=" + status); - if (!address.equals(mDevice.getAddress())) { - return; - } - - for (BluetoothGattService s : services) { - //services we receive don't have device set properly. - s.setDevice(mDevice); - } - - mServices.addAll(services); - - // Fix references to included services, as they doesn't point to right objects. - for (BluetoothGattService fixedService : mServices) { - ArrayList<BluetoothGattService> includedServices = - new ArrayList(fixedService.getIncludedServices()); - fixedService.getIncludedServices().clear(); + /** + * Remote search has been completed. + * The internal object structure should now reflect the state + * of the remote device database. Let the application know that + * we are done at this point. + * @hide + */ + @Override + public void onSearchComplete(String address, List<BluetoothGattService> services, + int status) { + if (DBG) { + Log.d(TAG, + "onSearchComplete() = Device=" + address + " Status=" + status); + } + if (!address.equals(mDevice.getAddress())) { + return; + } - for(BluetoothGattService brokenRef : includedServices) { - BluetoothGattService includedService = getService(mDevice, - brokenRef.getUuid(), brokenRef.getInstanceId(), brokenRef.getType()); - if (includedService != null) { - fixedService.addIncludedService(includedService); - } else { - Log.e(TAG, "Broken GATT database: can't find included service."); - } + for (BluetoothGattService s : services) { + //services we receive don't have device set properly. + s.setDevice(mDevice); } - } - runOrQueueCallback(new Runnable() { - @Override - public void run() { - if (mCallback != null) { - mCallback.onServicesDiscovered(BluetoothGatt.this, status); + mServices.addAll(services); + + // Fix references to included services, as they doesn't point to right objects. + for (BluetoothGattService fixedService : mServices) { + ArrayList<BluetoothGattService> includedServices = + new ArrayList(fixedService.getIncludedServices()); + fixedService.getIncludedServices().clear(); + + for (BluetoothGattService brokenRef : includedServices) { + BluetoothGattService includedService = getService(mDevice, + brokenRef.getUuid(), brokenRef.getInstanceId(), + brokenRef.getType()); + if (includedService != null) { + fixedService.addIncludedService(includedService); + } else { + Log.e(TAG, "Broken GATT database: can't find included service."); + } } } - }); - } - /** - * Remote characteristic has been read. - * Updates the internal value. - * @hide - */ - @Override - public void onCharacteristicRead(String address, int status, int handle, byte[] value) { - if (VDBG) Log.d(TAG, "onCharacteristicRead() - Device=" + address - + " handle=" + handle + " Status=" + status); - - if (!address.equals(mDevice.getAddress())) { - return; + runOrQueueCallback(new Runnable() { + @Override + public void run() { + if (mCallback != null) { + mCallback.onServicesDiscovered(BluetoothGatt.this, status); + } + } + }); } - synchronized(mDeviceBusy) { - mDeviceBusy = false; - } + /** + * Remote characteristic has been read. + * Updates the internal value. + * @hide + */ + @Override + public void onCharacteristicRead(String address, int status, int handle, + byte[] value) { + if (VDBG) { + Log.d(TAG, "onCharacteristicRead() - Device=" + address + + " handle=" + handle + " Status=" + status); + } - if ((status == GATT_INSUFFICIENT_AUTHENTICATION - || status == GATT_INSUFFICIENT_ENCRYPTION) - && (mAuthRetryState != AUTH_RETRY_STATE_MITM)) { - try { - final int authReq = (mAuthRetryState == AUTH_RETRY_STATE_IDLE) ? - AUTHENTICATION_NO_MITM : AUTHENTICATION_MITM; - mService.readCharacteristic(mClientIf, address, handle, authReq); - mAuthRetryState++; + if (!address.equals(mDevice.getAddress())) { return; - } catch (RemoteException e) { - Log.e(TAG,"",e); } - } - mAuthRetryState = AUTH_RETRY_STATE_IDLE; + synchronized (mDeviceBusy) { + mDeviceBusy = false; + } - BluetoothGattCharacteristic characteristic = getCharacteristicById(mDevice, handle); - if (characteristic == null) { - Log.w(TAG, "onCharacteristicRead() failed to find characteristic!"); - return; - } + if ((status == GATT_INSUFFICIENT_AUTHENTICATION + || status == GATT_INSUFFICIENT_ENCRYPTION) + && (mAuthRetryState != AUTH_RETRY_STATE_MITM)) { + try { + final int authReq = (mAuthRetryState == AUTH_RETRY_STATE_IDLE) + ? AUTHENTICATION_NO_MITM : AUTHENTICATION_MITM; + mService.readCharacteristic(mClientIf, address, handle, authReq); + mAuthRetryState++; + return; + } catch (RemoteException e) { + Log.e(TAG, "", e); + } + } - if (status == 0) characteristic.setValue(value); + mAuthRetryState = AUTH_RETRY_STATE_IDLE; - runOrQueueCallback(new Runnable() { - @Override - public void run() { - if (mCallback != null) { - mCallback.onCharacteristicRead(BluetoothGatt.this, characteristic, - status); - } + BluetoothGattCharacteristic characteristic = getCharacteristicById(mDevice, + handle); + if (characteristic == null) { + Log.w(TAG, "onCharacteristicRead() failed to find characteristic!"); + return; } - }); - } - /** - * Characteristic has been written to the remote device. - * Let the app know how we did... - * @hide - */ - @Override - public void onCharacteristicWrite(String address, int status, int handle) { - if (VDBG) Log.d(TAG, "onCharacteristicWrite() - Device=" + address - + " handle=" + handle + " Status=" + status); - - if (!address.equals(mDevice.getAddress())) { - return; - } + if (status == 0) characteristic.setValue(value); - synchronized(mDeviceBusy) { - mDeviceBusy = false; + runOrQueueCallback(new Runnable() { + @Override + public void run() { + if (mCallback != null) { + mCallback.onCharacteristicRead(BluetoothGatt.this, characteristic, + status); + } + } + }); } - BluetoothGattCharacteristic characteristic = getCharacteristicById(mDevice, handle); - if (characteristic == null) return; + /** + * Characteristic has been written to the remote device. + * Let the app know how we did... + * @hide + */ + @Override + public void onCharacteristicWrite(String address, int status, int handle) { + if (VDBG) { + Log.d(TAG, "onCharacteristicWrite() - Device=" + address + + " handle=" + handle + " Status=" + status); + } - if ((status == GATT_INSUFFICIENT_AUTHENTICATION - || status == GATT_INSUFFICIENT_ENCRYPTION) - && (mAuthRetryState != AUTH_RETRY_STATE_MITM)) { - try { - final int authReq = (mAuthRetryState == AUTH_RETRY_STATE_IDLE) ? - AUTHENTICATION_NO_MITM : AUTHENTICATION_MITM; - mService.writeCharacteristic(mClientIf, address, handle, - characteristic.getWriteType(), authReq, characteristic.getValue()); - mAuthRetryState++; + if (!address.equals(mDevice.getAddress())) { return; - } catch (RemoteException e) { - Log.e(TAG,"",e); } - } - mAuthRetryState = AUTH_RETRY_STATE_IDLE; + synchronized (mDeviceBusy) { + mDeviceBusy = false; + } - runOrQueueCallback(new Runnable() { - @Override - public void run() { - if (mCallback != null) { - mCallback.onCharacteristicWrite(BluetoothGatt.this, characteristic, - status); + BluetoothGattCharacteristic characteristic = getCharacteristicById(mDevice, + handle); + if (characteristic == null) return; + + if ((status == GATT_INSUFFICIENT_AUTHENTICATION + || status == GATT_INSUFFICIENT_ENCRYPTION) + && (mAuthRetryState != AUTH_RETRY_STATE_MITM)) { + try { + final int authReq = (mAuthRetryState == AUTH_RETRY_STATE_IDLE) + ? AUTHENTICATION_NO_MITM : AUTHENTICATION_MITM; + mService.writeCharacteristic(mClientIf, address, handle, + characteristic.getWriteType(), authReq, + characteristic.getValue()); + mAuthRetryState++; + return; + } catch (RemoteException e) { + Log.e(TAG, "", e); } } - }); - } - /** - * Remote characteristic has been updated. - * Updates the internal value. - * @hide - */ - @Override - public void onNotify(String address, int handle, byte[] value) { - if (VDBG) Log.d(TAG, "onNotify() - Device=" + address + " handle=" + handle); - - if (!address.equals(mDevice.getAddress())) { - return; - } + mAuthRetryState = AUTH_RETRY_STATE_IDLE; - BluetoothGattCharacteristic characteristic = getCharacteristicById(mDevice, handle); - if (characteristic == null) return; + runOrQueueCallback(new Runnable() { + @Override + public void run() { + if (mCallback != null) { + mCallback.onCharacteristicWrite(BluetoothGatt.this, characteristic, + status); + } + } + }); + } - characteristic.setValue(value); + /** + * Remote characteristic has been updated. + * Updates the internal value. + * @hide + */ + @Override + public void onNotify(String address, int handle, byte[] value) { + if (VDBG) Log.d(TAG, "onNotify() - Device=" + address + " handle=" + handle); - runOrQueueCallback(new Runnable() { - @Override - public void run() { - if (mCallback != null) { - mCallback.onCharacteristicChanged(BluetoothGatt.this, characteristic); - } + if (!address.equals(mDevice.getAddress())) { + return; } - }); - } - /** - * Descriptor has been read. - * @hide - */ - @Override - public void onDescriptorRead(String address, int status, int handle, byte[] value) { - if (VDBG) Log.d(TAG, "onDescriptorRead() - Device=" + address + " handle=" + handle); + BluetoothGattCharacteristic characteristic = getCharacteristicById(mDevice, + handle); + if (characteristic == null) return; - if (!address.equals(mDevice.getAddress())) { - return; - } + characteristic.setValue(value); - synchronized(mDeviceBusy) { - mDeviceBusy = false; + runOrQueueCallback(new Runnable() { + @Override + public void run() { + if (mCallback != null) { + mCallback.onCharacteristicChanged(BluetoothGatt.this, + characteristic); + } + } + }); } - BluetoothGattDescriptor descriptor = getDescriptorById(mDevice, handle); - if (descriptor == null) return; - - if (status == 0) descriptor.setValue(value); + /** + * Descriptor has been read. + * @hide + */ + @Override + public void onDescriptorRead(String address, int status, int handle, byte[] value) { + if (VDBG) { + Log.d(TAG, + "onDescriptorRead() - Device=" + address + " handle=" + handle); + } - if ((status == GATT_INSUFFICIENT_AUTHENTICATION - || status == GATT_INSUFFICIENT_ENCRYPTION) - && (mAuthRetryState != AUTH_RETRY_STATE_MITM)) { - try { - final int authReq = (mAuthRetryState == AUTH_RETRY_STATE_IDLE) ? - AUTHENTICATION_NO_MITM : AUTHENTICATION_MITM; - mService.readDescriptor(mClientIf, address, handle, authReq); - mAuthRetryState++; + if (!address.equals(mDevice.getAddress())) { return; - } catch (RemoteException e) { - Log.e(TAG,"",e); } - } - mAuthRetryState = AUTH_RETRY_STATE_IDLE; + synchronized (mDeviceBusy) { + mDeviceBusy = false; + } - runOrQueueCallback(new Runnable() { - @Override - public void run() { - if (mCallback != null) { - mCallback.onDescriptorRead(BluetoothGatt.this, descriptor, status); + BluetoothGattDescriptor descriptor = getDescriptorById(mDevice, handle); + if (descriptor == null) return; + + if (status == 0) descriptor.setValue(value); + + if ((status == GATT_INSUFFICIENT_AUTHENTICATION + || status == GATT_INSUFFICIENT_ENCRYPTION) + && (mAuthRetryState != AUTH_RETRY_STATE_MITM)) { + try { + final int authReq = (mAuthRetryState == AUTH_RETRY_STATE_IDLE) + ? AUTHENTICATION_NO_MITM : AUTHENTICATION_MITM; + mService.readDescriptor(mClientIf, address, handle, authReq); + mAuthRetryState++; + return; + } catch (RemoteException e) { + Log.e(TAG, "", e); } } - }); - } - - /** - * Descriptor write operation complete. - * @hide - */ - @Override - public void onDescriptorWrite(String address, int status, int handle) { - if (VDBG) Log.d(TAG, "onDescriptorWrite() - Device=" + address + " handle=" + handle); - if (!address.equals(mDevice.getAddress())) { - return; - } + mAuthRetryState = AUTH_RETRY_STATE_IDLE; - synchronized(mDeviceBusy) { - mDeviceBusy = false; + runOrQueueCallback(new Runnable() { + @Override + public void run() { + if (mCallback != null) { + mCallback.onDescriptorRead(BluetoothGatt.this, descriptor, status); + } + } + }); } - BluetoothGattDescriptor descriptor = getDescriptorById(mDevice, handle); - if (descriptor == null) return; + /** + * Descriptor write operation complete. + * @hide + */ + @Override + public void onDescriptorWrite(String address, int status, int handle) { + if (VDBG) { + Log.d(TAG, + "onDescriptorWrite() - Device=" + address + " handle=" + handle); + } - if ((status == GATT_INSUFFICIENT_AUTHENTICATION - || status == GATT_INSUFFICIENT_ENCRYPTION) - && (mAuthRetryState != AUTH_RETRY_STATE_MITM)) { - try { - final int authReq = (mAuthRetryState == AUTH_RETRY_STATE_IDLE) ? - AUTHENTICATION_NO_MITM : AUTHENTICATION_MITM; - mService.writeDescriptor(mClientIf, address, handle, - authReq, descriptor.getValue()); - mAuthRetryState++; + if (!address.equals(mDevice.getAddress())) { return; - } catch (RemoteException e) { - Log.e(TAG,"",e); } - } - mAuthRetryState = AUTH_RETRY_STATE_IDLE; + synchronized (mDeviceBusy) { + mDeviceBusy = false; + } - runOrQueueCallback(new Runnable() { - @Override - public void run() { - if (mCallback != null) { - mCallback.onDescriptorWrite(BluetoothGatt.this, descriptor, status); + BluetoothGattDescriptor descriptor = getDescriptorById(mDevice, handle); + if (descriptor == null) return; + + if ((status == GATT_INSUFFICIENT_AUTHENTICATION + || status == GATT_INSUFFICIENT_ENCRYPTION) + && (mAuthRetryState != AUTH_RETRY_STATE_MITM)) { + try { + final int authReq = (mAuthRetryState == AUTH_RETRY_STATE_IDLE) + ? AUTHENTICATION_NO_MITM : AUTHENTICATION_MITM; + mService.writeDescriptor(mClientIf, address, handle, + authReq, descriptor.getValue()); + mAuthRetryState++; + return; + } catch (RemoteException e) { + Log.e(TAG, "", e); } } - }); - } - /** - * Prepared write transaction completed (or aborted) - * @hide - */ - @Override - public void onExecuteWrite(String address, int status) { - if (VDBG) Log.d(TAG, "onExecuteWrite() - Device=" + address - + " status=" + status); - if (!address.equals(mDevice.getAddress())) { - return; - } + mAuthRetryState = AUTH_RETRY_STATE_IDLE; - synchronized(mDeviceBusy) { - mDeviceBusy = false; + runOrQueueCallback(new Runnable() { + @Override + public void run() { + if (mCallback != null) { + mCallback.onDescriptorWrite(BluetoothGatt.this, descriptor, status); + } + } + }); } - runOrQueueCallback(new Runnable() { - @Override - public void run() { - if (mCallback != null) { - mCallback.onReliableWriteCompleted(BluetoothGatt.this, status); - } + /** + * Prepared write transaction completed (or aborted) + * @hide + */ + @Override + public void onExecuteWrite(String address, int status) { + if (VDBG) { + Log.d(TAG, "onExecuteWrite() - Device=" + address + + " status=" + status); + } + if (!address.equals(mDevice.getAddress())) { + return; } - }); - } - /** - * Remote device RSSI has been read - * @hide - */ - @Override - public void onReadRemoteRssi(String address, int rssi, int status) { - if (VDBG) Log.d(TAG, "onReadRemoteRssi() - Device=" + address + - " rssi=" + rssi + " status=" + status); - if (!address.equals(mDevice.getAddress())) { - return; - } - runOrQueueCallback(new Runnable() { - @Override - public void run() { - if (mCallback != null) { - mCallback.onReadRemoteRssi(BluetoothGatt.this, rssi, status); - } + synchronized (mDeviceBusy) { + mDeviceBusy = false; } - }); - } - /** - * Callback invoked when the MTU for a given connection changes - * @hide - */ - @Override - public void onConfigureMTU(String address, int mtu, int status) { - if (DBG) Log.d(TAG, "onConfigureMTU() - Device=" + address + - " mtu=" + mtu + " status=" + status); - if (!address.equals(mDevice.getAddress())) { - return; + runOrQueueCallback(new Runnable() { + @Override + public void run() { + if (mCallback != null) { + mCallback.onReliableWriteCompleted(BluetoothGatt.this, status); + } + } + }); } - runOrQueueCallback(new Runnable() { - @Override - public void run() { - if (mCallback != null) { - mCallback.onMtuChanged(BluetoothGatt.this, mtu, status); + /** + * Remote device RSSI has been read + * @hide + */ + @Override + public void onReadRemoteRssi(String address, int rssi, int status) { + if (VDBG) { + Log.d(TAG, "onReadRemoteRssi() - Device=" + address + + " rssi=" + rssi + " status=" + status); + } + if (!address.equals(mDevice.getAddress())) { + return; + } + runOrQueueCallback(new Runnable() { + @Override + public void run() { + if (mCallback != null) { + mCallback.onReadRemoteRssi(BluetoothGatt.this, rssi, status); + } } + }); + } + + /** + * Callback invoked when the MTU for a given connection changes + * @hide + */ + @Override + public void onConfigureMTU(String address, int mtu, int status) { + if (DBG) { + Log.d(TAG, "onConfigureMTU() - Device=" + address + + " mtu=" + mtu + " status=" + status); + } + if (!address.equals(mDevice.getAddress())) { + return; } - }); - } - /** - * Callback invoked when the given connection is updated - * @hide - */ - @Override - public void onConnectionUpdated(String address, int interval, int latency, - int timeout, int status) { - if (DBG) Log.d(TAG, "onConnectionUpdated() - Device=" + address + - " interval=" + interval + " latency=" + latency + - " timeout=" + timeout + " status=" + status); - if (!address.equals(mDevice.getAddress())) { - return; + runOrQueueCallback(new Runnable() { + @Override + public void run() { + if (mCallback != null) { + mCallback.onMtuChanged(BluetoothGatt.this, mtu, status); + } + } + }); } - runOrQueueCallback(new Runnable() { - @Override - public void run() { - if (mCallback != null) { - mCallback.onConnectionUpdated(BluetoothGatt.this, interval, latency, - timeout, status); - } + /** + * Callback invoked when the given connection is updated + * @hide + */ + @Override + public void onConnectionUpdated(String address, int interval, int latency, + int timeout, int status) { + if (DBG) { + Log.d(TAG, "onConnectionUpdated() - Device=" + address + + " interval=" + interval + " latency=" + latency + + " timeout=" + timeout + " status=" + status); + } + if (!address.equals(mDevice.getAddress())) { + return; } - }); - } - }; + + runOrQueueCallback(new Runnable() { + @Override + public void run() { + if (mCallback != null) { + mCallback.onConnectionUpdated(BluetoothGatt.this, interval, latency, + timeout, status); + } + } + }); + } + }; /*package*/ BluetoothGatt(IBluetoothGatt iGatt, BluetoothDevice device, - int transport, boolean opportunistic, int phy) { + int transport, boolean opportunistic, int phy) { mService = iGatt; mDevice = device; mTransport = transport; @@ -657,15 +698,16 @@ public final class BluetoothGatt implements BluetoothProfile { /** * Returns a service by UUID, instance and type. + * * @hide */ /*package*/ BluetoothGattService getService(BluetoothDevice device, UUID uuid, - int instanceId, int type) { - for(BluetoothGattService svc : mServices) { - if (svc.getDevice().equals(device) && - svc.getType() == type && - svc.getInstanceId() == instanceId && - svc.getUuid().equals(uuid)) { + int instanceId, int type) { + for (BluetoothGattService svc : mServices) { + if (svc.getDevice().equals(device) + && svc.getType() == type + && svc.getInstanceId() == instanceId + && svc.getUuid().equals(uuid)) { return svc; } } @@ -675,13 +717,16 @@ public final class BluetoothGatt implements BluetoothProfile { /** * Returns a characteristic with id equal to instanceId. + * * @hide */ - /*package*/ BluetoothGattCharacteristic getCharacteristicById(BluetoothDevice device, int instanceId) { - for(BluetoothGattService svc : mServices) { - for(BluetoothGattCharacteristic charac : svc.getCharacteristics()) { - if (charac.getInstanceId() == instanceId) + /*package*/ BluetoothGattCharacteristic getCharacteristicById(BluetoothDevice device, + int instanceId) { + for (BluetoothGattService svc : mServices) { + for (BluetoothGattCharacteristic charac : svc.getCharacteristics()) { + if (charac.getInstanceId() == instanceId) { return charac; + } } } return null; @@ -689,14 +734,16 @@ public final class BluetoothGatt implements BluetoothProfile { /** * Returns a descriptor with id equal to instanceId. + * * @hide */ /*package*/ BluetoothGattDescriptor getDescriptorById(BluetoothDevice device, int instanceId) { - for(BluetoothGattService svc : mServices) { - for(BluetoothGattCharacteristic charac : svc.getCharacteristics()) { - for(BluetoothGattDescriptor desc : charac.getDescriptors()) { - if (desc.getInstanceId() == instanceId) + for (BluetoothGattService svc : mServices) { + for (BluetoothGattCharacteristic charac : svc.getCharacteristics()) { + for (BluetoothGattDescriptor desc : charac.getDescriptors()) { + if (desc.getInstanceId() == instanceId) { return desc; + } } } } @@ -709,13 +756,13 @@ public final class BluetoothGatt implements BluetoothProfile { */ private void runOrQueueCallback(final Runnable cb) { if (mHandler == null) { - try { - cb.run(); - } catch (Exception ex) { - Log.w(TAG, "Unhandled exception in callback", ex); - } + try { + cb.run(); + } catch (Exception ex) { + Log.w(TAG, "Unhandled exception in callback", ex); + } } else { - mHandler.post(cb); + mHandler.post(cb); } } @@ -728,8 +775,8 @@ public final class BluetoothGatt implements BluetoothProfile { * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission. * * @param callback GATT callback handler that will receive asynchronous callbacks. - * @return If true, the callback will be called to notify success or failure, - * false on immediate error + * @return If true, the callback will be called to notify success or failure, false on immediate + * error */ private boolean registerApp(BluetoothGattCallback callback, Handler handler) { if (DBG) Log.d(TAG, "registerApp()"); @@ -743,7 +790,7 @@ public final class BluetoothGatt implements BluetoothProfile { try { mService.registerClient(new ParcelUuid(uuid), mBluetoothGattCallback); } catch (RemoteException e) { - Log.e(TAG,"",e); + Log.e(TAG, "", e); return false; } @@ -762,7 +809,7 @@ public final class BluetoothGatt implements BluetoothProfile { mService.unregisterClient(mClientIf); mClientIf = 0; } catch (RemoteException e) { - Log.e(TAG,"",e); + Log.e(TAG, "", e); } } @@ -784,15 +831,17 @@ public final class BluetoothGatt implements BluetoothProfile { * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission. * * @param device Remote device to connect to - * @param autoConnect Whether to directly connect to the remote device (false) - * or to automatically connect as soon as the remote - * device becomes available (true). + * @param autoConnect Whether to directly connect to the remote device (false) or to + * automatically connect as soon as the remote device becomes available (true). * @return true, if the connection attempt was initiated successfully */ /*package*/ boolean connect(Boolean autoConnect, BluetoothGattCallback callback, - Handler handler) { - if (DBG) Log.d(TAG, "connect() - device: " + mDevice.getAddress() + ", auto: " + autoConnect); - synchronized(mStateLock) { + Handler handler) { + if (DBG) { + Log.d(TAG, + "connect() - device: " + mDevice.getAddress() + ", auto: " + autoConnect); + } + synchronized (mStateLock) { if (mConnState != CONN_STATE_IDLE) { throw new IllegalStateException("Not idle"); } @@ -802,7 +851,7 @@ public final class BluetoothGatt implements BluetoothProfile { mAutoConnect = autoConnect; if (!registerApp(callback, handler)) { - synchronized(mStateLock) { + synchronized (mStateLock) { mConnState = CONN_STATE_IDLE; } Log.e(TAG, "Failed to register callback"); @@ -826,7 +875,7 @@ public final class BluetoothGatt implements BluetoothProfile { try { mService.clientDisconnect(mClientIf, mDevice.getAddress()); } catch (RemoteException e) { - Log.e(TAG,"",e); + Log.e(TAG, "", e); } } @@ -845,7 +894,7 @@ public final class BluetoothGatt implements BluetoothProfile { mOpportunistic, mPhy); // autoConnect is inverse of "isDirect" return true; } catch (RemoteException e) { - Log.e(TAG,"",e); + Log.e(TAG, "", e); return false; } } @@ -858,22 +907,22 @@ public final class BluetoothGatt implements BluetoothProfile { * {@link BluetoothGattCallback#onPhyUpdate} will be triggered as a result of this call, even * if no PHY change happens. It is also triggered when remote device updates the PHY. * - * @param txPhy preferred transmitter PHY. Bitwise OR of any of - * {@link BluetoothDevice#PHY_LE_1M_MASK}, {@link BluetoothDevice#PHY_LE_2M_MASK}, - * and {@link BluetoothDevice#PHY_LE_CODED_MASK}. - * @param rxPhy preferred receiver PHY. Bitwise OR of any of - * {@link BluetoothDevice#PHY_LE_1M_MASK}, {@link BluetoothDevice#PHY_LE_2M_MASK}, - * and {@link BluetoothDevice#PHY_LE_CODED_MASK}. + * @param txPhy preferred transmitter PHY. Bitwise OR of any of {@link + * BluetoothDevice#PHY_LE_1M_MASK}, {@link BluetoothDevice#PHY_LE_2M_MASK}, and {@link + * BluetoothDevice#PHY_LE_CODED_MASK}. + * @param rxPhy preferred receiver PHY. Bitwise OR of any of {@link + * BluetoothDevice#PHY_LE_1M_MASK}, {@link BluetoothDevice#PHY_LE_2M_MASK}, and {@link + * BluetoothDevice#PHY_LE_CODED_MASK}. * @param phyOptions preferred coding to use when transmitting on the LE Coded PHY. Can be one - * of {@link BluetoothDevice#PHY_OPTION_NO_PREFERRED}, - * {@link BluetoothDevice#PHY_OPTION_S2} or {@link BluetoothDevice#PHY_OPTION_S8} + * of {@link BluetoothDevice#PHY_OPTION_NO_PREFERRED}, {@link BluetoothDevice#PHY_OPTION_S2} or + * {@link BluetoothDevice#PHY_OPTION_S8} */ public void setPreferredPhy(int txPhy, int rxPhy, int phyOptions) { try { mService.clientSetPreferredPhy(mClientIf, mDevice.getAddress(), txPhy, rxPhy, - phyOptions); + phyOptions); } catch (RemoteException e) { - Log.e(TAG,"",e); + Log.e(TAG, "", e); } } @@ -885,7 +934,7 @@ public final class BluetoothGatt implements BluetoothProfile { try { mService.clientReadPhy(mClientIf, mDevice.getAddress()); } catch (RemoteException e) { - Log.e(TAG,"",e); + Log.e(TAG, "", e); } } @@ -920,7 +969,7 @@ public final class BluetoothGatt implements BluetoothProfile { try { mService.discoverServices(mClientIf, mDevice.getAddress()); } catch (RemoteException e) { - Log.e(TAG,"",e); + Log.e(TAG, "", e); return false; } @@ -960,8 +1009,8 @@ public final class BluetoothGatt implements BluetoothProfile { * * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission. * - * @return List of services on the remote device. Returns an empty list - * if service discovery has not yet been performed. + * @return List of services on the remote device. Returns an empty list if service discovery has + * not yet been performed. */ public List<BluetoothGattService> getServices() { List<BluetoothGattService> result = @@ -989,13 +1038,12 @@ public final class BluetoothGatt implements BluetoothProfile { * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission. * * @param uuid UUID of the requested service - * @return BluetoothGattService if supported, or null if the requested - * service is not offered by the remote device. + * @return BluetoothGattService if supported, or null if the requested service is not offered by + * the remote device. */ public BluetoothGattService getService(UUID uuid) { for (BluetoothGattService service : mServices) { - if (service.getDevice().equals(mDevice) && - service.getUuid().equals(uuid)) { + if (service.getDevice().equals(mDevice) && service.getUuid().equals(uuid)) { return service; } } @@ -1016,8 +1064,9 @@ public final class BluetoothGatt implements BluetoothProfile { * @return true, if the read operation was initiated successfully */ public boolean readCharacteristic(BluetoothGattCharacteristic characteristic) { - if ((characteristic.getProperties() & - BluetoothGattCharacteristic.PROPERTY_READ) == 0) return false; + if ((characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_READ) == 0) { + return false; + } if (VDBG) Log.d(TAG, "readCharacteristic() - uuid: " + characteristic.getUuid()); if (mService == null || mClientIf == 0) return false; @@ -1028,16 +1077,16 @@ public final class BluetoothGatt implements BluetoothProfile { BluetoothDevice device = service.getDevice(); if (device == null) return false; - synchronized(mDeviceBusy) { + synchronized (mDeviceBusy) { if (mDeviceBusy) return false; mDeviceBusy = true; } try { mService.readCharacteristic(mClientIf, device.getAddress(), - characteristic.getInstanceId(), AUTHENTICATION_NONE); + characteristic.getInstanceId(), AUTHENTICATION_NONE); } catch (RemoteException e) { - Log.e(TAG,"",e); + Log.e(TAG, "", e); mDeviceBusy = false; return false; } @@ -1062,16 +1111,16 @@ public final class BluetoothGatt implements BluetoothProfile { if (VDBG) Log.d(TAG, "readUsingCharacteristicUuid() - uuid: " + uuid); if (mService == null || mClientIf == 0) return false; - synchronized(mDeviceBusy) { + synchronized (mDeviceBusy) { if (mDeviceBusy) return false; mDeviceBusy = true; } try { mService.readUsingCharacteristicUuid(mClientIf, mDevice.getAddress(), - new ParcelUuid(uuid), startHandle, endHandle, AUTHENTICATION_NONE); + new ParcelUuid(uuid), startHandle, endHandle, AUTHENTICATION_NONE); } catch (RemoteException e) { - Log.e(TAG,"",e); + Log.e(TAG, "", e); mDeviceBusy = false; return false; } @@ -1094,8 +1143,10 @@ public final class BluetoothGatt implements BluetoothProfile { */ public boolean writeCharacteristic(BluetoothGattCharacteristic characteristic) { if ((characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_WRITE) == 0 - && (characteristic.getProperties() & - BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE) == 0) return false; + && (characteristic.getProperties() + & BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE) == 0) { + return false; + } if (VDBG) Log.d(TAG, "writeCharacteristic() - uuid: " + characteristic.getUuid()); if (mService == null || mClientIf == 0 || characteristic.getValue() == null) return false; @@ -1106,17 +1157,17 @@ public final class BluetoothGatt implements BluetoothProfile { BluetoothDevice device = service.getDevice(); if (device == null) return false; - synchronized(mDeviceBusy) { + synchronized (mDeviceBusy) { if (mDeviceBusy) return false; mDeviceBusy = true; } try { mService.writeCharacteristic(mClientIf, device.getAddress(), - characteristic.getInstanceId(), characteristic.getWriteType(), - AUTHENTICATION_NONE, characteristic.getValue()); + characteristic.getInstanceId(), characteristic.getWriteType(), + AUTHENTICATION_NONE, characteristic.getValue()); } catch (RemoteException e) { - Log.e(TAG,"",e); + Log.e(TAG, "", e); mDeviceBusy = false; return false; } @@ -1149,16 +1200,16 @@ public final class BluetoothGatt implements BluetoothProfile { BluetoothDevice device = service.getDevice(); if (device == null) return false; - synchronized(mDeviceBusy) { + synchronized (mDeviceBusy) { if (mDeviceBusy) return false; mDeviceBusy = true; } try { mService.readDescriptor(mClientIf, device.getAddress(), - descriptor.getInstanceId(), AUTHENTICATION_NONE); + descriptor.getInstanceId(), AUTHENTICATION_NONE); } catch (RemoteException e) { - Log.e(TAG,"",e); + Log.e(TAG, "", e); mDeviceBusy = false; return false; } @@ -1190,16 +1241,16 @@ public final class BluetoothGatt implements BluetoothProfile { BluetoothDevice device = service.getDevice(); if (device == null) return false; - synchronized(mDeviceBusy) { + synchronized (mDeviceBusy) { if (mDeviceBusy) return false; mDeviceBusy = true; } try { mService.writeDescriptor(mClientIf, device.getAddress(), descriptor.getInstanceId(), - AUTHENTICATION_NONE, descriptor.getValue()); + AUTHENTICATION_NONE, descriptor.getValue()); } catch (RemoteException e) { - Log.e(TAG,"",e); + Log.e(TAG, "", e); mDeviceBusy = false; return false; } @@ -1234,7 +1285,7 @@ public final class BluetoothGatt implements BluetoothProfile { try { mService.beginReliableWrite(mClientIf, mDevice.getAddress()); } catch (RemoteException e) { - Log.e(TAG,"",e); + Log.e(TAG, "", e); return false; } @@ -1258,7 +1309,7 @@ public final class BluetoothGatt implements BluetoothProfile { if (VDBG) Log.d(TAG, "executeReliableWrite() - device: " + mDevice.getAddress()); if (mService == null || mClientIf == 0) return false; - synchronized(mDeviceBusy) { + synchronized (mDeviceBusy) { if (mDeviceBusy) return false; mDeviceBusy = true; } @@ -1266,7 +1317,7 @@ public final class BluetoothGatt implements BluetoothProfile { try { mService.endReliableWrite(mClientIf, mDevice.getAddress(), true); } catch (RemoteException e) { - Log.e(TAG,"",e); + Log.e(TAG, "", e); mDeviceBusy = false; return false; } @@ -1289,7 +1340,7 @@ public final class BluetoothGatt implements BluetoothProfile { try { mService.endReliableWrite(mClientIf, mDevice.getAddress(), false); } catch (RemoteException e) { - Log.e(TAG,"",e); + Log.e(TAG, "", e); } } @@ -1316,9 +1367,11 @@ public final class BluetoothGatt implements BluetoothProfile { * @return true, if the requested notification status was set successfully */ public boolean setCharacteristicNotification(BluetoothGattCharacteristic characteristic, - boolean enable) { - if (DBG) Log.d(TAG, "setCharacteristicNotification() - uuid: " + characteristic.getUuid() - + " enable: " + enable); + boolean enable) { + if (DBG) { + Log.d(TAG, "setCharacteristicNotification() - uuid: " + characteristic.getUuid() + + " enable: " + enable); + } if (mService == null || mClientIf == 0) return false; BluetoothGattService service = characteristic.getService(); @@ -1329,9 +1382,9 @@ public final class BluetoothGatt implements BluetoothProfile { try { mService.registerForNotification(mClientIf, device.getAddress(), - characteristic.getInstanceId(), enable); + characteristic.getInstanceId(), enable); } catch (RemoteException e) { - Log.e(TAG,"",e); + Log.e(TAG, "", e); return false; } @@ -1341,6 +1394,7 @@ public final class BluetoothGatt implements BluetoothProfile { /** * Clears the internal cache and forces a refresh of the services from the * remote device. + * * @hide */ public boolean refresh() { @@ -1350,7 +1404,7 @@ public final class BluetoothGatt implements BluetoothProfile { try { mService.refreshDevice(mClientIf, mDevice.getAddress()); } catch (RemoteException e) { - Log.e(TAG,"",e); + Log.e(TAG, "", e); return false; } @@ -1374,7 +1428,7 @@ public final class BluetoothGatt implements BluetoothProfile { try { mService.readRemoteRssi(mClientIf, mDevice.getAddress()); } catch (RemoteException e) { - Log.e(TAG,"",e); + Log.e(TAG, "", e); return false; } @@ -1396,14 +1450,16 @@ public final class BluetoothGatt implements BluetoothProfile { * @return true, if the new MTU value has been requested successfully */ public boolean requestMtu(int mtu) { - if (DBG) Log.d(TAG, "configureMTU() - device: " + mDevice.getAddress() - + " mtu: " + mtu); + if (DBG) { + Log.d(TAG, "configureMTU() - device: " + mDevice.getAddress() + + " mtu: " + mtu); + } if (mService == null || mClientIf == 0) return false; try { mService.configureMTU(mClientIf, mDevice.getAddress(), mtu); } catch (RemoteException e) { - Log.e(TAG,"",e); + Log.e(TAG, "", e); return false; } @@ -1416,16 +1472,14 @@ public final class BluetoothGatt implements BluetoothProfile { * <p>This function will send a connection parameter update request to the * remote device. * - * @param connectionPriority Request a specific connection priority. Must be one of - * {@link BluetoothGatt#CONNECTION_PRIORITY_BALANCED}, - * {@link BluetoothGatt#CONNECTION_PRIORITY_HIGH} - * or {@link BluetoothGatt#CONNECTION_PRIORITY_LOW_POWER}. - * @throws IllegalArgumentException If the parameters are outside of their - * specified range. + * @param connectionPriority Request a specific connection priority. Must be one of {@link + * BluetoothGatt#CONNECTION_PRIORITY_BALANCED}, {@link BluetoothGatt#CONNECTION_PRIORITY_HIGH} + * or {@link BluetoothGatt#CONNECTION_PRIORITY_LOW_POWER}. + * @throws IllegalArgumentException If the parameters are outside of their specified range. */ public boolean requestConnectionPriority(int connectionPriority) { - if (connectionPriority < CONNECTION_PRIORITY_BALANCED || - connectionPriority > CONNECTION_PRIORITY_LOW_POWER) { + if (connectionPriority < CONNECTION_PRIORITY_BALANCED + || connectionPriority > CONNECTION_PRIORITY_LOW_POWER) { throw new IllegalArgumentException("connectionPriority not within valid range"); } @@ -1435,7 +1489,7 @@ public final class BluetoothGatt implements BluetoothProfile { try { mService.connectionParameterUpdate(mClientIf, mDevice.getAddress(), connectionPriority); } catch (RemoteException e) { - Log.e(TAG,"",e); + Log.e(TAG, "", e); return false; } @@ -1461,8 +1515,8 @@ public final class BluetoothGatt implements BluetoothProfile { */ @Override public List<BluetoothDevice> getConnectedDevices() { - throw new UnsupportedOperationException - ("Use BluetoothManager#getConnectedDevices instead."); + throw new UnsupportedOperationException( + "Use BluetoothManager#getConnectedDevices instead."); } /** @@ -1474,7 +1528,7 @@ public final class BluetoothGatt implements BluetoothProfile { */ @Override public List<BluetoothDevice> getDevicesMatchingConnectionStates(int[] states) { - throw new UnsupportedOperationException - ("Use BluetoothManager#getDevicesMatchingConnectionStates instead."); + throw new UnsupportedOperationException( + "Use BluetoothManager#getDevicesMatchingConnectionStates instead."); } } |