Direct-BT v3.3.0-1-gc2d430c
Direct-BT - Direct Bluetooth Programming.
DBGattService.java
Go to the documentation of this file.
1/**
2 * Author: Sven Gothel <sgothel@jausoft.com>
3 * Copyright (c) 2021 Gothel Software e.K.
4 * Copyright (c) 2021 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 org.direct_bt;
27
28import java.util.List;
29
30/**
31 * Representing a Gatt Service object from the ::GATTRole::Server perspective.
32 *
33 * A list of shared DBGattService instances are passed at DBGattServer construction
34 * and are retrievable via DBGattServer::getServices().
35 *
36 * See [Direct-BT Overview](namespaceorg_1_1direct__bt.html#details).
37 *
38 * BT Core Spec v5.2: Vol 3, Part G GATT: 3.1 Service Definition
39 *
40 * Includes a complete [Primary] Service Declaration
41 * including its list of Characteristic Declarations,
42 * which also may include its client config if available.
43 *
44 * @since 2.4.0
45 */
46public final class DBGattService implements AutoCloseable
47{
48 private volatile long nativeInstance;
49 /* pp */ long getNativeInstance() { return nativeInstance; }
50
51 /**
52 * Selected standard GATT service service numbers in UUID16 format as defined.
53 */
54 public static class UUID16 {
55 /** This service contains generic information about the device. This is a mandatory service. */
56 public static String GENERIC_ACCESS = "1800";
57 /** The service allows receiving indications of changed services. This is a mandatory service. */
58 public static String GENERIC_ATTRIBUTE = "1801";
59 /** This service exposes a control point to change the peripheral alert behavior. */
60 public static String IMMEDIATE_ALERT = "1802";
61 /** The service defines behavior on the device when a link is lost between two devices. */
62 public static String LINK_LOSS = "1803";
63 /** This service exposes temperature and other data from a thermometer intended for healthcare and fitness applications. */
64 public static String HEALTH_THERMOMETER = "1809";
65 /** This service exposes manufacturer and/or vendor information about a device. */
66 public static String DEVICE_INFORMATION = "180a";
67 /** This service exposes the state of a battery within a device. */
68 public static String BATTERY_SERVICE = "180f";
69 };
70 /**
71 * Indicate whether this service is a primary service.
72 */
73 public final boolean primary;
74
75 /**
76 * Service start handle
77 * <p>
78 * Attribute handles are unique for each device (server) (BT Core Spec v5.2: Vol 3, Part F Protocol..: 3.2.2 Attribute Handle).
79 * </p>
80 */
81 public native short getHandle();
82
83 /**
84 * Service end handle, inclusive.
85 * <p>
86 * Attribute handles are unique for each device (server) (BT Core Spec v5.2: Vol 3, Part F Protocol..: 3.2.2 Attribute Handle).
87 * </p>
88 */
89 public native short getEndHandle();
90
91 private final String type;
92
93 /** Service type UUID (lower-case) */
94 public String getType() { return type; }
95
96 /* pp */ final List<DBGattChar> characteristics;
97
98 /** List of Characteristic Declarations. */
99 public final List<DBGattChar> getCharacteristics() { return characteristics; }
100
101 public DBGattService(final boolean primary_,
102 final String type_,
103 final List<DBGattChar> characteristics_)
104 {
105 primary = primary_;
106 type = type_;
107 characteristics = characteristics_;
108
109 final long[] nativeCharacteristics = new long[characteristics_.size()];
110 for(int i=0; i < nativeCharacteristics.length; i++) {
111 nativeCharacteristics[i] = characteristics_.get(i).getNativeInstance();
112 }
113 nativeInstance = ctorImpl(primary_, type_, nativeCharacteristics);
114 }
115 private native long ctorImpl(final boolean primary, final String type,
116 final long[] characteristics);
117
118 @Override
119 public void close() {
120 final long handle;
121 synchronized( this ) {
122 handle = nativeInstance;
123 nativeInstance = 0;
124 }
125 if( 0 != handle ) {
126 dtorImpl(handle);
127 }
128 }
129 private static native void dtorImpl(final long nativeInstance);
130
131 @Override
132 public void finalize() {
133 close();
134 }
135
136 public DBGattChar findGattChar(final String char_uuid) {
137 for(final DBGattChar c : characteristics) {
138 if( char_uuid.equals( c.getValueType() ) ) {
139 return c;
140 }
141 }
142 return null;
143 }
144 public DBGattChar findGattCharByValueHandle(final short char_value_handle) {
145 for(final DBGattChar c : characteristics) {
146 if( char_value_handle == c.getValueHandle() ) {
147 return c;
148 }
149 }
150 return null;
151 }
152
153 @Override
154 public boolean equals(final Object other) {
155 if( this == other ) {
156 return true;
157 }
158 if( !(other instanceof DBGattService) ) {
159 return false;
160 }
161 final DBGattService o = (DBGattService)other;
162 return getHandle() == o.getHandle() && getEndHandle() == o.getEndHandle(); /** unique attribute handles */
163 }
164
165 @Override
166 public native String toString();
167}
Representing a Gatt Characteristic object from the GATT server perspective.
Definition: DBGattChar.java:46
Selected standard GATT service service numbers in UUID16 format as defined.
static String GENERIC_ACCESS
This service contains generic information about the device.
static String BATTERY_SERVICE
This service exposes the state of a battery within a device.
static String DEVICE_INFORMATION
This service exposes manufacturer and/or vendor information about a device.
static String LINK_LOSS
The service defines behavior on the device when a link is lost between two devices.
static String IMMEDIATE_ALERT
This service exposes a control point to change the peripheral alert behavior.
static String HEALTH_THERMOMETER
This service exposes temperature and other data from a thermometer intended for healthcare and fitnes...
static String GENERIC_ATTRIBUTE
The service allows receiving indications of changed services.
Representing a Gatt Service object from the ::GATTRole::Server perspective.
String getType()
Service type UUID (lower-case)
DBGattChar findGattChar(final String char_uuid)
final boolean primary
Indicate whether this service is a primary service.
boolean equals(final Object other)
native short getEndHandle()
Service end handle, inclusive.
native short getHandle()
Service start handle.
native String toString()
DBGattChar findGattCharByValueHandle(final short char_value_handle)
DBGattService(final boolean primary_, final String type_, final List< DBGattChar > characteristics_)
final List< DBGattChar > getCharacteristics()
List of Characteristic Declarations.