28import java.lang.ref.WeakReference;
31import org.direct_bt.BTException;
32import org.direct_bt.BTGattChar;
33import org.direct_bt.BTGattDesc;
34import org.direct_bt.BTGattService;
35import org.direct_bt.GattCharPropertySet;
36import org.jau.util.BasicTypes;
37import org.direct_bt.BTGattCharListener;
44 final WeakReference<DBTGattService> wbr_service;
52 private final short handle;
57 private final String value_type_uuid;
65 private final short value_handle;
68 private final int clientCharacteristicsConfigIndex;
71 private final int userDescriptionIndex;
73 final List<BTGattDesc> descriptorList;
75 boolean enabledNotifyState =
false;
76 boolean enabledIndicateState =
false;
80 final String value_type_uuid,
final short value_handle,
81 final int clientCharacteristicsConfigIndex,
82 final int userDescriptionIndex)
84 super(nativeInstance, handle );
85 this.wbr_service =
new WeakReference<DBTGattService>(service);
88 this.properties = properties;
89 this.value_type_uuid = value_type_uuid;
90 this.value_handle = value_handle;
91 this.clientCharacteristicsConfigIndex = clientCharacteristicsConfigIndex;
92 this.userDescriptionIndex = userDescriptionIndex;
93 this.descriptorList = getDescriptorsImpl();
99 if( hasNotify || hasIndicate ) {
102 public void notificationReceived(
final BTGattChar charDecl,
final byte[] value,
final long timestamp) {
103 System.err.println(
"GATTCharacteristicListener.notificationReceived: "+charDecl+
104 ", value[len "+value.length+
": "+BasicTypes.bytesHexString(value, 0, -1,
true)+
"]");
107 public void indicationReceived(
final BTGattChar charDecl,
final byte[] value,
final long timestamp,
108 final boolean confirmationSent) {
109 System.err.println(
"GATTCharacteristicListener.indicationReceived: "+charDecl+
110 ", value[len "+value.length+
": "+BasicTypes.bytesHexString(value, 0, -1,
true)+
111 "], confirmationSent "+confirmationSent);
118 private native List<BTGattDesc> getDescriptorsImpl();
135 if (obj ==
null || !(obj instanceof
DBTGattChar)) {
139 return handle == other.handle;
143 public String
getUUID() {
return value_type_uuid; }
148 if(
null == service ) {
151 final DBTDevice device = service.wbr_device.get();
152 if(
null == device ) {
155 final int size = descriptorList.size();
156 for(
int i = 0; i < size; i++ ) {
158 if( descr.
getUUID().equals(desc_uuid) ) {
170 enabledState[0] = enabledNotifyState;
171 enabledState[1] = enabledIndicateState;
172 return enabledNotifyState || enabledIndicateState;
182 public final synchronized boolean configNotificationIndication(
final boolean enableNotification,
final boolean enableIndication,
final boolean enabledState[])
183 throws IllegalStateException
188 if( hasNotify || hasIndicate ) {
189 final boolean resEnableNotification = hasNotify && enableNotification;
190 final boolean resEnableIndication = hasIndicate && enableIndication;
192 if( resEnableNotification == enabledNotifyState &&
193 resEnableIndication == enabledIndicateState )
195 enabledState[0] = resEnableNotification;
196 enabledState[1] = resEnableIndication;
198 System.err.printf(
"GATTCharacteristic.configNotificationIndication: Unchanged: notification[shall %b, has %b: %b == %b], indication[shall %b, has %b: %b == %b]\n",
199 enableNotification, hasNotify, enabledNotifyState, resEnableNotification,
200 enableIndication, hasIndicate, enabledIndicateState, resEnableIndication);
205 final boolean res = configNotificationIndicationImpl(enableNotification, enableIndication, enabledState);
207 enabledState[0] =
false;
208 enabledState[1] =
false;
211 System.err.printf(
"GATTCharacteristic.configNotificationIndication: res %b, notification[shall %b, has %b: %b -> %b], indication[shall %b, has %b: %b -> %b]\n",
213 enableNotification, hasNotify, enabledNotifyState, enabledState[0],
214 enableIndication, hasIndicate, enabledIndicateState, enabledState[1]);
216 enabledNotifyState = enabledState[0];
217 enabledIndicateState = enabledState[1];
220 enabledState[0] =
false;
221 enabledState[1] =
false;
223 System.err.println(
"GATTCharacteristic.configNotificationIndication: FALSE*: hasNotify "+hasNotify+
", hasIndicate "+hasIndicate);
228 private native
boolean configNotificationIndicationImpl(
boolean enableNotification,
boolean enableIndication,
final boolean enabledState[])
229 throws IllegalStateException;
233 throws IllegalStateException
266 if( shallDisableIndicationNotification ) {
290 if( 0 > clientCharacteristicsConfigIndex ) {
293 return descriptorList.get(clientCharacteristicsConfigIndex);
298 if( 0 > userDescriptionIndex ) {
301 return descriptorList.get(userDescriptionIndex);
306 return readValueImpl();
308 private native
byte[] readValueImpl() throws
BTException;
312 return writeValueImpl(value, withResponse);
314 private native
boolean writeValueImpl(
byte[] argValue,
boolean withResponse)
throws BTException;
319 return "Characteristic" +
"\u271D" +
"[uuid "+
getUUID()+
", handle 0x"+Integer.toHexString(handle)+
"]";
321 return toStringImpl();
323 private native String toStringImpl();
final BTGattDesc getUserDescription()
Return the User Description BTGattDesc if available or null.
synchronized void close()
Release the native memory associated with this object The object should not be used following a call ...
BTGattDesc findGattDesc(final String desc_uuid)
Find a BTGattDesc by its desc_uuid.
String getUUID()
Get the UUID of this characteristic.
boolean disableIndicationNotification()
BT Core Spec v5.2: Vol 3, Part G GATT: 3.3.3.3 Client Characteristic Configuration.
final List< BTGattDesc > getDescriptors()
Returns a list of BluetoothGattDescriptors this characteristic exposes.
boolean equals(final Object obj)
final short getHandle()
Characteristic Handle of this instance.
final GattCharPropertySet getProperties()
Returns the properties of this characteristic.
final byte[] readValue()
Reads the value of this characteristic.
final boolean addCharListener(final BTGattCharListener listener)
Add the given BTGattCharListener to the listener list if not already present.
boolean enableNotificationOrIndication(final boolean enabledState[])
BT Core Spec v5.2: Vol 3, Part G GATT: 3.3.3.3 Client Characteristic Configuration.
final boolean writeValue(final byte[] value, final boolean withResponse)
Writes the value of this characteristic, using one of the following methods depending on withRespons...
final synchronized boolean configNotificationIndication(final boolean enableNotification, final boolean enableIndication, final boolean enabledState[])
BT Core Spec v5.2: Vol 3, Part G GATT: 3.3.3.3 Client Characteristic Configuration.
final boolean removeCharListener(final BTGattCharListener listener)
Remove the given associated BTGattCharListener from the listener list if present.
final BTGattDesc getClientCharConfig()
Return the Client Characteristic Configuration BTGattDesc if available or null.
native void deleteImpl(long nativeInstance)
Deletes the native instance.
final BTGattService getService()
Returns the service to which this characteristic belongs to.
final short getValueHandle()
Returns Characteristics Value Handle.
final boolean getNotifying(final boolean enabledState[])
Returns true if notification for changes of this characteristic are activated.
final int removeAllAssociatedCharListener(final boolean shallDisableIndicationNotification)
Disables the notification and/or indication for this characteristic BLE level if disableIndicationNot...
final boolean addCharListener(final BTGattCharListener listener, final boolean enabledState[])
Add the given BTGattCharListener to the listener list if not already present and if enabling the noti...
String getUUID()
Get the UUID of this descriptor.
static final boolean DEBUG
final boolean isNativeValid()
Returns true if native instance is valid, otherwise false.
BTGattChar event listener for notification and indication events.
Bit mask of GATT Characteristic Properties.
boolean isSet(final Type bit)
BT Core Spec v5.2: Vol 3, Part G GATT: 3.3.1.1 Characteristic Properties.
boolean removeCharListener(final BTGattCharListener l)
Remove the given BTGattCharListener from the listener list.
boolean addCharListener(final BTGattCharListener listener)
Add the given BTGattCharListener to the listener list if not already present.
int removeAllAssociatedCharListener(final BTGattChar associatedCharacteristic)
Remove all BTGattCharListener from the list, which are associated to the given BTGattChar.
Representing a Gatt Characteristic object from the GATT client perspective.
Representing a Gatt Characteristic Descriptor object from the GATT client perspective.
Representing a Gatt Service object from the GATT client perspective.
BTDevice getDevice()
Returns the device to which this service belongs to.