Java Code Examples for android.bluetooth.BluetoothGattDescriptor#ENABLE_INDICATION_VALUE

The following examples show how to use android.bluetooth.BluetoothGattDescriptor#ENABLE_INDICATION_VALUE . 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
static byte[] getWriteValue(BluetoothGattCharacteristic char_native, boolean enable)
{
	final int type;
	
	if( (char_native.getProperties() & BluetoothGattCharacteristic.PROPERTY_NOTIFY) != 0x0 )
	{
		type = Type_NOTIFY;
	}
	else if( (char_native.getProperties() & BluetoothGattCharacteristic.PROPERTY_INDICATE) != 0x0 )
	{
		type = Type_INDICATE;
	}
	else
	{
		type = Type_NOTIFY;
	}
	
	final byte[] enableValue = type == Type_NOTIFY ? BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE : BluetoothGattDescriptor.ENABLE_INDICATION_VALUE;
	final byte[] disableValue = BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE;

	return enable ? enableValue : disableValue;
}
 
Example 2
static byte[] getWriteValue(BluetoothGattCharacteristic char_native, boolean enable)
{
	final int type;
	
	if( (char_native.getProperties() & BluetoothGattCharacteristic.PROPERTY_NOTIFY) != 0x0 )
	{
		type = Type_NOTIFY;
	}
	else if( (char_native.getProperties() & BluetoothGattCharacteristic.PROPERTY_INDICATE) != 0x0 )
	{
		type = Type_INDICATE;
	}
	else
	{
		type = Type_NOTIFY;
	}
	
	final byte[] enableValue = type == Type_NOTIFY ? BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE : BluetoothGattDescriptor.ENABLE_INDICATION_VALUE;
	final byte[] disableValue = BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE;

	return enable ? enableValue : disableValue;
}
 
Example 3
Source Project: EFRConnect-android   File: BLEUtils.java    License: Apache License 2.0 5 votes vote down vote up
Notifications(Types type, boolean enabled) {
    this.enabled = enabled;
    this.type = type;
    this.btValue = this.enabled ?
            this.type == Types.NOTIFICATIONS ? BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE : BluetoothGattDescriptor.ENABLE_INDICATION_VALUE :
            BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE;
}
 
Example 4
/**
 * send a request for enable/disable the notification update on a specific characteristics
 * @param characteristic characteristics to notify
 * @param enable true if you want enable the notification, false if you want disable it
 * @return true if the request is correctly send, false otherwise
 */
boolean changeNotificationStatus(BluetoothGattCharacteristic characteristic,
                                      boolean enable){

    if(charCanBeNotify(characteristic) && mConnection!=null && isConnected()){
        BluetoothGattDescriptor descriptor = characteristic.getDescriptor(NOTIFY_CHAR_DESC_UUID);

        if(descriptor==null)
            return false;

        WriteDescCommand command = null;
        final  int properties = characteristic.getProperties();
        if((properties & BluetoothGattCharacteristic.PROPERTY_NOTIFY) != 0) {
            command = new WriteDescCommand(descriptor,
                    enable ? BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE :
                            BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE);

        }else if((properties & BluetoothGattCharacteristic.PROPERTY_INDICATE) != 0) {
            command = new WriteDescCommand(descriptor,
                    enable ? BluetoothGattDescriptor.ENABLE_INDICATION_VALUE :
                            BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE);
        }
        if(command==null)
            return false;

        enqueueWriteDesc(command);

        return true;
    }else
        return false;
}
 
Example 5
Source Project: GizwitsBLE   File: AndroidBle.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public boolean characteristicNotification(String address,
		BleGattCharacteristic characteristic) {
	BleRequest request = mService.getCurrentRequest();
	BluetoothGatt gatt = mBluetoothGatts.get(address);
	if (gatt == null || characteristic == null) {
		return false;
	}

	boolean enable = true;
	if (request.type == RequestType.CHARACTERISTIC_STOP_NOTIFICATION) {
		enable = false;
	}
	BluetoothGattCharacteristic c = characteristic.getGattCharacteristicA();
	if (!gatt.setCharacteristicNotification(c, enable)) {
		return false;
	}

	BluetoothGattDescriptor descriptor = c
			.getDescriptor(BleService.DESC_CCC);
	if (descriptor == null) {
		return false;
	}

	byte[] val_set = null;
	if (request.type == RequestType.CHARACTERISTIC_NOTIFICATION) {
		val_set = BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE;
	} else if (request.type == RequestType.CHARACTERISTIC_INDICATION) {
		val_set = BluetoothGattDescriptor.ENABLE_INDICATION_VALUE;
	} else {
		val_set = BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE;
	}
	if (!descriptor.setValue(val_set)) {
		return false;
	}

	return gatt.writeDescriptor(descriptor);
}
 
