Direct-BT v3.3.0-1-gc2d430c
Direct-BT - Direct Bluetooth Programming.
DBTGattService.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;
29import java.util.List;
30
31import org.direct_bt.BTDevice;
32import org.direct_bt.BTGattChar;
33import org.direct_bt.BTGattService;
34
35public class DBTGattService extends DBTObject implements BTGattService
36{
37 /** Service's device weak back-reference */
38 final WeakReference<DBTDevice> wbr_device;
39
40 private final boolean isPrimary;
41 private final String type_uuid;
42 private final short handleStart;
43 private final short handleEnd;
44 /* pp */ final List<BTGattChar> charList;
45
46 /* pp */ DBTGattService(final long nativeInstance, final DBTDevice device, final boolean isPrimary,
47 final String type_uuid, final short handleStart, final short handleEnd)
48 {
49 super(nativeInstance, compHash(handleStart, handleEnd));
50 this.wbr_device = new WeakReference<DBTDevice>(device);
51 this.isPrimary = isPrimary;
52 this.type_uuid = type_uuid;
53 this.handleStart = handleStart;
54 this.handleEnd = handleEnd;
55 this.charList = getCharsImpl();
56 }
57
58 @Override
59 public boolean equals(final Object obj)
60 {
61 if (obj == null || !(obj instanceof DBTGattService)) {
62 return false;
63 }
64 final DBTGattService other = (DBTGattService)obj;
65 return handleStart == other.handleStart && handleEnd == other.handleEnd; /** unique attribute handles */
66 }
67
68 @Override
69 public String getUUID() { return type_uuid; }
70
71 @Override
72 public final BTGattService clone()
73 { throw new UnsupportedOperationException(); } // FIXME
74
75 @Override
76 public BTGattChar findGattChar(final String char_uuid) {
77 final DBTDevice device = wbr_device.get();
78 if( null == device ) {
79 return null;
80 }
81 final int characteristicSize = charList.size();
82 for(int charIdx = 0; charIdx < characteristicSize; charIdx++ ) {
83 final DBTGattChar characteristic = (DBTGattChar) charList.get(charIdx);
84 if( characteristic.getUUID().equals(char_uuid) ) {
85 return characteristic;
86 }
87 }
88 return null;
89 }
90
91 @Override
92 public final BTDevice getDevice() { return wbr_device.get(); }
93
94 @Override
95 public final boolean getPrimary() { return isPrimary; }
96
97 @Override
98 public final List<BTGattChar> getChars() { return charList; }
99
100 /**
101 * Returns the service start handle.
102 * <p>
103 * Attribute handles are unique for each device (server) (BT Core Spec v5.2: Vol 3, Part F Protocol..: 3.2.2 Attribute Handle).
104 * </p>
105 */
106 public final short getHandleStart() { return handleStart; }
107
108 /**
109 * Returns the service end handle.
110 * <p>
111 * Attribute handles are unique for each device (server) (BT Core Spec v5.2: Vol 3, Part F Protocol..: 3.2.2 Attribute Handle).
112 * </p>
113 */
114 public final short getHandleEnd() { return handleEnd; }
115
116 @Override
117 public final String toString() {
118 if( !isNativeValid() ) {
119 return "Service" + "\u271D" + "[uuid "+getUUID()+", handles [0x"+Integer.toHexString(handleStart)+".."+Integer.toHexString(handleEnd)+"]]";
120 }
121 return toStringImpl();
122 }
123
124 /* Native method calls: */
125
126 private native String toStringImpl();
127
128 private native List<BTGattChar> getCharsImpl();
129
130 @Override
131 protected native void deleteImpl(long nativeInstance);
132}
String getUUID()
Get the UUID of this characteristic.
String getUUID()
Get the UUID of this service.
BTGattChar findGattChar(final String char_uuid)
Find a BTGattChar by its char_uuid.
final short getHandleEnd()
Returns the service end handle.
final short getHandleStart()
Returns the service start handle.
final List< BTGattChar > getChars()
Returns a list of BTGattChar this service exposes.
final BTDevice getDevice()
Returns the device to which this service belongs to.
final boolean getPrimary()
Returns true if this service is a primary service, false if secondary.
boolean equals(final Object obj)
native void deleteImpl(long nativeInstance)
Deletes the native instance.
final BTGattService clone()
BTDevice represents one remote Bluetooth device.
Definition: BTDevice.java:47
Representing a Gatt Characteristic object from the GATT client perspective.
Definition: BTGattChar.java:49
Representing a Gatt Service object from the GATT client perspective.