Java Code Examples for android.bluetooth.BluetoothGattCharacteristic#WRITE_TYPE_NO_RESPONSE

The following examples show how to use android.bluetooth.BluetoothGattCharacteristic#WRITE_TYPE_NO_RESPONSE . 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: MainActivity.java    From easyble-x with Apache License 2.0 6 votes vote down vote up
private void writeCharacteristic(@NotNull Item item) {
    Log.d("EasyBLE", "开始写入");
    WriteCharacteristicBuilder builder = new RequestBuilderFactory().getWriteCharacteristicBuilder(item.service.getUuid(), 
            item.characteristic.getUuid(), ("Multi-pass deformation also shows that in high-temperature rolling process, " +
                    "the material will be softened as a result of the recovery and recrystallization, " +
                    "so the rolling force is reduced and the time interval of the passes of rough rolling should be longer." +
                    "Multi-pass deformation also shows that in high-temperature rolling process, " +
                    "the material will be softened as a result of the recovery and recrystallization, " +
                    "so the rolling force is reduced and the time interval of the passes of rough rolling should be longer.").getBytes());
    //根据需要设置写入配置
    int writeType = connection.hasProperty(item.service.getUuid(), item.characteristic.getUuid(), 
            BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE) ? 
            BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE : BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT;
    builder.setWriteOptions(new WriteOptions.Builder()
            .setPackageSize(connection.getMtu() - 3)
            .setPackageWriteDelayMillis(5)
            .setRequestWriteDelayMillis(10)
            .setWaitWriteResult(true)
            .setWriteType(writeType)
            .build());
    //不设置回调,使用观察者模式接收结果
    builder.build().execute(connection);
}
 
Example 2
Source File: ConnectionImpl.java    From easyble-x with Apache License 2.0 6 votes vote down vote up
private boolean write(GenericRequest request, BluetoothGattCharacteristic characteristic, byte[] value) {
    characteristic.setValue(value);
    int writeType = request.writeOptions.writeType;
    if ((writeType == BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE ||
            writeType == BluetoothGattCharacteristic.WRITE_TYPE_SIGNED ||
            writeType == BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT)) {
        characteristic.setWriteType(writeType);
    }
    if (bluetoothGatt == null) {
        handleFailedCallback(request, REQUEST_FAIL_TYPE_GATT_IS_NULL, true);
        return false;
    }
    if (!bluetoothGatt.writeCharacteristic(characteristic)) {
        handleWriteFailed(request);
        return false;
    }
    return true;
}
 
Example 3
Source File: Peripheral.java    From react-native-ble-manager with Apache License 2.0 6 votes vote down vote up
private BluetoothGattCharacteristic findWritableCharacteristic(BluetoothGattService service,
		UUID characteristicUUID, int writeType) {
	try {
		// get write property
		int writeProperty = BluetoothGattCharacteristic.PROPERTY_WRITE;
		if (writeType == BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE) {
			writeProperty = BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE;
		}

		List<BluetoothGattCharacteristic> characteristics = service.getCharacteristics();
		for (BluetoothGattCharacteristic characteristic : characteristics) {
			if ((characteristic.getProperties() & writeProperty) != 0
					&& characteristicUUID.equals(characteristic.getUuid())) {
				return characteristic;
			}
		}

		// As a last resort, try and find ANY characteristic with this UUID, even if it
		// doesn't have the correct properties
		return service.getCharacteristic(characteristicUUID);
	} catch (Exception e) {
		Log.e(BleManager.LOG_TAG, "Error on findWritableCharacteristic", e);
		return null;
	}
}
 
Example 4
Source File: WriteOptions.java    From easyble-x with Apache License 2.0 5 votes vote down vote up
/**
 * 设置写入模式
 *
 * @param writeType {@link BluetoothGattCharacteristic#WRITE_TYPE_DEFAULT}
 *                  <br>{@link BluetoothGattCharacteristic#WRITE_TYPE_NO_RESPONSE}
 *                  <br>{@link BluetoothGattCharacteristic#WRITE_TYPE_SIGNED}
 */
public Builder setWriteType(int writeType) {
    if (writeType == BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT ||
            writeType == BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE ||
            writeType == BluetoothGattCharacteristic.WRITE_TYPE_SIGNED) {
        this.writeType = writeType;
    }            
    return this;
}
 
