Java Code Examples for android.bluetooth.BluetoothGattCharacteristic#getWriteType()

The following examples show how to use android.bluetooth.BluetoothGattCharacteristic#getWriteType() . 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: DescriptorWriteOperation.java    From RxAndroidBle with Apache License 2.0 6 votes vote down vote up
@Override
protected boolean startOperation(BluetoothGatt bluetoothGatt) {
    bluetoothGattDescriptor.setValue(data);

    /*
    * According to the source code below Android 7.0.0 the BluetoothGatt.writeDescriptor() function used
    * writeType of the parent BluetoothCharacteristic which caused operation failure (for instance when
    * setting Client Characteristic Config). With WRITE_TYPE_DEFAULT problem did not occurred.
    * Compare:
    * https://android.googlesource.com/platform/frameworks/base/+/android-6.0.1_r74/core/java/android/bluetooth/BluetoothGatt.java#1039
    * https://android.googlesource.com/platform/frameworks/base/+/android-7.0.0_r1/core/java/android/bluetooth/BluetoothGatt.java#947
    */
    final BluetoothGattCharacteristic bluetoothGattCharacteristic = bluetoothGattDescriptor.getCharacteristic();
    final int originalWriteType = bluetoothGattCharacteristic.getWriteType();
    bluetoothGattCharacteristic.setWriteType(bluetoothGattCharacteristicDefaultWriteType);

    final boolean success = bluetoothGatt.writeDescriptor(bluetoothGattDescriptor);
    bluetoothGattCharacteristic.setWriteType(originalWriteType);
    return success;
}
 
Example 2
Source File: ConnectionImpl.java    From easyble-x with Apache License 2.0 5 votes vote down vote up
private boolean enableNotificationOrIndicationFail(boolean enable, boolean notification, BluetoothGattCharacteristic characteristic) {
    if (!bluetoothAdapter.isEnabled() || bluetoothGatt == null || !bluetoothGatt
            .setCharacteristicNotification(characteristic, enable)) {
        return true;
    }
    BluetoothGattDescriptor descriptor = characteristic.getDescriptor(clientCharacteristicConfig);
    if (descriptor == null) {
        return true;
    }
    byte[] originValue = descriptor.getValue();
    if (currentRequest != null) {
        if (currentRequest.type == RequestType.SET_NOTIFICATION || currentRequest.type == RequestType.SET_INDICATION) {
            currentRequest.descriptorTemp = originValue;
        }
    }
    if (enable) {
        if (notification) {
            descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
        } else {
            descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
        }
    } else {
        descriptor.setValue(BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE);
    }
    // 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.
    int writeType = characteristic.getWriteType();
    characteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT);
    boolean result = bluetoothGatt.writeDescriptor(descriptor);
    if (!enable) {
        //还原原始值
        descriptor.setValue(originValue);
    }
    characteristic.setWriteType(writeType);
    return !result;
}
 
Example 3
Source File: BleManagerHandler.java    From Android-BLE-Library with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * 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 4
Source File: BleManager.java    From Android-nRF-Toolbox with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * 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 5
Source File: BluetoothLeService.java    From OpenFit with MIT License 5 votes vote down vote up
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
    Log.d(LOG_TAG, "onServicesDiscovered: "+status);
    if(status == BluetoothGatt.GATT_SUCCESS) {    
        // loops through available GATT Services.
        for(BluetoothGattService gattService : gatt.getServices()) {
            String uuid = gattService.getUuid().toString();
            String type = gattServiceType[gattService.getType()];

            Log.d(LOG_TAG, "onServicesDiscovered type: "+type);
            Log.d(LOG_TAG, "onServicesDiscovered uuid: "+uuid);
            //Log.d(LOG_TAG, "onServicesDiscovered: getCharacteristic: "+mWriteCharacteristic);
            for(BluetoothGattCharacteristic gattCharacteristic : gattService.getCharacteristics()) {
                String cUuid = gattCharacteristic.getUuid().toString();
                int cInstanceId = gattCharacteristic.getInstanceId();
                int cPermissions = gattCharacteristic.getPermissions();
                int cProperties = gattCharacteristic.getProperties();
                byte[] cValue = gattCharacteristic.getValue();
                int cWriteType = gattCharacteristic.getWriteType();

                Log.d(LOG_TAG, "onServicesDiscovered cUuid: "+cUuid);
                Log.d(LOG_TAG, "onServicesDiscovered cInstanceId: "+cInstanceId);
                Log.d(LOG_TAG, "onServicesDiscovered cPermissions: "+cPermissions);
                Log.d(LOG_TAG, "onServicesDiscovered cProperties: "+cProperties);
                Log.d(LOG_TAG, "onServicesDiscovered cValue: "+cValue);
                Log.d(LOG_TAG, "onServicesDiscovered cWriteType: "+cWriteType);
            }
        }
        Log.d(LOG_TAG, "BluetoothLe Service discovered: "+status);
        broadcastUpdate(ACTION_GATT_SERVICES_DISCOVERED);
    }
    else {
        Log.d(LOG_TAG, "BluetoothLe onServicesDiscovered received: "+status);
    }
}
 