Example 6
@Override
public void execute(@NonNull BluetoothGatt gatt) {

    BluetoothGattCharacteristic c = extractCharacteristic(gatt);
    if (c == null) {
        postOnError(ErrorStrings.NO_CHARACTERISTIC + ": " + this.characteristic.toString());
        return;
    }

    BluetoothGattDescriptor d = c.getDescriptor(descriptor);
    if (d == null) {
        postOnError(ErrorStrings.NO_DESCRIPTOR);
        return;
    }

    int characteristicProperties = c.getProperties();
    byte[] notificationValue = BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE;
    if ((characteristicProperties & 0x10) == 16) {
        notificationValue = BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE;
    } else if ((characteristicProperties & 0x20) == 32) {
        notificationValue = BluetoothGattDescriptor.ENABLE_INDICATION_VALUE;
    }

    if (notificationValue.length == 0) {
        postOnError(ErrorStrings.NOTIFY_TYPE_FAIL);
        return;
    }

    if (!d.setValue(notificationValue)) {
        postOnError(ErrorStrings.VALUE_STORE_FAIL);
        return;
    }

    if (!gatt.setCharacteristicNotification(c, true)) {
        postOnError(ErrorStrings.NOTIFY_OP_INIT_FAIL);
        return;
    }

    if (!gatt.writeDescriptor(d)) {
        postOnError(ErrorStrings.WRITE_OP_INIT_FAIL);
    }
}
 
Example 7
Source Project: RxCentralBle   File: CorePeripheral.java    License: Apache License 2.0 4 votes vote down vote up
@Nullable
private PeripheralError setCharacteristicNotification(
    BluetoothGatt bluetoothGatt, BluetoothGattCharacteristic characteristic, boolean enable) {

  BluetoothGattDescriptor cccd = characteristic.getDescriptor(CCCD_UUID);
  if (cccd == null) {
    return new PeripheralError(PeripheralError.Code.SET_CHARACTERISTIC_NOTIFICATION_CCCD_MISSING);
  }

  if (!bluetoothGatt.setCharacteristicNotification(characteristic, enable)) {
    return new PeripheralError(PeripheralError.Code.SET_CHARACTERISTIC_NOTIFICATION_FAILED);
  }

  int properties = characteristic.getProperties();
  byte[] value;

  if (enable) {
    if ((properties & BluetoothGattCharacteristic.PROPERTY_NOTIFY)
        == BluetoothGattCharacteristic.PROPERTY_NOTIFY) {
      value = BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE;
    } else if ((properties & BluetoothGattCharacteristic.PROPERTY_INDICATE)
        == BluetoothGattCharacteristic.PROPERTY_INDICATE) {
      value = BluetoothGattDescriptor.ENABLE_INDICATION_VALUE;
    } else {
      return new PeripheralError(PeripheralError.Code.SET_CHARACTERISTIC_NOTIFICATION_MISSING_PROPERTY);
    }
  } else {
    value = BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE;
  }

  characteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT);

  if (!cccd.setValue(value)) {
    return new PeripheralError(CHARACTERISTIC_SET_VALUE_FAILED);
  }

  if (!bluetoothGatt.writeDescriptor(cccd)) {
    return new PeripheralError(PeripheralError.Code.WRITE_DESCRIPTOR_FAILED, ERROR_STATUS_CALL_FAILED);
  }

  return null;
}
 
