Java Code Examples for android.bluetooth.BluetoothGattDescriptor#getCharacteristic()

The following examples show how to use android.bluetooth.BluetoothGattDescriptor#getCharacteristic() . These examples are extracted from open source projects. 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 Project: SimpleBluetoothLeTerminal   File: SerialSocket.java    License: MIT License 6 votes vote down vote up
@Override
public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
    delegate.onDescriptorWrite(gatt, descriptor, status);
    if(canceled)
        return;
    if(descriptor.getCharacteristic() == readCharacteristic) {
        Log.d(TAG,"writing read characteristic descriptor finished, status="+status);
        if (status != BluetoothGatt.GATT_SUCCESS) {
            onSerialConnectError(new IOException("write descriptor failed"));
        } else {
            // onCharacteristicChanged with incoming data can happen after writeDescriptor(ENABLE_INDICATION/NOTIFICATION)
            // before confirmed by this method, so receive data can be shown before device is shown as 'Connected'.
            onSerialConnect();
            connected = true;
            Log.d(TAG, "connected");
        }
    }
}
 
Example 2
Source Project: SimpleBluetoothLeTerminal   File: SerialSocket.java    License: MIT License 6 votes vote down vote up
@Override
void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
    if(descriptor.getCharacteristic() == readCreditsCharacteristic) {
        Log.d(TAG, "writing read credits characteristic descriptor finished, status=" + status);
        if (status != BluetoothGatt.GATT_SUCCESS) {
            onSerialConnectError(new IOException("write credits descriptor failed"));
        } else {
            connectCharacteristics2(gatt);
        }
    }
    if(descriptor.getCharacteristic() == readCharacteristic) {
        Log.d(TAG, "writing read characteristic descriptor finished, status=" + status);
        if (status == BluetoothGatt.GATT_SUCCESS) {
            readCharacteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE);
            writeCharacteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE);
            grantReadCredits();
            // grantReadCredits includes gatt.writeCharacteristic(writeCreditsCharacteristic)
            // but we do not have to wait for confirmation, as it is the last write of connect phase.
        }
    }
}
 
Example 3
Source Project: sony-smartband-open-api   File: BtBridge.java    License: MIT License 6 votes vote down vote up
@Override
public void onDescriptorRead( BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status ) {
    if( status != BluetoothGatt.GATT_SUCCESS ) {
        Log.e( CLASS, "onDescriptorRead: Status != SUCCESS: status=" + status );
        return;
    }
    mGatt = gatt;

    if( descriptor == null ) {
        Log.e( CLASS, "onDescriptorRead: null descriptor" );
        return;
    }

    BluetoothGattCharacteristic characteristic = descriptor.getCharacteristic();
    for( BtBridgeListener listener : mListeners ) {
        listener.onDescriptorRead( characteristic, descriptor );
    }
}
 
Example 4
Source Project: sony-smartband-open-api   File: BtBridge.java    License: MIT License 6 votes vote down vote up
@Override
public void onDescriptorWrite( BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status ) {
    if( status != BluetoothGatt.GATT_SUCCESS ) {
        Log.e( CLASS, "onDescriptorWrite: Status != SUCCESS: status=" + status );
        return;
    }
    mGatt = gatt;

    if( descriptor == null ) {
        Log.e( CLASS, "onDescriptorWrite: null descriptor" );
        return;
    }

    BluetoothGattCharacteristic characteristic = descriptor.getCharacteristic();
    for( BtBridgeListener listener : mListeners ) {
        listener.onDescriptorWrite( characteristic, descriptor );
    }
}
 
Example 5
@Override
public void onDataAvailable(BluetoothGattDescriptor descriptor) {
    BluetoothGattCharacteristic characteristic = descriptor.getCharacteristic();
    BluetoothGattService service = characteristic.getService();
    final String key = new ElementPath(service.getUuid().toString(), service.getInstanceId(), characteristic.getUuid().toString(), descriptor.getUuid().toString(), null, null).getKey();
    final byte[] value = descriptor.getValue();
    mValuesMap.put(key, value);

    // Update UI
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            updateUI();
        }
    });
}
 
Example 6
/**
 * There was a bug in Android up to 6.0 where the descriptor was written using parent
 * characteristic's write type, instead of always Write With Response, as the spec says.
 * <p>
 * See: <a href="https://android.googlesource.com/platform/frameworks/base/+/942aebc95924ab1e7ea1e92aaf4e7fc45f695a6c%5E%21/#F0">
 * https://android.googlesource.com/platform/frameworks/base/+/942aebc95924ab1e7ea1e92aaf4e7fc45f695a6c%5E%21/#F0</a>
 *
 * @param descriptor the descriptor to be written
 * @return the result of {@link BluetoothGatt#writeDescriptor(BluetoothGattDescriptor)}
 */
