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

The following examples show how to use android.bluetooth.BluetoothGattCharacteristic#getProperties() . 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: BluetoothLeDeviceBase.java    From BlogPracticeDems with Apache License 2.0 6 votes vote down vote up
private void setNotify(BluetoothGattCharacteristic characteristic) {

        final int charaProp = characteristic.getProperties();
        if ((charaProp | BluetoothGattCharacteristic.PROPERTY_READ) > 0) {
            // If there is an active notification on a characteristic, clear
            // it first so it doesn't update the data field on the user interface.
            if (mNotifyCharacteristic1 != null) {
                setCharacteristicNotification(
                        mNotifyCharacteristic1, false);
                mNotifyCharacteristic1 = null;
            }
            readCharacteristic(characteristic);
        }
        if ((charaProp | BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0) {
            mNotifyCharacteristic1 = characteristic;
            setCharacteristicNotification(
                    characteristic, true);
        }
    }
 
Example 2
Source File: AbstractHOGPServer.java    From DeviceConnect-Android with MIT License 6 votes vote down vote up
@Override
public void onCharacteristicWriteRequest(final BluetoothDevice device, final int requestId,
                                         final BluetoothGattCharacteristic characteristic,
                                         final boolean preparedWrite, final boolean responseNeeded,
                                         final int offset, final byte[] value) {
    if (DEBUG) {
        Log.d(TAG, "onCharacteristicWriteRequest characteristic: " + characteristic.getUuid() + ", value: " + Arrays.toString(value));
    }

    if (mGattServer == null) {
        return;
    }

    if (responseNeeded) {
        if (BleUuidUtils.matches(CHARACTERISTIC_REPORT, characteristic.getUuid())) {
            if (characteristic.getProperties() == (BluetoothGattCharacteristic.PROPERTY_READ | BluetoothGattCharacteristic.PROPERTY_WRITE | BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE)) {
                onOutputReport(value);
            }
            mGattServer.sendResponse(device, requestId, BluetoothGatt.GATT_SUCCESS, 0, EMPTY_BYTES);
        } else {
            mGattServer.sendResponse(device, requestId, BluetoothGatt.GATT_FAILURE, 0, EMPTY_BYTES);
        }
    }
}
 
Example 3
Source File: SerialSocket.java    From SimpleBluetoothLeTerminal with MIT License 6 votes vote down vote up
@Override
boolean connectCharacteristics(BluetoothGattService gattService) {
    Log.d(TAG, "service nrf uart");
    BluetoothGattCharacteristic rw2 = gattService.getCharacteristic(BLUETOOTH_LE_NRF_CHAR_RW2);
    BluetoothGattCharacteristic rw3 = gattService.getCharacteristic(BLUETOOTH_LE_NRF_CHAR_RW3);
    if (rw2 != null && rw3 != null) {
        int rw2prop = rw2.getProperties();
        int rw3prop = rw3.getProperties();
        boolean rw2write = (rw2prop & BluetoothGattCharacteristic.PROPERTY_WRITE) != 0;
        boolean rw3write = (rw3prop & BluetoothGattCharacteristic.PROPERTY_WRITE) != 0;
        Log.d(TAG, "characteristic properties " + rw2prop + "/" + rw3prop);
        if (rw2write && rw3write) {
            onSerialConnectError(new IOException("multiple write characteristics (" + rw2prop + "/" + rw3prop + ")"));
        } else if (rw2write) {
            writeCharacteristic = rw2;
            readCharacteristic = rw3;
        } else if (rw3write) {
            writeCharacteristic = rw3;
            readCharacteristic = rw2;
        } else {
            onSerialConnectError(new IOException("no write characteristic (" + rw2prop + "/" + rw3prop + ")"));
        }
    }
    return true;
}
 
Example 4
Source File: BluetoothLEGatt.java    From EFRConnect-android with Apache License 2.0 6 votes vote down vote up
void addCharacteristics(BluetoothGatt gatt, List<BluetoothGattCharacteristic> gattCharacteristics) {
    // Loops through available Characteristics.
    for (BluetoothGattCharacteristic characteristic : gattCharacteristics) {
        if (BuildConfig.DEBUG) {
            String characteristicName = getCharacteristicName(characteristic.getUuid());
            Timber.d("    char: " + characteristicName);
            //Log.d("addCharacteristics"," char: " + characteristicName);
        }

        final int characteristicID = getIdentification(characteristic.getUuid());
        this.gattCharacteristics.put(characteristicID, characteristic);
        this.values.put(characteristicID, null);

        int properties = characteristic.getProperties();
        if ((properties & BluetoothGattCharacteristic.PROPERTY_NOTIFY) != 0) {
            gatt.setCharacteristicNotification(characteristic, true);
        }
    }
}
 
Example 5
Source File: BlePeripheral.java    From Bluefruit_LE_Connect_Android_V2 with MIT License 5 votes vote down vote up
private static int getCharacteristicProperties(BluetoothGattService service, UUID characteristicUUID) {
    BluetoothGattCharacteristic characteristic = service.getCharacteristic(characteristicUUID);
    int properties = 0;
    if (characteristic != null) {
        properties = characteristic.getProperties();
    }

    return properties;
}
 
Example 6
Source File: BleManagerHandler.java    From Android-BLE-Library with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Returns the Client Characteristic Config Descriptor if the characteristic has the
 * required property. It may return null if the CCCD is not there.
 *
 * @param characteristic   the characteristic to look the CCCD in.
 * @param requiredProperty the required property: {@link BluetoothGattCharacteristic#PROPERTY_NOTIFY}
 *                         or {@link BluetoothGattCharacteristic#PROPERTY_INDICATE}.
 * @return The CCC descriptor or null if characteristic is null, if it doesn't have the
 * required property, or if the CCCD is missing.
 */
private static BluetoothGattDescriptor getCccd(@Nullable final BluetoothGattCharacteristic characteristic,
											   final int requiredProperty) {
	if (characteristic == null)
		return null;

	// Check characteristic property
	final int properties = characteristic.getProperties();
	if ((properties & requiredProperty) == 0)
		return null;

	return characteristic.getDescriptor(BleManager.CLIENT_CHARACTERISTIC_CONFIG_DESCRIPTOR_UUID);
}
 
Example 7
Source File: DexCollectionService.java    From xDrip-Experimental with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
    if (status != BluetoothGatt.GATT_SUCCESS) {
        Log.d(TAG, "onServicesDiscovered received: " + status);
        return;
    }

    Log.d(TAG, "onServicesDiscovered received status: " + status);

    final BluetoothGattService gattService = mBluetoothGatt.getService(xDripDataService);
    if (gattService == null) {
        Log.w(TAG, "onServicesDiscovered: service " + xDripDataService + " not found");
        listAvailableServices(mBluetoothGatt);
        return;
    }

    final BluetoothGattCharacteristic gattCharacteristic = gattService.getCharacteristic(xDripDataCharacteristic);
    if (gattCharacteristic == null) {
        Log.w(TAG, "onServicesDiscovered: characteristic " + xDripDataCharacteristic + " not found");
        return;
    }

    mCharacteristic = gattCharacteristic;
    final int charaProp = gattCharacteristic.getProperties();
    if ((charaProp | BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0) {
        mBluetoothGatt.setCharacteristicNotification(gattCharacteristic, true);
    } else {
        Log.w(TAG, "onServicesDiscovered: characteristic " + xDripDataCharacteristic + " not found");
    }
}
 
Example 8
Source File: BluetoothLeService.java    From IoT-Firstep with GNU General Public License v3.0 5 votes vote down vote up
private void broadcastUpdate(final String action,
                             final BluetoothGattCharacteristic characteristic) {
    final Intent intent = new Intent(action);

    // This is special handling for the Heart Rate Measurement profile.  Data parsing is
    // carried out as per profile specifications:
    // http://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.heart_rate_measurement.xml
    if (UUID_HEART_RATE_MEASUREMENT.equals(characteristic.getUuid())) {
        int flag = characteristic.getProperties();
        int format = -1;
        if ((flag & 0x01) != 0) {
            format = BluetoothGattCharacteristic.FORMAT_UINT16;
            Log.d(TAG, "Heart rate format UINT16.");
        } else {
            format = BluetoothGattCharacteristic.FORMAT_UINT8;
            Log.d(TAG, "Heart rate format UINT8.");
        }
        final int heartRate = characteristic.getIntValue(format, 1);
        Log.d(TAG, String.format("Received heart rate: %d", heartRate));
        intent.putExtra(EXTRA_DATA, String.valueOf(heartRate));
    } else {
        // For all other profiles, writes the data formatted in HEX.
        final byte[] data = characteristic.getValue();
        //System.out.println("LeService ---->" + new String(data));
        if (data != null && data.length > 0) {
            intent.putExtra(EXTRA_DATA, data);
        }
    }
    sendBroadcast(intent);
}
 
Example 9
Source File: BleGattExecutor.java    From Bluefruit_LE_Connect_Android with MIT License 5 votes vote down vote up
private BleGattExecutor.ServiceAction serviceReadAction(final BluetoothGattService gattService, final String characteristicUuidString, final String descriptorUuidString) {
    return new BleGattExecutor.ServiceAction() {
        @Override
        public boolean execute(BluetoothGatt bluetoothGatt) {
            final UUID characteristicUuid = UUID.fromString(characteristicUuidString);
            final BluetoothGattCharacteristic characteristic = gattService.getCharacteristic(characteristicUuid);
            if (characteristic != null) {
                if (descriptorUuidString == null) {
                    // Read Characteristic
                    if ((characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_READ) != 0) {
                        bluetoothGatt.readCharacteristic(characteristic);
                        return false;
                    } else {
                        Log.w(TAG, "read: characteristic not readable: " + characteristicUuidString);
                        return true;
                    }
                } else {
                    // Read Descriptor
                    final UUID descriptorUuid = UUID.fromString(descriptorUuidString);
                    final BluetoothGattDescriptor descriptor = characteristic.getDescriptor(descriptorUuid);
                    if (descriptor != null) {
                        bluetoothGatt.readDescriptor(descriptor);
                        return false;
                    } else {
                        Log.w(TAG, "read: descriptor not found: " + descriptorUuidString);
                        return true;
                    }
                }
            } else {
                Log.w(TAG, "read: characteristic not found: " + characteristicUuidString);
                return true;
            }
        }
    };
}
 
Example 10
Source File: BleManager.java    From BLEChat with GNU General Public License v3.0 5 votes vote down vote up
private boolean isWritableCharacteristic(BluetoothGattCharacteristic chr) {
	if(chr == null) return false;
	
	final int charaProp = chr.getProperties();
	if (((charaProp & BluetoothGattCharacteristic.PROPERTY_WRITE) |
			(charaProp & BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE)) > 0) {
		Logs.d("# Found writable characteristic");
		return true;
	} else {
		Logs.d("# Not writable characteristic");
		return false;
	}
}
 
Example 11
Source File: BleHeartRateSensor.java    From BLE-Heart-rate-variability-demo with MIT License 5 votes vote down vote up
@Override
public boolean onCharacteristicRead(BluetoothGattCharacteristic c) {
    super.onCharacteristicRead(c);

    Log.d(TAG, "onCharacteristicsReas");
    
    if ( !c.getUuid().toString().equals(UUID_SENSOR_BODY_LOCATION) )
        return false;

    location = c.getProperties();
    Log.d(TAG, "Sensor body location: " + location);
    return true;
}
 
Example 12
Source File: BluetoothLeService.java    From IoT-Firstep with GNU General Public License v3.0 5 votes vote down vote up
private void broadcastUpdate(final String action,
                             final BluetoothGattCharacteristic characteristic) {
    final Intent intent = new Intent(action);

    // This is special handling for the Heart Rate Measurement profile.  Data parsing is
    // carried out as per profile specifications:
    // http://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.heart_rate_measurement.xml
    if (UUID_HEART_RATE_MEASUREMENT.equals(characteristic.getUuid())) {
        int flag = characteristic.getProperties();
        int format = -1;
        if ((flag & 0x01) != 0) {
            format = BluetoothGattCharacteristic.FORMAT_UINT16;
            Log.d(TAG, "Heart rate format UINT16.");
        } else {
            format = BluetoothGattCharacteristic.FORMAT_UINT8;
            Log.d(TAG, "Heart rate format UINT8.");
        }
        final int heartRate = characteristic.getIntValue(format, 1);
        Log.d(TAG, String.format("Received heart rate: %d", heartRate));
        intent.putExtra(EXTRA_DATA, String.valueOf(heartRate));
    } else {
        // For all other profiles, writes the data formatted in HEX.
        final byte[] data = characteristic.getValue();
        //System.out.println("LeService ---->" + new String(data));
        if (data != null && data.length > 0) {
            intent.putExtra(EXTRA_DATA, data);
        }
    }
    sendBroadcast(intent);
}
 
Example 13
Source File: BleConnectWorker.java    From Android-BluetoothKit with Apache License 2.0 4 votes vote down vote up
private boolean isCharacteristicIndicatable(BluetoothGattCharacteristic characteristic) {
    return characteristic != null && (characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_INDICATE) != 0;
}
 
Example 14
Source File: BluetoothUtilImpl.java    From android-ponewheel with MIT License 4 votes vote down vote up
public static boolean isCharacteristicReadable(BluetoothGattCharacteristic c) {
    return ((c.getProperties() & BluetoothGattCharacteristic.PROPERTY_READ) != 0);
}
 
Example 15
Source File: Presenter.java    From RxAndroidBle with Apache License 2.0 4 votes vote down vote up
private static boolean hasProperty(BluetoothGattCharacteristic characteristic, int property) {
    return (characteristic.getProperties() & property) > 0;
}
 
Example 16
Source File: BluetoothPeripheral.java    From blessed-android with MIT License 4 votes vote down vote up
/**
 * Write a value to a characteristic using the specified write type.
 *
 * <p>All parameters must have a valid value in order for the operation
 * to be enqueued. If the characteristic does not support writing with the specified writeType, the operation will not be enqueued.
 *
 * <p>{@link BluetoothPeripheralCallback#onCharacteristicWrite(BluetoothPeripheral, byte[], BluetoothGattCharacteristic, int)} will be triggered as a result of this call.
 *
 * @param characteristic the characteristic to write to
 * @param value          the byte array to write
 * @param writeType      the write type to use when writing. Must be WRITE_TYPE_DEFAULT, WRITE_TYPE_NO_RESPONSE or WRITE_TYPE_SIGNED
 * @return true if a write operation was succesfully enqueued, otherwise false
 */
public boolean writeCharacteristic(final BluetoothGattCharacteristic characteristic, final byte[] value, final int writeType) {
    // Check if gatt object is valid
    if (bluetoothGatt == null) {
        Timber.e("gatt is 'null', ignoring read request");
        return false;
    }

    // Check if characteristic is valid
    if (characteristic == null) {
        Timber.e("characteristic is 'null', ignoring write request");
        return false;
    }

    // Check if byte array is valid
    if (value == null) {
        Timber.e("value to write is 'null', ignoring write request");
        return false;
    }

    // Copy the value to avoid race conditions
    final byte[] bytesToWrite = copyOf(value);

    // Check if this characteristic actually supports this writeType
    int writeProperty;
    switch (writeType) {
        case WRITE_TYPE_DEFAULT:
            writeProperty = PROPERTY_WRITE;
            break;
        case WRITE_TYPE_NO_RESPONSE:
            writeProperty = PROPERTY_WRITE_NO_RESPONSE;
            break;
        case WRITE_TYPE_SIGNED:
            writeProperty = PROPERTY_SIGNED_WRITE;
            break;
        default:
            writeProperty = 0;
            break;
    }
    if ((characteristic.getProperties() & writeProperty) == 0) {
        Timber.e("characteristic <%s> does not support writeType '%s'", characteristic.getUuid(), writeTypeToString(writeType));
        return false;
    }

    // Enqueue the write command now that all checks have been passed
    boolean result = commandQueue.add(new Runnable() {
        @Override
        public void run() {
            if (isConnected()) {
                currentWriteBytes = bytesToWrite;
                characteristic.setValue(bytesToWrite);
                characteristic.setWriteType(writeType);
                if (!bluetoothGatt.writeCharacteristic(characteristic)) {
                    Timber.e("writeCharacteristic failed for characteristic: %s", characteristic.getUuid());
                    completedCommand();
                } else {
                    Timber.d("writing <%s> to characteristic <%s>", bytes2String(bytesToWrite), characteristic.getUuid());
                    nrTries++;
                }
            } else {
                completedCommand();
            }
        }
    });

    if (result) {
        nextCommand();
    } else {
        Timber.e("could not enqueue write characteristic command");
    }
    return result;
}
 
Example 17
Source File: CharacteristicHelper.java    From bletools with MIT License 4 votes vote down vote up
public CharacteristicHelper(BluetoothGattCharacteristic characteristic) {
	this.characteristic = characteristic;
	this.propertiesFlag = characteristic.getProperties();
}
 
Example 18
Source File: BleGattImpl.java    From EasyBle with Apache License 2.0 4 votes vote down vote up
@Override
public void write(final BleDevice device, String serviceUuid, String writeUuid, byte[] data, final BleWriteCallback callback) {
    checkNotNull(callback, BleWriteCallback.class);
    if (data == null || data.length < 1 || data.length > 509) {
        callback.onFailure(BleCallback.FAIL_OTHER, data == null ? "data is null" :
                "data length must range from 1 to 509", device);
        return;
    }
    if (data.length > 20) {
        Logger.w("data length is greater than the default(20 bytes), make sure  MTU >= " + (data.length + 3));
    }
    if (!checkConnection(device, callback)) {
        return;
    }
    BluetoothGatt gatt = mGattMap.get(device.address);
    if (!checkUuid(serviceUuid, writeUuid, gatt, device, callback)) {
        return;
    }

    OperationIdentify identify = getOperationIdentifyFromMap(mWriteCallbackMap, device.address, serviceUuid, writeUuid);
    if (identify != null) {
        mWriteCallbackMap.put(identify, callback);
    } else {
        mWriteCallbackMap.put(new OperationIdentify(device.address, serviceUuid, writeUuid), callback);
    }

    BluetoothGattService service = gatt.getService(UUID.fromString(serviceUuid));
    BluetoothGattCharacteristic characteristic = service.getCharacteristic(UUID.fromString(writeUuid));
    boolean writable = (characteristic.getProperties() & (BluetoothGattCharacteristic.PROPERTY_WRITE |
            BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE)) > 0;
    if (!writable) {
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                callback.onFailure(BleCallback.FAIL_OTHER, "the characteristic is not writable", device);
            }
        });
        return;
    }
    if (!characteristic.setValue(data) || !gatt.writeCharacteristic(characteristic)) {
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                callback.onFailure(BleCallback.FAIL_OTHER, "write fail because of unknown reason", device);
            }
        });
    }
}
 
Example 19
Source File: BleConnectWorker.java    From Android-BluetoothKit with Apache License 2.0 4 votes vote down vote up
private boolean isCharacteristicNoRspWritable(BluetoothGattCharacteristic characteristic) {
    return characteristic != null && (characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE) != 0;
}
 
Example 20
Source File: BLEUtils.java    From android with MIT License 4 votes vote down vote up
/**
 * @return Returns <b>true</b> if property is supports notification
 */
public static boolean isCharacteristicNotifiable(BluetoothGattCharacteristic pChar) {
    return (pChar.getProperties() & BluetoothGattCharacteristic.PROPERTY_NOTIFY) != 0;
}