Example 6
Source File: P_Task_TestMtu.java    From AsteroidOSSync with GNU General Public License v3.0 4 votes vote down vote up
@Override protected void executeReadOrWrite()
{
    if( false == write_earlyOut(m_data) )
    {
        final BluetoothGattCharacteristic char_native = getFilteredCharacteristic() != null ? getFilteredCharacteristic() : getDevice().getNativeCharacteristic(getServiceUuid(), getCharUuid());

        if( char_native == null )
        {
            fail(BleDevice.ReadWriteListener.Status.NO_MATCHING_TARGET, BleStatuses.GATT_STATUS_NOT_APPLICABLE, getDefaultTarget(), getCharUuid(), BleDevice.ReadWriteListener.ReadWriteEvent.NON_APPLICABLE_UUID);
        }
        else
        {
            // Set the write type now, if it is not null
            if (m_writeType != null)
            {
                if (m_writeType == BleDevice.ReadWriteListener.Type.WRITE_NO_RESPONSE)
                {
                    char_native.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE);
                }
                else if (m_writeType == BleDevice.ReadWriteListener.Type.WRITE_SIGNED)
                {
                    char_native.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_SIGNED);
                }
                else if (char_native.getWriteType() != BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT)
                {
                    char_native.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT);
                }
            }

            if( false == getDevice().layerManager().setCharValue(char_native, m_data) )
            {
                fail(BleDevice.ReadWriteListener.Status.FAILED_TO_SET_VALUE_ON_TARGET, BleStatuses.GATT_STATUS_NOT_APPLICABLE, getDefaultTarget(), getCharUuid(), BleDevice.ReadWriteListener.ReadWriteEvent.NON_APPLICABLE_UUID);
            }
            else
            {
                if( false == getDevice().layerManager().writeCharacteristic(char_native) )
                {
                    fail(BleDevice.ReadWriteListener.Status.FAILED_TO_SEND_OUT, BleStatuses.GATT_STATUS_NOT_APPLICABLE, getDefaultTarget(), getCharUuid(), BleDevice.ReadWriteListener.ReadWriteEvent.NON_APPLICABLE_UUID);
                }
                else
                {
                    // SUCCESS, for now...
                }
            }
        }
    }
}
 
Example 7
Source File: P_Task_Write.java    From AsteroidOSSync with GNU General Public License v3.0 4 votes vote down vote up
@Override protected void executeReadOrWrite()
{
	m_data = m_futureData.getData();

	if( false == write_earlyOut(m_data) )
	{
		final BluetoothGattCharacteristic char_native = getFilteredCharacteristic() != null ? getFilteredCharacteristic() : getDevice().getNativeCharacteristic(getServiceUuid(), getCharUuid());

		if( char_native == null )
		{
			fail(Status.NO_MATCHING_TARGET, BleStatuses.GATT_STATUS_NOT_APPLICABLE, getDefaultTarget(), getCharUuid(), ReadWriteEvent.NON_APPLICABLE_UUID);
		}
		else
		{
			// Set the write type now, if it is not null
			if (m_writeType != null)
			{
				if (m_writeType == Type.WRITE_NO_RESPONSE)
				{
					char_native.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE);
				}
				else if (m_writeType == Type.WRITE_SIGNED)
				{
					char_native.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_SIGNED);
				}
				else if (char_native.getWriteType() != BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT)
				{
					char_native.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT);
				}
			}

			if( false == getDevice().layerManager().setCharValue(char_native, m_data) )
			{
				fail(Status.FAILED_TO_SET_VALUE_ON_TARGET, BleStatuses.GATT_STATUS_NOT_APPLICABLE, getDefaultTarget(), getCharUuid(), ReadWriteEvent.NON_APPLICABLE_UUID);
			}
			else
			{
				if( false == getDevice().layerManager().writeCharacteristic(char_native) )
				{
					fail(Status.FAILED_TO_SEND_OUT, BleStatuses.GATT_STATUS_NOT_APPLICABLE, getDefaultTarget(), getCharUuid(), ReadWriteEvent.NON_APPLICABLE_UUID);
				}
				else
				{
					// SUCCESS, for now...
				}
			}
		}
	}
}
 