private boolean internalWriteDescriptorWorkaround(@Nullable final BluetoothGattDescriptor descriptor) {
	final BluetoothGatt gatt = bluetoothGatt;
	if (gatt == null || descriptor == null || !connected)
		return false;

	final BluetoothGattCharacteristic parentCharacteristic = descriptor.getCharacteristic();
	final int originalWriteType = parentCharacteristic.getWriteType();
	parentCharacteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT);
	final boolean result = gatt.writeDescriptor(descriptor);
	parentCharacteristic.setWriteType(originalWriteType);
	return result;
}
 
Example 7
public P_Task_ReadDescriptor(BleDevice device, BluetoothGattDescriptor descriptor, Type type, boolean requiresBonding, ReadWriteListener readListener, BleTransaction txn, PE_TaskPriority priority)
{
	super(device, descriptor.getCharacteristic(), readListener, requiresBonding, txn, priority);
	
	m_type = type;
	m_descriptorUuid = descriptor.getUuid();
}
 
Example 8
public P_Task_WriteDescriptor(BleDevice device, BluetoothGattDescriptor descriptor, FutureData futureData, boolean requiresBonding, ReadWriteListener readListener, BleTransaction txn, PE_TaskPriority priority)
{
	super(device, descriptor.getCharacteristic(), readListener, requiresBonding, txn, priority);

	m_descriptorUuid = descriptor.getUuid();
	m_futureData = futureData;
}
 
Example 9
@Override
public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
    if (status == BluetoothGatt.GATT_SUCCESS) {
        BluetoothGattCharacteristic characteristic = descriptor.getCharacteristic();
        Log.d(TAG, "Characteristic onDescriptorWrite ch " + characteristic.getUuid());
        if(mHeartBeatCharacteristic.getUuid().equals(characteristic.getUuid())) {
            state_notifSetupSucess = true;
            setCharacteristicIndication(mReceiveDataCharacteristic);
        }
        if(mReceiveDataCharacteristic.getUuid().equals(characteristic.getUuid())) {
            setCharacteristicIndication(mResponseCharacteristic);
        }
        if(mResponseCharacteristic.getUuid().equals(characteristic.getUuid())) {
            attemptRead();
        }
    } else if ((status & BluetoothGatt.GATT_INSUFFICIENT_AUTHENTICATION) != 0 || (status & BluetoothGatt.GATT_INSUFFICIENT_ENCRYPTION) != 0) {
        if (gatt.getDevice().getBondState() == BluetoothDevice.BOND_NONE) {
            device = gatt.getDevice();
            state_authInProgress = true;
            bondDevice();
        } else {
            Log.e(TAG, "The phone is trying to read from paired device without encryption. Android Bug? Have the dexcom forget whatever device it was previously paired to: ondescriptorwrite code: "+status+ "bond: "+gatt.getDevice().getBondState());
        }
    } else {
        Log.e(TAG, "Unknown error writing descriptor");
    }
}
 
Example 10
@Override
public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
    if (status == BluetoothGatt.GATT_SUCCESS) {
        BluetoothGattCharacteristic characteristic = descriptor.getCharacteristic();
        Log.d(TAG, "Characteristic onDescriptorWrite ch " + characteristic.getUuid());
        if(mHeartBeatCharacteristic.getUuid().equals(characteristic.getUuid())) {
            state_notifSetupSucess = true;
            setCharacteristicIndication(mReceiveDataCharacteristic);
        }
        if(mReceiveDataCharacteristic.getUuid().equals(characteristic.getUuid())) {
            setCharacteristicIndication(mResponseCharacteristic);
        }
        if(mResponseCharacteristic.getUuid().equals(characteristic.getUuid())) {
            attemptRead();
        }
    } else if ((status & BluetoothGatt.GATT_INSUFFICIENT_AUTHENTICATION) != 0 || (status & BluetoothGatt.GATT_INSUFFICIENT_ENCRYPTION) != 0) {
        if (gatt.getDevice().getBondState() == BluetoothDevice.BOND_NONE) {
            device = gatt.getDevice();
            state_authInProgress = true;
            bondDevice();
        } else {
            Log.e(TAG, "The phone is trying to read from paired device without encryption. Android Bug? Have the dexcom forget whatever device it was previously paired to: ondescriptorwrite code: "+status+ "bond: "+gatt.getDevice().getBondState());
        }
    } else {
        Log.e(TAG, "Unknown error writing descriptor");
    }
}
 