Example 5
Source File: BlePeripheralUart.java    From Bluefruit_LE_Connect_Android_V2 with MIT License 5 votes vote down vote up
private void uartSendPacket(@NonNull byte[] data, int offset, BluetoothGattCharacteristic uartTxCharacteristic, int withResponseEveryPacketCount, int numPacketsRemainingForDelay, BlePeripheral.ProgressHandler progressHandler, BlePeripheral.CompletionHandler completionHandler) {
    final int packetSize = Math.min(data.length - offset, mBlePeripheral.getMaxPacketLength());
    final byte[] packet = Arrays.copyOfRange(data, offset, offset + packetSize);
    final int writeStartingOffset = offset;
    final int uartTxCharacteristicWriteType = numPacketsRemainingForDelay <= 0 ? BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT : BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE;          // Send a packet WRITE_TYPE_DEFAULT to force wait until receive response and avoid dropping packets if the peripheral is not processing them fast enough

    mBlePeripheral.writeCharacteristic(uartTxCharacteristic, uartTxCharacteristicWriteType, packet, status -> {
        int writtenSize = writeStartingOffset;

        if (status != BluetoothGatt.GATT_SUCCESS) {
            Log.w(TAG, "Error " + status + " writing packet at offset" + writeStartingOffset + " Error: " + status);
        } else {
            Log.d(TAG, "uart tx " + (uartTxCharacteristicWriteType == BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE ? "withoutResponse" : "withResponse") + " offset " + writeStartingOffset + ": " + BleUtils.bytesToHex2(packet));

            writtenSize += packet.length;

            if (!mIsSendSequentiallyCancelled && writtenSize < data.length) {
                //int finalWrittenSize = writtenSize;
                //handler.postDelayed(() -> uartSendPacket(handler, data, finalWrittenSize, uartTxCharacteristic, uartTxCharacteristicWriteType, delayBetweenPackets, progressHandler, completionHandler), delayBetweenPackets);
                uartSendPacket(data, writtenSize, uartTxCharacteristic, withResponseEveryPacketCount, numPacketsRemainingForDelay <= 0 ? withResponseEveryPacketCount : numPacketsRemainingForDelay - 1, progressHandler, completionHandler);
            }
        }

        if (mIsSendSequentiallyCancelled) {
            completionHandler.completion(BluetoothGatt.GATT_SUCCESS);
        } else if (writtenSize >= data.length) {
            progressHandler.progress(1);
            completionHandler.completion(status);
        } else {
            progressHandler.progress(writtenSize / (float) data.length);
        }
    });
}
 
Example 6
Source File: BlePeripheral.java    From Bluefruit_LE_Connect_Android_V2 with MIT License 5 votes vote down vote up
public void writeCharacteristic(@NonNull BluetoothGattCharacteristic characteristic, int writeType, @NonNull byte[] data, @Nullable CompletionHandler completionHandler) {
    BleCommand command = new BleCommand(BleCommand.BLECOMMANDTYPE_WRITECHARACTERISTIC, kDebugCommands ? getCharacteristicIdentifier(characteristic) : null, completionHandler) {
        @Override
        public void execute() {
            if (mBluetoothGatt != null) {
                // Write value
                int selectedWriteType;
                if (kForceWriteWithoutResponse) {
                    selectedWriteType = BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE;
                } else {
                    selectedWriteType = writeType;
                }

                characteristic.setWriteType(selectedWriteType);
                characteristic.setValue(data);
                final boolean success = mBluetoothGatt.writeCharacteristic(characteristic);
                if (success) {

                    // Simulate response if needed
                    // Android: no need to simulate response: https://stackoverflow.com/questions/43741849/oncharacteristicwrite-and-onnotificationsent-are-being-called-too-fast-how-to/43744888
                    /*
                    if (selectedWriteType == BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE) {
                        finishExecutingCommand(BluetoothGatt.GATT_SUCCESS);
                    }*/
                } else {
                    Log.w(TAG, "writeCharacteristic could not be initiated");
                    finishExecutingCommand(BluetoothGatt.GATT_FAILURE);
                }

            } else {
                Log.w(TAG, "mBluetoothGatt is null");
                finishExecutingCommand(BluetoothGatt.GATT_FAILURE);
            }
        }
    };
    mCommmandQueue.add(command);
}
 
Example 7
Source File: ParserUtils.java    From Android-BLE-Library with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@NonNull
public static String writeTypeToString(@WriteType final int type) {
	switch (type) {
		case BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT:
			return "WRITE REQUEST";
		case BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE:
			return "WRITE COMMAND";
		case BluetoothGattCharacteristic.WRITE_TYPE_SIGNED:
			return "WRITE SIGNED";
		default:
			return "UNKNOWN (" + type + ")";
	}
}
 