Example 8
Source File: P_Task_TestMtu.java    From SweetBlue with GNU General Public License v3.0 4 votes vote down vote up
@Override protected void executeReadOrWrite()
{
    if( false == write_earlyOut(m_data) )
    {
        final BluetoothGattCharacteristic char_native = getFilteredCharacteristic() != null ? getFilteredCharacteristic() : getDevice().getNativeCharacteristic(getServiceUuid(), getCharUuid());

        if( char_native == null )
        {
            fail(BleDevice.ReadWriteListener.Status.NO_MATCHING_TARGET, BleStatuses.GATT_STATUS_NOT_APPLICABLE, getDefaultTarget(), getCharUuid(), BleDevice.ReadWriteListener.ReadWriteEvent.NON_APPLICABLE_UUID);
        }
        else
        {
            // Set the write type now, if it is not null
            if (m_writeType != null)
            {
                if (m_writeType == BleDevice.ReadWriteListener.Type.WRITE_NO_RESPONSE)
                {
                    char_native.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE);
                }
                else if (m_writeType == BleDevice.ReadWriteListener.Type.WRITE_SIGNED)
                {
                    char_native.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_SIGNED);
                }
                else if (char_native.getWriteType() != BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT)
                {
                    char_native.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT);
                }
            }

            if( false == getDevice().layerManager().setCharValue(char_native, m_data) )
            {
                fail(BleDevice.ReadWriteListener.Status.FAILED_TO_SET_VALUE_ON_TARGET, BleStatuses.GATT_STATUS_NOT_APPLICABLE, getDefaultTarget(), getCharUuid(), BleDevice.ReadWriteListener.ReadWriteEvent.NON_APPLICABLE_UUID);
            }
            else
            {
                if( false == getDevice().layerManager().writeCharacteristic(char_native) )
                {
                    fail(BleDevice.ReadWriteListener.Status.FAILED_TO_SEND_OUT, BleStatuses.GATT_STATUS_NOT_APPLICABLE, getDefaultTarget(), getCharUuid(), BleDevice.ReadWriteListener.ReadWriteEvent.NON_APPLICABLE_UUID);
                }
                else
                {
                    // SUCCESS, for now...
                }
            }
        }
    }
}
 
Example 9
Source File: P_Task_Write.java    From SweetBlue with GNU General Public License v3.0 4 votes vote down vote up
@Override protected void executeReadOrWrite()
{
	m_data = m_futureData.getData();

	if( false == write_earlyOut(m_data) )
	{
		final BluetoothGattCharacteristic char_native = getFilteredCharacteristic() != null ? getFilteredCharacteristic() : getDevice().getNativeCharacteristic(getServiceUuid(), getCharUuid());

		if( char_native == null )
		{
			fail(Status.NO_MATCHING_TARGET, BleStatuses.GATT_STATUS_NOT_APPLICABLE, getDefaultTarget(), getCharUuid(), ReadWriteEvent.NON_APPLICABLE_UUID);
		}
		else
		{
			// Set the write type now, if it is not null
			if (m_writeType != null)
			{
				if (m_writeType == Type.WRITE_NO_RESPONSE)
				{
					char_native.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE);
				}
				else if (m_writeType == Type.WRITE_SIGNED)
				{
					char_native.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_SIGNED);
				}
				else if (char_native.getWriteType() != BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT)
				{
					char_native.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT);
				}
			}

			if( false == getDevice().layerManager().setCharValue(char_native, m_data) )
			{
				fail(Status.FAILED_TO_SET_VALUE_ON_TARGET, BleStatuses.GATT_STATUS_NOT_APPLICABLE, getDefaultTarget(), getCharUuid(), ReadWriteEvent.NON_APPLICABLE_UUID);
			}
			else
			{
				if( false == getDevice().layerManager().writeCharacteristic(char_native) )
				{
					fail(Status.FAILED_TO_SEND_OUT, BleStatuses.GATT_STATUS_NOT_APPLICABLE, getDefaultTarget(), getCharUuid(), ReadWriteEvent.NON_APPLICABLE_UUID);
				}
				else
				{
					// SUCCESS, for now...
				}
			}
		}
	}
}
 