Example 11
@Override
public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
    if (status == BluetoothGatt.GATT_SUCCESS) {
        BluetoothGattCharacteristic characteristic = descriptor.getCharacteristic();
        Log.d(TAG, "Characteristic onDescriptorWrite ch " + characteristic.getUuid());
        if(mHeartBeatCharacteristic.getUuid().equals(characteristic.getUuid())) {
            state_notifSetupSucess = true;
            setCharacteristicIndication(mReceiveDataCharacteristic);
        }
        if(mReceiveDataCharacteristic.getUuid().equals(characteristic.getUuid())) {
            setCharacteristicIndication(mResponseCharacteristic);
        }
        if(mResponseCharacteristic.getUuid().equals(characteristic.getUuid())) {
            attemptRead();
        }
    } else if ((status & BluetoothGatt.GATT_INSUFFICIENT_AUTHENTICATION) != 0 || (status & BluetoothGatt.GATT_INSUFFICIENT_ENCRYPTION) != 0) {
        if (gatt.getDevice().getBondState() == BluetoothDevice.BOND_NONE) {
            device = gatt.getDevice();
            state_authInProgress = true;
            bondDevice();
        } else {
            Log.e(TAG, "The phone is trying to read from paired device without encryption. Android Bug? Have the dexcom forget whatever device it was previously paired to: ondescriptorwrite code: "+status+ "bond: "+gatt.getDevice().getBondState());
        }
    } else {
        Log.e(TAG, "Unknown error writing descriptor");
    }
}
 
Example 12
@Override
public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
    if (status == BluetoothGatt.GATT_SUCCESS) {
        BluetoothGattCharacteristic characteristic = descriptor.getCharacteristic();
        Log.d(TAG, "Characteristic onDescriptorWrite ch " + characteristic.getUuid());
        if(mHeartBeatCharacteristic.getUuid().equals(characteristic.getUuid())) {
            state_notifSetupSucess = true;
            setCharacteristicIndication(mReceiveDataCharacteristic);
        }
        if(mReceiveDataCharacteristic.getUuid().equals(characteristic.getUuid())) {
            setCharacteristicIndication(mResponseCharacteristic);
        }
        if(mResponseCharacteristic.getUuid().equals(characteristic.getUuid())) {
            attemptRead();
        }
    } else if ((status & BluetoothGatt.GATT_INSUFFICIENT_AUTHENTICATION) != 0 || (status & BluetoothGatt.GATT_INSUFFICIENT_ENCRYPTION) != 0) {
        if (gatt.getDevice().getBondState() == BluetoothDevice.BOND_NONE) {
            device = gatt.getDevice();
            state_authInProgress = true;
            bondDevice();
        } else {
            Log.e(TAG, "The phone is trying to read from paired device without encryption. Android Bug? Have the dexcom forget whatever device it was previously paired to: ondescriptorwrite code: "+status+ "bond: "+gatt.getDevice().getBondState());
        }
    } else {
        Log.e(TAG, "Unknown error writing descriptor");
    }
}
 
Example 13
@Override
public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
    if (status == BluetoothGatt.GATT_SUCCESS) {
        BluetoothGattCharacteristic characteristic = descriptor.getCharacteristic();
        Log.d(TAG, "Characteristic onDescriptorWrite ch " + characteristic.getUuid());
        if(mHeartBeatCharacteristic.getUuid().equals(characteristic.getUuid())) {
            state_notifSetupSucess = true;
            setCharacteristicIndication(mReceiveDataCharacteristic);
        }
        if(mReceiveDataCharacteristic.getUuid().equals(characteristic.getUuid())) {
            setCharacteristicIndication(mResponseCharacteristic);
        }
        if(mResponseCharacteristic.getUuid().equals(characteristic.getUuid())) {
            attemptRead();
        }
    } else if ((status & BluetoothGatt.GATT_INSUFFICIENT_AUTHENTICATION) != 0 || (status & BluetoothGatt.GATT_INSUFFICIENT_ENCRYPTION) != 0) {
        if (gatt.getDevice().getBondState() == BluetoothDevice.BOND_NONE) {
            device = gatt.getDevice();
            state_authInProgress = true;
            bondDevice();
        } else {
            Log.e(TAG, "The phone is trying to read from paired device without encryption. Android Bug? Have the dexcom forget whatever device it was previously paired to");
        }
    } else {
        Log.e(TAG, "Unknown error writing descriptor");
    }
}
 
