Direct-BT v3.3.0-1-gc2d430c
Direct-BT - Direct Bluetooth Programming.
DBTGattDesc.java
Go to the documentation of this file.
1/**
2 * Author: Sven Gothel <sgothel@jausoft.com>
3 * Copyright (c) 2020 Gothel Software e.K.
4 * Copyright (c) 2020 ZAFENA AB
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sublicense, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26package jau.direct_bt;
27
28import java.lang.ref.WeakReference;
29
30import org.direct_bt.BTException;
31import org.direct_bt.BTGattDesc;
32
33public class DBTGattDesc extends DBTObject implements BTGattDesc
34{
35 /** Descriptor's characteristic weak back-reference */
36 final WeakReference<DBTGattChar> wbr_characteristic;
37
38 /** Type of Descriptor */
39 private final String type_uuid;
40
41 /**
42 * Characteristic Descriptor Handle
43 * <p>
44 * Attribute handles are unique for each device (server) (BT Core Spec v5.2: Vol 3, Part F Protocol..: 3.2.2 Attribute Handle).
45 * </p>
46 */
47 private final short handle;
48
49 private byte[] cachedValue;
50
51 private void updateCachedValue(final byte[] value) {
52 if( null == cachedValue || cachedValue.length != value.length ) {
53 cachedValue = new byte[value.length];
54 }
55 System.arraycopy(value, 0, cachedValue, 0, value.length);
56 }
57
58 /* pp */ DBTGattDesc(final long nativeInstance, final DBTGattChar characteristic,
59 final String type_uuid, final short handle, final byte[] value)
60 {
61 super(nativeInstance, handle /* hash */);
62 this.wbr_characteristic = new WeakReference<DBTGattChar>(characteristic);
63 this.type_uuid = type_uuid;
64 this.handle = handle;
65 this.cachedValue = value;
66 }
67
68 @Override
69 public synchronized void close() {
70 if( !isNativeValid() ) {
71 return;
72 }
73 super.close();
74 }
75
76 @Override
77 public boolean equals(final Object obj)
78 {
79 if (obj == null || !(obj instanceof DBTGattDesc)) {
80 return false;
81 }
82 final DBTGattDesc other = (DBTGattDesc)obj;
83 return handle == other.handle; /** unique attribute handles */
84 }
85
86 @Override
87 public String getUUID() { return type_uuid; }
88
89 @Override
90 public final DBTGattChar getCharacteristic() { return wbr_characteristic.get(); }
91
92 @Override
93 public final byte[] getValue() { return cachedValue; }
94
95 @Override
96 public final byte[] readValue() {
97 final byte[] value = readValueImpl();
98 updateCachedValue(value);
99 return cachedValue;
100 }
101
102 @Override
103 public final boolean writeValue(final byte[] value) throws BTException {
104 final boolean res = writeValueImpl(value);
105 if( res ) {
106 updateCachedValue(value);
107 }
108 return res;
109 }
110
111 /**
112 * Characteristic Descriptor Handle
113 * <p>
114 * Attribute handles are unique for each device (server) (BT Core Spec v5.2: Vol 3, Part F Protocol..: 3.2.2 Attribute Handle).
115 * </p>
116 */
117 public final short getHandle() { return handle; }
118
119 @Override
120 public final String toString() {
121 if( !isNativeValid() ) {
122 return "Descriptor" + "\u271D" + "[uuid "+getUUID()+", handle 0x"+Integer.toHexString(handle)+"]";
123 }
124 return toStringImpl();
125 }
126
127 /* Native method calls: */
128
129 private native String toStringImpl();
130
131 private native byte[] readValueImpl();
132
133 private native boolean writeValueImpl(byte[] argValue) throws BTException;
134
135 @Override
136 protected native void deleteImpl(long nativeInstance);
137}
boolean equals(final Object obj)
final DBTGattChar getCharacteristic()
Returns the characteristic to which this descriptor belongs to.
synchronized void close()
Release the native memory associated with this object The object should not be used following a call ...
final byte[] readValue()
Reads the value of this descriptor.
final boolean writeValue(final byte[] value)
Writes the value of this descriptor.
final byte[] getValue()
Returns the cached value of this descriptor, if any.
final short getHandle()
Characteristic Descriptor Handle.
String getUUID()
Get the UUID of this descriptor.
native void deleteImpl(long nativeInstance)
Deletes the native instance.
Representing a Gatt Characteristic Descriptor object from the GATT client perspective.
Definition: BTGattDesc.java:42