Example 10
Source File: Request.java    From Android-BLE-Library with BSD 3-Clause "New" or "Revised" License 3 votes vote down vote up
/**
 * Creates new Write Characteristic request. The request will not be executed if given
 * characteristic is null or does not have WRITE property.
 * After the operation is complete a proper callback will be invoked.
 *
 * @param characteristic characteristic to be written.
 * @param value          value to be written. The array is copied into another buffer so it's
 *                       safe to reuse the array again.
 * @return The new request.
 * @deprecated Access to this method will change to package-only.
 * Use {@link BleManager#writeCharacteristic(BluetoothGattCharacteristic, byte[])} instead.
 */
@Deprecated
@NonNull
public static WriteRequest newWriteRequest(
		@Nullable final BluetoothGattCharacteristic characteristic,
		@Nullable final byte[] value) {
	return new WriteRequest(Type.WRITE, characteristic, value, 0,
			value != null ? value.length : 0,
			characteristic != null ?
					characteristic.getWriteType() :
					BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT);
}
 
Example 11
Source File: Request.java    From Android-BLE-Library with BSD 3-Clause "New" or "Revised" License 3 votes vote down vote up
/**
 * Creates new Write Characteristic request. The request will not be executed if given
 * characteristic is null or does not have WRITE property.
 * After the operation is complete a proper callback will be invoked.
 *
 * @param characteristic characteristic to be written.
 * @param value          value to be written. The array is copied into another buffer so it's
 *                       safe to reuse the array again.
 * @param offset         the offset from which value has to be copied.
 * @param length         number of bytes to be copied from the value buffer.
 * @return The new request.
 * @deprecated Access to this method will change to package-only.
 * Use {@link BleManager#writeCharacteristic(BluetoothGattCharacteristic, byte[], int, int)}
 * instead.
 */
@Deprecated
@NonNull
public static WriteRequest newWriteRequest(
		@Nullable final BluetoothGattCharacteristic characteristic,
		@Nullable final byte[] value,
		@IntRange(from = 0) final int offset, @IntRange(from = 0) final int length) {
	return new WriteRequest(Type.WRITE, characteristic, value, offset, length,
			characteristic != null ?
					characteristic.getWriteType() :
					BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT);
}
 
Example 12
Source File: BleProfileApi.java    From Android-nRF-Toolbox with BSD 3-Clause "New" or "Revised" License 2 votes vote down vote up
/**
 * Creates new Write Characteristic request. The request will not be executed if given characteristic
 * is null or does not have WRITE property. After the operation is complete a proper callback will be invoked.
 * @param characteristic characteristic to be written
 * @param data data to be written. The array is copied into another buffer so it's safe to reuse the array again.
 * @return the new request that can be enqueued using {@link #enqueue(Request)} method.
 */
public static Request newWriteRequest(final BluetoothGattCharacteristic characteristic, final byte[] data) {
	return new Request(Type.WRITE, characteristic, characteristic.getWriteType(), data, 0, data != null ? data.length : 0);
}
 
Example 13
Source File: BleProfileApi.java    From Android-nRF-Toolbox with BSD 3-Clause "New" or "Revised" License 2 votes vote down vote up
/**
 * Creates new Write Characteristic request. The request will not be executed if given characteristic
 * is null or does not have WRITE property. After the operation is complete a proper callback will be invoked.
 * @param characteristic characteristic to be written
 * @param data data to be written. The array is copied into another buffer so it's safe to reuse the array again.
 * @param offset the offset from which data has to be copied
 * @param length number of bytes to be copied from the data buffer
 * @return the new request that can be enqueued using {@link #enqueue(Request)} method.
 */
public static Request newWriteRequest(final BluetoothGattCharacteristic characteristic, final byte[] data, final int offset, final int length) {
	return new Request(Type.WRITE, characteristic, characteristic.getWriteType(), data, offset, length);
}