Java Code Examples for android.bluetooth.BluetoothGattDescriptor#ENABLE_NOTIFICATION_VALUE

The following examples show how to use android.bluetooth.BluetoothGattDescriptor#ENABLE_NOTIFICATION_VALUE . 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: GattServerActivity.java    From sample-bluetooth-le-gattserver with Apache License 2.0 6 votes vote down vote up
@Override
public void onDescriptorReadRequest(BluetoothDevice device, int requestId, int offset,
                                    BluetoothGattDescriptor descriptor) {
    if (TimeProfile.CLIENT_CONFIG.equals(descriptor.getUuid())) {
        Log.d(TAG, "Config descriptor read");
        byte[] returnValue;
        if (mRegisteredDevices.contains(device)) {
            returnValue = BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE;
        } else {
            returnValue = BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE;
        }
        mBluetoothGattServer.sendResponse(device,
                requestId,
                BluetoothGatt.GATT_FAILURE,
                0,
                returnValue);
    } else {
        Log.w(TAG, "Unknown descriptor read request");
        mBluetoothGattServer.sendResponse(device,
                requestId,
                BluetoothGatt.GATT_FAILURE,
                0,
                null);
    }
}
 
Example 2
Source File: P_Task_ToggleNotify.java    From AsteroidOSSync with GNU General Public License v3.0 6 votes vote down vote up
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 File: BLeSerialPortService.java    From Android-BLE-Terminal with MIT License 6 votes vote down vote up
public boolean setCharacteristicNotification(BluetoothGattCharacteristic characteristic, boolean enabled) {
    if (adapter != null || gatt != null) {
        if (gatt.setCharacteristicNotification(characteristic, enabled)) {
            BluetoothGattDescriptor descriptor = characteristic.getDescriptor(CLIENT_UUID);

            if (descriptor != null) {
                byte[] data = enabled ? BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE : BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE;
                if (descriptor.setValue(data)) {
                    gatt.writeDescriptor(descriptor);
                } else {
                    connectFailure();
                }
            } else {
                connectFailure();
            }
        } else {
            connectFailure();
        }
    }
    return true;
}
 
Example 4
Source File: P_Task_ToggleNotify.java    From SweetBlue with GNU General Public License v3.0 6 votes vote down vote up
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 5
Source File: BleWrapper.java    From BLEService with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public void setNotificationForCharacteristic(BluetoothGattCharacteristic ch, boolean enabled) {
    if (mBluetoothAdapter == null || mBluetoothGatt == null) return;
    
    boolean success = mBluetoothGatt.setCharacteristicNotification(ch, enabled);
    if(!success) {
    	Log.e("------", "Seting proper notification status for characteristic failed!");
    }
    
    // This is also sometimes required (e.g. for heart rate monitors) to enable notifications/indications
    // see: https://developer.bluetooth.org/gatt/descriptors/Pages/DescriptorViewer.aspx?u=org.bluetooth.descriptor.gatt.client_characteristic_configuration.xml
    BluetoothGattDescriptor descriptor = ch.getDescriptor(UUID.fromString("00002902-0000-1000-8000-00805f9b34fb"));
    if(descriptor != null) {
    	byte[] val = enabled ? BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE : BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE;
     descriptor.setValue(val);
     mBluetoothGatt.writeDescriptor(descriptor);
    }
}
 
Example 6
Source File: BLEUtils.java    From EFRConnect-android with 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 7
Source File: BluetoothLeService.java    From SensorTag-CC2650 with Apache License 2.0 5 votes vote down vote up
public boolean isNotificationEnabled(
    BluetoothGattCharacteristic characteristic) {
       if (characteristic == null) {
           return false;
       }
	if (!checkGatt())
		return false;

	BluetoothGattDescriptor clientConfig = characteristic
	    .getDescriptor(GattInfo.CLIENT_CHARACTERISTIC_CONFIG);
	if (clientConfig == null)
		return false;

	return clientConfig.getValue() == BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE;
}
 
Example 8
Source File: Node.java    From BlueSTSDK_Android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * 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 9
Source File: AndroidBle.java    From GizwitsBLE with 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 10
Source File: SetNotificationOperation.java    From tap-android-sdk with Apache License 2.0 4 votes vote down vote up
@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 11
Source File: CorePeripheral.java    From RxCentralBle with 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 12
Source File: SubscribeCommand.java    From neatle with 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 13
Source File: ClientComponent.java    From RxAndroidBle with Apache License 2.0 4 votes vote down vote up
@Provides
@Named(BluetoothConstants.ENABLE_NOTIFICATION_VALUE)
static byte[] provideEnableNotificationValue() {
    return BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE;
}
 
Example 14
Source File: RileyLinkBLE.java    From AndroidAPS with GNU Affero General Public License v3.0 4 votes vote down vote up
public BLECommOperationResult setNotification_blocking(UUID serviceUUID, UUID charaUUID) {
    BLECommOperationResult rval = new BLECommOperationResult();
    if (bluetoothConnectionGatt != null) {

        try {
            gattOperationSema.acquire();
            SystemClock.sleep(1); // attempting to yield thread, to make sequence of events easier to follow
        } catch (InterruptedException e) {
            LOG.error("setNotification_blocking: interrupted waiting for gattOperationSema");
            return rval;
        }
        if (mCurrentOperation != null) {
            rval.resultCode = BLECommOperationResult.RESULT_BUSY;
        } else {
            if (bluetoothConnectionGatt.getService(serviceUUID) == null) {
                // Catch if the service is not supported by the BLE device
                rval.resultCode = BLECommOperationResult.RESULT_NONE;
                LOG.error("BT Device not supported");
                // TODO: 11/07/2016 UI update for user
            } else {
                BluetoothGattCharacteristic chara = bluetoothConnectionGatt.getService(serviceUUID)
                        .getCharacteristic(charaUUID);
                // Tell Android that we want the notifications
                bluetoothConnectionGatt.setCharacteristicNotification(chara, true);
                List<BluetoothGattDescriptor> list = chara.getDescriptors();
                if (gattDebugEnabled) {
                    for (int i = 0; i < list.size(); i++) {
                        if (isLogEnabled())
                            LOG.debug("Found descriptor: " + list.get(i).toString());
                    }
                }
                BluetoothGattDescriptor descr = list.get(0);
                // Tell the remote device to send the notifications
                mCurrentOperation = new DescriptorWriteOperation(bluetoothConnectionGatt, descr,
                        BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
                mCurrentOperation.execute(this);
                if (mCurrentOperation.timedOut) {
                    rval.resultCode = BLECommOperationResult.RESULT_TIMEOUT;
                } else if (mCurrentOperation.interrupted) {
                    rval.resultCode = BLECommOperationResult.RESULT_INTERRUPTED;
                } else {
                    rval.resultCode = BLECommOperationResult.RESULT_SUCCESS;
                }
            }
            mCurrentOperation = null;
            gattOperationSema.release();
        }
    } else {
        LOG.error("setNotification_blocking: not configured!");
        rval.resultCode = BLECommOperationResult.RESULT_NOT_CONFIGURED;
    }
    return rval;
}
 
Example 15
Source File: BleConnectWorker.java    From Android-BluetoothKit with Apache License 2.0 4 votes vote down vote up
@Override
    public boolean setCharacteristicNotification(UUID service, UUID character, boolean enable) {
        checkRuntime();

        BluetoothLog.v(String.format("setCharacteristicNotification 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 (!isCharacteristicNotifyable(characteristic)) {
//            BluetoothLog.e(String.format("characteristic not notifyable!"));
//            return false;
//        }

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

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

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

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

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

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

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

        return true;
    }