Example 8
Source File: BluetoothDebug.java    From openScale with GNU General Public License v3.0 5 votes vote down vote up
private boolean isWriteType(int property, int writeType) {
    switch (property) {
        case BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE:
            return writeType == BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE;
        case BluetoothGattCharacteristic.PROPERTY_WRITE:
            return writeType == BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT;
        case BluetoothGattCharacteristic.PROPERTY_SIGNED_WRITE:
            return writeType == BluetoothGattCharacteristic.WRITE_TYPE_SIGNED;
    }
    return false;
}
 
Example 9
Source File: P_DeviceServiceManager.java    From AsteroidOSSync with GNU General Public License v3.0 4 votes vote down vote up
static BleDevice.ReadWriteListener.Type modifyResultType(BleCharacteristicWrapper char_native, BleDevice.ReadWriteListener.Type type)
{
	if( !char_native.isNull())
	{
		if( type == Type.NOTIFICATION )
		{
			if( (char_native.getCharacteristic().getProperties() & BluetoothGattCharacteristic.PROPERTY_NOTIFY) == 0x0 )
			{
				if( (char_native.getCharacteristic().getProperties() & BluetoothGattCharacteristic.PROPERTY_INDICATE) != 0x0 )
				{
					type = Type.INDICATION;
				}
			}
		}
		else if( type == Type.WRITE )
		{
			if( (char_native.getCharacteristic().getProperties() & BluetoothGattCharacteristic.PROPERTY_WRITE) == 0x0 )
			{
				if( (char_native.getCharacteristic().getProperties() & BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE) != 0x0 )
				{
					type = Type.WRITE_NO_RESPONSE;
				}
				else if( (char_native.getCharacteristic().getProperties() & BluetoothGattCharacteristic.PROPERTY_SIGNED_WRITE) != 0x0 )
				{
					type = Type.WRITE_SIGNED;
				}
			}
			//--- RB > Check the write type on the characteristic, in case this char has multiple write types
			int writeType = char_native.getCharacteristic().getWriteType();
			if (writeType == BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE)
			{
				type = Type.WRITE_NO_RESPONSE;
			}
			else if (writeType == BluetoothGattCharacteristic.WRITE_TYPE_SIGNED)
			{
				type = Type.WRITE_SIGNED;
			}
		}
	}
	
	return type;
}
 
Example 10
Source File: P_DeviceServiceManager.java    From SweetBlue with GNU General Public License v3.0 4 votes vote down vote up
static BleDevice.ReadWriteListener.Type modifyResultType(BleCharacteristicWrapper char_native, BleDevice.ReadWriteListener.Type type)
{
	if( !char_native.isNull())
	{
		if( type == Type.NOTIFICATION )
		{
			if( (char_native.getCharacteristic().getProperties() & BluetoothGattCharacteristic.PROPERTY_NOTIFY) == 0x0 )
			{
				if( (char_native.getCharacteristic().getProperties() & BluetoothGattCharacteristic.PROPERTY_INDICATE) != 0x0 )
				{
					type = Type.INDICATION;
				}
			}
		}
		else if( type == Type.WRITE )
		{
			if( (char_native.getCharacteristic().getProperties() & BluetoothGattCharacteristic.PROPERTY_WRITE) == 0x0 )
			{
				if( (char_native.getCharacteristic().getProperties() & BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE) != 0x0 )
				{
					type = Type.WRITE_NO_RESPONSE;
				}
				else if( (char_native.getCharacteristic().getProperties() & BluetoothGattCharacteristic.PROPERTY_SIGNED_WRITE) != 0x0 )
				{
					type = Type.WRITE_SIGNED;
				}
			}
			//--- RB > Check the write type on the characteristic, in case this char has multiple write types
			int writeType = char_native.getCharacteristic().getWriteType();
			if (writeType == BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE)
			{
				type = Type.WRITE_NO_RESPONSE;
			}
			else if (writeType == BluetoothGattCharacteristic.WRITE_TYPE_SIGNED)
			{
				type = Type.WRITE_SIGNED;
			}
		}
	}
	
	return type;
}
 
Example 11
Source File: OperationBuilder.java    From neatle with MIT License 2 votes vote down vote up
/**
 * Writes data to a characteristic of a service, but does not require a response from the BTLE
 * device.
 *
 * @param serviceUUID         the UUID of the service
 * @param characteristicsUUID the UUID of the characteristic.
 * @param source              the source of data for the write command
 * @param observer            the operation observer - callback
 * @return this object
 */
public OperationBuilder writeNoResponse(UUID serviceUUID, UUID characteristicsUUID, InputSource source, CommandObserver observer) {
    WriteCommand cmd = new WriteCommand(serviceUUID, characteristicsUUID, BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE, source, observer);
    commands.add(cmd);
    return this;
}