Direct-BT  2.3.1
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  * Author: Andrei Vasiliu <andrei.vasiliu@intel.com>
7  * Copyright (c) 2016 Intel Corporation.
8  *
9  * Permission is hereby granted, free of charge, to any person obtaining
10  * a copy of this software and associated documentation files (the
11  * "Software"), to deal in the Software without restriction, including
12  * without limitation the rights to use, copy, modify, merge, publish,
13  * distribute, sublicense, and/or sell copies of the Software, and to
14  * permit persons to whom the Software is furnished to do so, subject to
15  * the following conditions:
16  *
17  * The above copyright notice and this permission notice shall be
18  * included in all copies or substantial portions of the Software.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27  */
28 
29 package org.direct_bt;
30 
31 import java.util.Iterator;
32 import java.util.List;
33 
34 /**
35  * Provides access to Bluetooth GATT characteristic.
36  *
37  * @see [Bluetooth Specification](https://www.bluetooth.com/specifications/bluetooth-core-specification/)
38  * @see [BlueZ GATT API](http://git.kernel.org/cgit/bluetooth/bluez.git/tree/doc/gatt-api.txt)
39  */
40 public interface BTGattService extends BTObject
41 {
42  /** Find a BTGattChar. If parameter UUID is not null,
43  * the returned object will have to match it.
44  * It will first check for existing objects. It will not turn on discovery
45  * or connect to devices.
46  * @parameter UUID optionally specify the UUID of the BTGattChar you are
47  * waiting for
48  * @parameter timeoutMS the function will return after timeout time in milliseconds, a
49  * value of zero means wait forever. If object is not found during this time null will be returned.
50  * @return An object matching the UUID or null if not found before
51  * timeout expires or event is canceled.
52  */
53  public BTGattChar find(String UUID, long timeoutMS);
54 
55  /** Find a BTGattChar. If parameter UUID is not null,
56  * the returned object will have to match it.
57  * It will first check for existing objects. It will not turn on discovery
58  * or connect to devices.
59  * @parameter UUID optionally specify the UUID of the BluetoothGattDescriptor you are
60  * waiting for
61  * @return An object matching the UUID or null if not found before
62  * timeout expires or event is canceled.
63  */
64  public BTGattChar find(String UUID);
65 
66  /* D-Bus property accessors: */
67 
68  /** Get the UUID of this service
69  * @return The 128 byte UUID of this service, NULL if an error occurred
70  */
71  public String getUUID();
72 
73  /** Returns the device to which this service belongs to.
74  * @return The device.
75  */
76  public BTDevice getDevice();
77 
78  /** Returns true if this service is a primary service, false if secondary.
79  * @return true if this service is a primary service, false if secondary.
80  */
81  public boolean getPrimary();
82 
83  /** Returns a list of BTGattChar this service exposes.
84  * @return A list of BTGattChar exposed by this service
85  */
86  public List<BTGattChar> getChars();
87 
88  /**
89  * Adds the given {@link BTGattCharListener} to the {@link BTDevice}
90  * and {@link BTGattChar#enableNotificationOrIndication(boolean[])} for all {@link BTGattChar} instances.
91  * @param listener {@link BTGattCharListener} to add to the {@link BTDevice}.
92  * It is important to have hte listener's {@link BTGattCharListener#getAssociatedChar() associated characteristic} == null,
93  * otherwise the listener can't be used for all characteristics.
94  * @return true if successful, otherwise false
95  * @throws IllegalArgumentException if listener's {@link BTGattCharListener#getAssociatedChar() associated characteristic}
96  * is not null.
97  * @since 2.0.0
98  * @see BTGattChar#enableNotificationOrIndication(boolean[])
99  * @see BTDevice#addCharListener(BTGattCharListener, BTGattChar)
100  */
101  public static boolean addCharListenerToAll(final BTDevice device, final List<BTGattService> services,
102  final BTGattCharListener listener) {
103  if( null == listener ) {
104  throw new IllegalArgumentException("listener argument null");
105  }
106  if( null != listener.getAssociatedChar() ) {
107  throw new IllegalArgumentException("listener's associated characteristic is not null");
108  }
109  final boolean res = device.addCharListener(listener);
110  for(final Iterator<BTGattService> is = services.iterator(); is.hasNext(); ) {
111  final BTGattService s = is.next();
112  final List<BTGattChar> characteristics = s.getChars();
113  for(final Iterator<BTGattChar> ic = characteristics.iterator(); ic.hasNext(); ) {
114  ic.next().enableNotificationOrIndication(new boolean[2]);
115  }
116  }
117  return res;
118  }
119 
120  /**
121  * Removes the given {@link BTGattCharListener} from the {@link BTDevice}.
122  * @param listener {@link BTGattCharListener} to remove from the {@link BTDevice}.
123  * @return true if successful, otherwise false
124  * @since 2.0.0
125  * @see BTGattChar#configNotificationIndication(boolean, boolean, boolean[])
126  * @see BTDevice#removeCharListener(BTGattCharListener)
127  */
128  public static boolean removeCharListenerFromAll(final BTDevice device, final List<BTGattService> services,
129  final BTGattCharListener listener) {
130  for(final Iterator<BTGattService> is = services.iterator(); is.hasNext(); ) {
131  final BTGattService s = is.next();
132  final List<BTGattChar> characteristics = s.getChars();
133  for(final Iterator<BTGattChar> ic = characteristics.iterator(); ic.hasNext(); ) {
134  ic.next().configNotificationIndication(false /* enableNotification */, false /* enableIndication */, new boolean[2]);
135  }
136  }
137  return device.removeCharListener(listener);
138  }
139 
140  /**
141  * Removes all {@link BTGattCharListener} from the {@link BTDevice}.
142  * @return count of removed {@link BTGattCharListener}
143  * @since 2.0.0
144  * @see BTGattChar#configNotificationIndication(boolean, boolean, boolean[])
145  * @see BTDevice#removeAllCharListener()
146  */
147  public static int removeAllCharListener(final BTDevice device, final List<BTGattService> services) {
148  for(final Iterator<BTGattService> is = services.iterator(); is.hasNext(); ) {
149  final BTGattService s = is.next();
150  final List<BTGattChar> characteristics = s.getChars();
151  for(final Iterator<BTGattChar> ic = characteristics.iterator(); ic.hasNext(); ) {
152  ic.next().configNotificationIndication(false /* enableNotification */, false /* enableIndication */, new boolean[2]);
153  }
154  }
155  return device.removeAllCharListener();
156  }
157 }
org.direct_bt.BTGattService.getDevice
BTDevice getDevice()
Returns the device to which this service belongs to.
org.direct_bt.BTDevice.removeAllCharListener
int removeAllCharListener()
Remove all BTGattCharListener from the list.
org.direct_bt.BTGattService.removeCharListenerFromAll
static boolean removeCharListenerFromAll(final BTDevice device, final List< BTGattService > services, final BTGattCharListener listener)
Removes the given BTGattCharListener from the BTDevice.
Definition: BTGattService.java:128
org.direct_bt.BTGattService
Provides access to Bluetooth GATT characteristic.
Definition: BTGattService.java:41
org.direct_bt.BTDevice.removeCharListener
boolean removeCharListener(final BTGattCharListener l)
Remove the given BTGattCharListener from the listener list.
org.direct_bt.BTGattService.getChars
List< BTGattChar > getChars()
Returns a list of BTGattChar this service exposes.
org.direct_bt.BTGattService.find
BTGattChar find(String UUID, long timeoutMS)
Find a BTGattChar.
org.direct_bt.BTDevice.addCharListener
boolean addCharListener(final BTGattCharListener listener)
Add the given BTGattCharListener to the listener list if not already present.
org.direct_bt.BTGattService.getPrimary
boolean getPrimary()
Returns true if this service is a primary service, false if secondary.
org.direct_bt.BTGattChar
Provides access to Bluetooth GATT characteristic.
Definition: BTGattChar.java:40
org.direct_bt.BTDevice
Provides access to Bluetooth adapters.
Definition: BTDevice.java:42
org.direct_bt.BTGattService.removeAllCharListener
static int removeAllCharListener(final BTDevice device, final List< BTGattService > services)
Removes all BTGattCharListener from the BTDevice.
Definition: BTGattService.java:147
org.direct_bt.BTGattService.addCharListenerToAll
static boolean addCharListenerToAll(final BTDevice device, final List< BTGattService > services, final BTGattCharListener listener)
Adds the given BTGattCharListener to the BTDevice and BTGattChar#enableNotificationOrIndication(boole...
Definition: BTGattService.java:101
org.direct_bt.BTGattCharListener.getAssociatedChar
final BTGattChar getAssociatedChar()
Returns the weakly associated BTGattChar to this listener instance.
Definition: BTGattCharListener.java:69
org.direct_bt.BTGattCharListener
BTGattChar event listener for notification and indication events.
Definition: BTGattCharListener.java:57
org.direct_bt.BTObject
Definition: BTObject.java:31
org.direct_bt.BTGattService.getUUID
String getUUID()
Get the UUID of this service.
org.direct_bt.BTGattService.find
BTGattChar find(String UUID)
Find a BTGattChar.