Example 8
Source Project: neatle   File: SubscribeCommand.java    License: MIT License 4 votes vote down vote up
@Override
protected void start(Connection connection, BluetoothGatt gatt) {
    if (type == Type.UNSUBSCRIBE && connection.getCharacteristicsChangedListenerCount(characteristicUUID) > 0) {
        NeatleLogger.d("Won't unsubscribe on " + characteristicUUID + " since it has registered listeners");
        finish(CommandResult.createEmptySuccess(characteristicUUID));
        return;
    }

    BluetoothGattService service = gatt.getService(serviceUUID);
    if (service == null) {
        finish(CommandResult.createErrorResult(characteristicUUID, SERVICE_NOT_FOUND));
        return;
    }

    BluetoothGattCharacteristic characteristic = service.getCharacteristic(characteristicUUID);
    if (characteristic == null) {
        finish(CommandResult.createErrorResult(characteristicUUID, CHARACTERISTIC_NOT_FOUND));
        return;
    }

    BluetoothGattDescriptor descriptor = characteristic.getDescriptor(CLIENT_CHARACTERISTIC_CONFIG);
    if (descriptor == null) {
        finish(CommandResult.createErrorResult(characteristicUUID, DESCRIPTOR_NOT_FOUND));
        return;
    }

    boolean turnOn;
    byte[] valueToWrite;
    switch (type) {
        case Type.SUBSCRIBE_INDICATION:
            NeatleLogger.d("Subscribing to indications on  " + characteristicUUID);
            valueToWrite = BluetoothGattDescriptor.ENABLE_INDICATION_VALUE;
            turnOn = true;
            break;
        case Type.SUBSCRIBE_NOTIFICATION:
            NeatleLogger.d("Subscribing to notifications on  " + characteristicUUID);
            valueToWrite = BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE;
            turnOn = true;
            break;
        case Type.UNSUBSCRIBE:
            NeatleLogger.d("Unsubscribing from notifications/indications on  " + characteristicUUID);
            valueToWrite = BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE;
            turnOn = false;
            break;
        default:
            throw new IllegalStateException();
    }

    byte[] descriptorValue = descriptor.getValue();
    if (Arrays.equals(descriptorValue, valueToWrite)) {
        NeatleLogger.d("No subscription changes needed - is at  " + valueToWrite[0] + " on " + characteristicUUID);
        finish(CommandResult.createEmptySuccess(characteristicUUID));
        return;
    }

    if (!gatt.setCharacteristicNotification(characteristic, turnOn)) {
        NeatleLogger.e("Failed to change characteristics notification flag on " + characteristicUUID);
        finish(CommandResult.createErrorResult(characteristicUUID, BluetoothGatt.GATT_FAILURE));
        return;
    }

    descriptor.setValue(valueToWrite);
    NeatleLogger.d("Writing descriptor on " + characteristicUUID);
    if (!gatt.writeDescriptor(descriptor)) {
        NeatleLogger.e("Failed to write descriptor on " + characteristicUUID);
        finish(CommandResult.createErrorResult(characteristicUUID, BluetoothGatt.GATT_FAILURE));
    }
}
 
Example 9
Source Project: RxAndroidBle   File: ClientComponent.java    License: Apache License 2.0 4 votes vote down vote up
@Provides
@Named(BluetoothConstants.ENABLE_INDICATION_VALUE)
static byte[] provideEnableIndicationValue() {
    return BluetoothGattDescriptor.ENABLE_INDICATION_VALUE;
}
 
Example 10
@Override
    public boolean setCharacteristicIndication(UUID service, UUID character, boolean enable) {
        checkRuntime();

        BluetoothLog.v(String.format("setCharacteristicIndication for %s, service = %s, character = %s, enable = %b",
                getAddress(), service, character, enable));

        BluetoothGattCharacteristic characteristic = getCharacter(service, character);

        if (characteristic == null) {
            BluetoothLog.e(String.format("characteristic not exist!"));
            return false;
        }

//        if (!isCharacteristicIndicatable(characteristic)) {
//            BluetoothLog.e(String.format("characteristic not indicatable!"));
//            return false;
//        }

        if (mBluetoothGatt == null) {
            BluetoothLog.e(String.format("ble gatt null"));
            return false;
        }

        if (!mBluetoothGatt.setCharacteristicNotification(characteristic, enable)) {
            BluetoothLog.e(String.format("setCharacteristicIndication failed"));
            return false;
        }

        BluetoothGattDescriptor descriptor = characteristic.getDescriptor(Constants.CLIENT_CHARACTERISTIC_CONFIG);

        if (descriptor == null) {
            BluetoothLog.e(String.format("getDescriptor for indicate null!"));
            return false;
        }

        byte[] value = (enable ? BluetoothGattDescriptor.ENABLE_INDICATION_VALUE : BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE);

        if (!descriptor.setValue(value)) {
            BluetoothLog.e(String.format("setValue for indicate descriptor failed!"));
            return false;
        }

        if (!mBluetoothGatt.writeDescriptor(descriptor)) {
            BluetoothLog.e(String.format("writeDescriptor for indicate failed"));
            return false;
        }

        return true;
    }