Example 14
/**
 * There was a bug in Android up to 6.0 where the descriptor was written using parent
 * characteristic's write type, instead of always Write With Response, as the spec says.
 * <p>
 *     See: <a href="https://android.googlesource.com/platform/frameworks/base/+/942aebc95924ab1e7ea1e92aaf4e7fc45f695a6c%5E%21/#F0">
 *         https://android.googlesource.com/platform/frameworks/base/+/942aebc95924ab1e7ea1e92aaf4e7fc45f695a6c%5E%21/#F0</a>
 * </p>
 * @param descriptor the descriptor to be written
 * @return the result of {@link BluetoothGatt#writeDescriptor(BluetoothGattDescriptor)}
 */
private boolean internalWriteDescriptorWorkaround(final BluetoothGattDescriptor descriptor) {
	final BluetoothGatt gatt = bluetoothGatt;
	if (gatt == null || descriptor == null)
		return false;

	final BluetoothGattCharacteristic parentCharacteristic = descriptor.getCharacteristic();
	final int originalWriteType = parentCharacteristic.getWriteType();
	parentCharacteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT);
	final boolean result = gatt.writeDescriptor(descriptor);
	parentCharacteristic.setWriteType(originalWriteType);
	return result;
}
 
Example 15
public P_Task_ReadDescriptor(BleDevice device, BluetoothGattDescriptor descriptor, Type type, boolean requiresBonding, ReadWriteListener readListener, BleTransaction txn, PE_TaskPriority priority)
{
	super(device, descriptor.getCharacteristic(), readListener, requiresBonding, txn, priority);
	
	m_type = type;
	m_descriptorUuid = descriptor.getUuid();
}
 
Example 16
public P_Task_WriteDescriptor(BleDevice device, BluetoothGattDescriptor descriptor, FutureData futureData, boolean requiresBonding, ReadWriteListener readListener, BleTransaction txn, PE_TaskPriority priority)
{
	super(device, descriptor.getCharacteristic(), readListener, requiresBonding, txn, priority);

	m_descriptorUuid = descriptor.getUuid();
	m_futureData = futureData;
}
 
Example 17
@Override
public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
    if (status == BluetoothGatt.GATT_SUCCESS) {
        BluetoothGattCharacteristic characteristic = descriptor.getCharacteristic();
        Log.d(TAG, "Characteristic onDescriptorWrite ch " + characteristic.getUuid());
        if(mHeartBeatCharacteristic.getUuid().equals(characteristic.getUuid())) {
            state_notifSetupSucess = true;
            setCharacteristicIndication(mReceiveDataCharacteristic);
        }
        if(mReceiveDataCharacteristic.getUuid().equals(characteristic.getUuid())) {
            setCharacteristicIndication(mResponseCharacteristic);
        }
        if(mResponseCharacteristic.getUuid().equals(characteristic.getUuid())) {
            attemptRead();
        }
    } else if ((status & BluetoothGatt.GATT_INSUFFICIENT_AUTHENTICATION) != 0 || (status & BluetoothGatt.GATT_INSUFFICIENT_ENCRYPTION) != 0) {
        if (gatt.getDevice().getBondState() == BluetoothDevice.BOND_NONE) {
            device = gatt.getDevice();
            state_authInProgress = true;
            bondDevice();
        } else {
            Log.e(TAG, "The phone is trying to read from paired device without encryption. Android Bug?");
        }
    } else {
        Log.e(TAG, "Unknown error writing descriptor");
    }
}
 
