Direct-BT v3.3.0-1-gc2d430c
Direct-BT - Direct Bluetooth Programming.
BTGattService.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 org.direct_bt;
27
28import java.util.Iterator;
29import java.util.List;
30
31/**
32 * Representing a Gatt Service object from the GATT client perspective.
33 *
34 * A list of shared BTGattService instances can be retrieved from BTDevice
35 * after successful connection and optional pairing via BTDevice::getGattServices().
36 *
37 * See [Direct-BT Overview](namespaceorg_1_1direct__bt.html#details).
38 *
39 * BT Core Spec v5.2: Vol 3, Part G GATT: 3.1 Service Definition
40 *
41 * Includes a complete [Primary] Service Declaration
42 * including its list of Characteristic Declarations,
43 * which also may include its client config if available.
44 *
45 * See {@link DBGattService.UUID16} for selected standard GATT service numbers in UUID16 format
46 * and {@link BTUtils#toUUID128(String)} for their conversion to UUID128.
47 */
48public interface BTGattService extends BTObject
49{
50 /**
51 * Find a {@link BTGattChar} by its char_uuid.
52 *
53 * @parameter char_uuid the UUID of the desired {@link BTGattChar}
54 * @return The matching characteristic or null if not found
55 */
56 BTGattChar findGattChar(String char_uuid);
57
58 /** Get the UUID of this service
59 * @return The 128 byte UUID of this service, NULL if an error occurred
60 */
61 String getUUID();
62
63 /** Returns the device to which this service belongs to.
64 * @return The device.
65 */
67
68 /** Returns true if this service is a primary service, false if secondary.
69 * @return true if this service is a primary service, false if secondary.
70 */
71 boolean getPrimary();
72
73 /** Returns a list of BTGattChar this service exposes.
74 * @return A list of BTGattChar exposed by this service
75 */
76 List<BTGattChar> getChars();
77
78 /**
79 * Adds the given {@link BTGattCharListener} to the {@link BTDevice}
80 * and {@link BTGattChar#enableNotificationOrIndication(boolean[])} for all {@link BTGattChar} instances.
81 * @param listener {@link BTGattCharListener} to add to the {@link BTDevice}.
82 * It is important to have hte listener's {@link BTGattCharListener#getAssociatedChar() associated characteristic} == null,
83 * otherwise the listener can't be used for all characteristics.
84 * @return true if successful, otherwise false
85 * @throws IllegalArgumentException if listener's {@link BTGattCharListener#getAssociatedChar() associated characteristic}
86 * is not null.
87 * @since 2.0.0
88 * @see BTGattChar#enableNotificationOrIndication(boolean[])
89 * @see BTDevice#addCharListener(BTGattCharListener, BTGattChar)
90 */
91 public static boolean addCharListenerToAll(final BTDevice device, final List<BTGattService> services,
92 final BTGattCharListener listener) {
93 if( null == listener ) {
94 throw new IllegalArgumentException("listener argument null");
95 }
96 final boolean res = device.addCharListener(listener);
97 for(final Iterator<BTGattService> is = services.iterator(); is.hasNext(); ) {
98 final BTGattService s = is.next();
99 final List<BTGattChar> characteristics = s.getChars();
100 for(final Iterator<BTGattChar> ic = characteristics.iterator(); ic.hasNext(); ) {
101 ic.next().enableNotificationOrIndication(new boolean[2]);
102 }
103 }
104 return res;
105 }
106
107 /**
108 * Removes the given {@link BTGattCharListener} from the {@link BTDevice}.
109 * @param listener {@link BTGattCharListener} to remove from the {@link BTDevice}.
110 * @return true if successful, otherwise false
111 * @since 2.0.0
112 * @see BTGattChar#configNotificationIndication(boolean, boolean, boolean[])
113 * @see BTDevice#removeCharListener(BTGattCharListener)
114 */
115 public static boolean removeCharListenerFromAll(final BTDevice device, final List<BTGattService> services,
116 final BTGattCharListener listener) {
117 for(final Iterator<BTGattService> is = services.iterator(); is.hasNext(); ) {
118 final BTGattService s = is.next();
119 final List<BTGattChar> characteristics = s.getChars();
120 for(final Iterator<BTGattChar> ic = characteristics.iterator(); ic.hasNext(); ) {
121 ic.next().configNotificationIndication(false /* enableNotification */, false /* enableIndication */, new boolean[2]);
122 }
123 }
124 return device.removeCharListener(listener);
125 }
126
127 /**
128 * Removes all {@link BTGattCharListener} from the {@link BTDevice}.
129 * @return count of removed {@link BTGattCharListener}
130 * @since 2.0.0
131 * @see BTGattChar#configNotificationIndication(boolean, boolean, boolean[])
132 * @see BTDevice#removeAllCharListener()
133 */
134 public static int removeAllCharListener(final BTDevice device, final List<BTGattService> services) {
135 for(final Iterator<BTGattService> is = services.iterator(); is.hasNext(); ) {
136 final BTGattService s = is.next();
137 final List<BTGattChar> characteristics = s.getChars();
138 for(final Iterator<BTGattChar> ic = characteristics.iterator(); ic.hasNext(); ) {
139 ic.next().configNotificationIndication(false /* enableNotification */, false /* enableIndication */, new boolean[2]);
140 }
141 }
142 return device.removeAllCharListener();
143 }
144
145 @Override
146 String toString();
147}
BTGattChar event listener for notification and indication events.
BTDevice represents one remote Bluetooth device.
Definition: BTDevice.java:47
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 removeAllCharListener()
Remove all BTGattCharListener from the list.
Representing a Gatt Characteristic object from the GATT client perspective.
Definition: BTGattChar.java:49
Representing a Gatt Service object from the GATT client perspective.
static boolean addCharListenerToAll(final BTDevice device, final List< BTGattService > services, final BTGattCharListener listener)
Adds the given BTGattCharListener to the BTDevice and BTGattChar#enableNotificationOrIndication(boole...
BTGattChar findGattChar(String char_uuid)
Find a BTGattChar by its char_uuid.
String getUUID()
Get the UUID of this service.
List< BTGattChar > getChars()
Returns a list of BTGattChar this service exposes.
boolean getPrimary()
Returns true if this service is a primary service, false if secondary.
static int removeAllCharListener(final BTDevice device, final List< BTGattService > services)
Removes all BTGattCharListener from the BTDevice.
static boolean removeCharListenerFromAll(final BTDevice device, final List< BTGattService > services, final BTGattCharListener listener)
Removes the given BTGattCharListener from the BTDevice.
BTDevice getDevice()
Returns the device to which this service belongs to.