Example 18
Source Project: blessed-android   File: BluetoothPeripheral.java    License: MIT License 4 votes vote down vote up
@Override
public void onDescriptorWrite(BluetoothGatt gatt, final BluetoothGattDescriptor descriptor, final int status) {
    final BluetoothGattCharacteristic parentCharacteristic = descriptor.getCharacteristic();
    if (status != GATT_SUCCESS) {
        Timber.e("failed to write <%s> to descriptor of characteristic: <%s> for device: '%s', ", bytes2String(currentWriteBytes), parentCharacteristic.getUuid(), getAddress());
    }

    // Check if this was the Client Configuration Descriptor
    if (descriptor.getUuid().equals(UUID.fromString(CCC_DESCRIPTOR_UUID))) {
        if (status == GATT_SUCCESS) {
            byte[] value = descriptor.getValue();
            if (value != null) {
                if (Arrays.equals(value, BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE) ||
                    Arrays.equals(value, BluetoothGattDescriptor.ENABLE_INDICATION_VALUE)){
                    // Notify set to on, add it to the set of notifying characteristics
                    notifyingCharacteristics.add(parentCharacteristic.getUuid());
                    if (notifyingCharacteristics.size() > MAX_NOTIFYING_CHARACTERISTICS) {
                        Timber.e("too many (%d) notifying characteristics. The maximum Android can handle is %d", notifyingCharacteristics.size(), MAX_NOTIFYING_CHARACTERISTICS);
                    }
                } else if (Arrays.equals(value, BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE)){
                    // Notify was turned off, so remove it from the set of notifying characteristics
                    notifyingCharacteristics.remove(parentCharacteristic.getUuid());
                } else {
                    Timber.e("unexpected CCC descriptor value");
                }
            }
        }

        callbackHandler.post(new Runnable() {
            @Override
            public void run() {
                peripheralCallback.onNotificationStateUpdate(BluetoothPeripheral.this, parentCharacteristic, status);
            }
        });
    } else {
        callbackHandler.post(new Runnable() {
            @Override
            public void run() {
                peripheralCallback.onDescriptorWrite(BluetoothPeripheral.this, currentWriteBytes, descriptor, status);
            }
        });
    }
    completedCommand();
}
 
Example 19
@Override
public void onDescriptorWriteRequest(BluetoothDevice device, int requestId,
    BluetoothGattDescriptor descriptor, boolean preparedWrite, boolean responseNeeded,
    int offset,
    byte[] value) {
  super.onDescriptorWriteRequest(device, requestId, descriptor, preparedWrite, responseNeeded,
      offset, value);
  Log.v(TAG, "Descriptor Write Request " + descriptor.getUuid() + " " + Arrays.toString(value));
  int status = BluetoothGatt.GATT_SUCCESS;
  if (descriptor.getUuid() == CLIENT_CHARACTERISTIC_CONFIGURATION_UUID) {
    BluetoothGattCharacteristic characteristic = descriptor.getCharacteristic();
    boolean supportsNotifications = (characteristic.getProperties() &
        BluetoothGattCharacteristic.PROPERTY_NOTIFY) != 0;
    boolean supportsIndications = (characteristic.getProperties() &
        BluetoothGattCharacteristic.PROPERTY_INDICATE) != 0;

    if (!(supportsNotifications || supportsIndications)) {
      status = BluetoothGatt.GATT_REQUEST_NOT_SUPPORTED;
    } else if (value.length != 2) {
      status = BluetoothGatt.GATT_INVALID_ATTRIBUTE_LENGTH;
    } else if (Arrays.equals(value, BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE)) {
      status = BluetoothGatt.GATT_SUCCESS;
      mCurrentServiceFragment.notificationsDisabled(characteristic);
      descriptor.setValue(value);
    } else if (supportsNotifications &&
        Arrays.equals(value, BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE)) {
      status = BluetoothGatt.GATT_SUCCESS;
      mCurrentServiceFragment.notificationsEnabled(characteristic, false /* indicate */);
      descriptor.setValue(value);
    } else if (supportsIndications &&
        Arrays.equals(value, BluetoothGattDescriptor.ENABLE_INDICATION_VALUE)) {
      status = BluetoothGatt.GATT_SUCCESS;
      mCurrentServiceFragment.notificationsEnabled(characteristic, true /* indicate */);
      descriptor.setValue(value);
    } else {
      status = BluetoothGatt.GATT_REQUEST_NOT_SUPPORTED;
    }
  } else {
    status = BluetoothGatt.GATT_SUCCESS;
    descriptor.setValue(value);
  }
  if (responseNeeded) {
    mGattServer.sendResponse(device, requestId, status,
        /* No need to respond with offset */ 0,
        /* No need to respond with a value */ null);
  }
}
 
Example 20
Source Project: sony-smartband-open-api   File: BtBand.java    License: MIT License 4 votes vote down vote up
private DescriptorInfo getDescriptorInfo( BluetoothGattDescriptor descriptor ) {
    BluetoothGattCharacteristic characteristic = descriptor.getCharacteristic();
    return getDescriptorInfo( characteristic.getService().getUuid(), characteristic.getUuid(), descriptor.getUuid() );
}