blob: ce1dc1ce631135caab96964a29a35f4ce351ac09 [file] [log] [blame]
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -08001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package android.bluetooth;
17
Jakub Pawlowski8d312a82016-03-01 18:50:27 -080018import android.os.Parcel;
Jakub Pawlowski8d312a82016-03-01 18:50:27 -080019import android.os.ParcelUuid;
Jack Hea355e5e2017-08-22 16:06:54 -070020import android.os.Parcelable;
21
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -080022import java.util.ArrayList;
23import java.util.List;
24import java.util.UUID;
25
26/**
Matthew Xieddf7e472013-03-01 18:41:02 -080027 * Represents a Bluetooth GATT Service
Matthew Xie33ec9842013-04-03 00:29:27 -070028 *
29 * <p> Gatt Service contains a collection of {@link BluetoothGattCharacteristic},
30 * as well as referenced services.
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -080031 */
Jakub Pawlowski8d312a82016-03-01 18:50:27 -080032public class BluetoothGattService implements Parcelable {
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -080033
34 /**
35 * Primary service
36 */
37 public static final int SERVICE_TYPE_PRIMARY = 0;
38
39 /**
40 * Secondary service (included by primary services)
41 */
42 public static final int SERVICE_TYPE_SECONDARY = 1;
43
44
45 /**
46 * The remote device his service is associated with.
47 * This applies to client applications only.
Jack Hea355e5e2017-08-22 16:06:54 -070048 *
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -080049 * @hide
50 */
51 protected BluetoothDevice mDevice;
52
53 /**
54 * The UUID of this service.
Jack Hea355e5e2017-08-22 16:06:54 -070055 *
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -080056 * @hide
57 */
58 protected UUID mUuid;
59
60 /**
61 * Instance ID for this service.
Jack Hea355e5e2017-08-22 16:06:54 -070062 *
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -080063 * @hide
64 */
65 protected int mInstanceId;
66
67 /**
68 * Handle counter override (for conformance testing).
Jack Hea355e5e2017-08-22 16:06:54 -070069 *
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -080070 * @hide
71 */
72 protected int mHandles = 0;
73
74 /**
75 * Service type (Primary/Secondary).
Jack Hea355e5e2017-08-22 16:06:54 -070076 *
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -080077 * @hide
78 */
79 protected int mServiceType;
80
81 /**
82 * List of characteristics included in this service.
83 */
84 protected List<BluetoothGattCharacteristic> mCharacteristics;
85
86 /**
87 * List of included services for this service.
88 */
89 protected List<BluetoothGattService> mIncludedServices;
90
91 /**
Wei Wang18c76932013-10-29 21:05:37 -070092 * Whether the service uuid should be advertised.
93 */
94 private boolean mAdvertisePreferred;
95
96 /**
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -080097 * Create a new BluetoothGattService.
Matthew Xieddf7e472013-03-01 18:41:02 -080098 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission.
99 *
100 * @param uuid The UUID for this service
101 * @param serviceType The type of this service,
Jack Hea355e5e2017-08-22 16:06:54 -0700102 * {@link BluetoothGattService#SERVICE_TYPE_PRIMARY}
103 * or {@link BluetoothGattService#SERVICE_TYPE_SECONDARY}
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800104 */
Matthew Xieddf7e472013-03-01 18:41:02 -0800105 public BluetoothGattService(UUID uuid, int serviceType) {
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800106 mDevice = null;
107 mUuid = uuid;
108 mInstanceId = 0;
109 mServiceType = serviceType;
110 mCharacteristics = new ArrayList<BluetoothGattCharacteristic>();
111 mIncludedServices = new ArrayList<BluetoothGattService>();
112 }
113
114 /**
115 * Create a new BluetoothGattService
Jack Hea355e5e2017-08-22 16:06:54 -0700116 *
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800117 * @hide
118 */
119 /*package*/ BluetoothGattService(BluetoothDevice device, UUID uuid,
Jack Hea355e5e2017-08-22 16:06:54 -0700120 int instanceId, int serviceType) {
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800121 mDevice = device;
122 mUuid = uuid;
123 mInstanceId = instanceId;
124 mServiceType = serviceType;
125 mCharacteristics = new ArrayList<BluetoothGattCharacteristic>();
126 mIncludedServices = new ArrayList<BluetoothGattService>();
127 }
128
129 /**
Jakub Pawlowski8d312a82016-03-01 18:50:27 -0800130 * Create a new BluetoothGattService
Jack Hea355e5e2017-08-22 16:06:54 -0700131 *
Jakub Pawlowski8d312a82016-03-01 18:50:27 -0800132 * @hide
133 */
134 public BluetoothGattService(UUID uuid, int instanceId, int serviceType) {
135 mDevice = null;
136 mUuid = uuid;
137 mInstanceId = instanceId;
138 mServiceType = serviceType;
139 mCharacteristics = new ArrayList<BluetoothGattCharacteristic>();
140 mIncludedServices = new ArrayList<BluetoothGattService>();
141 }
142
143 /**
144 * @hide
145 */
146 public int describeContents() {
147 return 0;
148 }
149
150 @Override
151 public void writeToParcel(Parcel out, int flags) {
152 out.writeParcelable(new ParcelUuid(mUuid), 0);
153 out.writeInt(mInstanceId);
154 out.writeInt(mServiceType);
155 out.writeTypedList(mCharacteristics);
156
157 ArrayList<BluetoothGattIncludedService> includedServices =
158 new ArrayList<BluetoothGattIncludedService>(mIncludedServices.size());
Jack Hea355e5e2017-08-22 16:06:54 -0700159 for (BluetoothGattService s : mIncludedServices) {
Jakub Pawlowski8d312a82016-03-01 18:50:27 -0800160 includedServices.add(new BluetoothGattIncludedService(s.getUuid(),
Jack Hea355e5e2017-08-22 16:06:54 -0700161 s.getInstanceId(), s.getType()));
Jakub Pawlowski8d312a82016-03-01 18:50:27 -0800162 }
163 out.writeTypedList(includedServices);
Jack Hea355e5e2017-08-22 16:06:54 -0700164 }
Jakub Pawlowski8d312a82016-03-01 18:50:27 -0800165
Jack He2992cd02017-08-22 21:21:23 -0700166 public static final Parcelable.Creator<BluetoothGattService> CREATOR =
167 new Parcelable.Creator<BluetoothGattService>() {
Jakub Pawlowski8d312a82016-03-01 18:50:27 -0800168 public BluetoothGattService createFromParcel(Parcel in) {
169 return new BluetoothGattService(in);
170 }
171
172 public BluetoothGattService[] newArray(int size) {
173 return new BluetoothGattService[size];
174 }
175 };
176
177 private BluetoothGattService(Parcel in) {
Jack Hea355e5e2017-08-22 16:06:54 -0700178 mUuid = ((ParcelUuid) in.readParcelable(null)).getUuid();
Jakub Pawlowski8d312a82016-03-01 18:50:27 -0800179 mInstanceId = in.readInt();
180 mServiceType = in.readInt();
181
182 mCharacteristics = new ArrayList<BluetoothGattCharacteristic>();
183
184 ArrayList<BluetoothGattCharacteristic> chrcs =
185 in.createTypedArrayList(BluetoothGattCharacteristic.CREATOR);
186 if (chrcs != null) {
187 for (BluetoothGattCharacteristic chrc : chrcs) {
188 chrc.setService(this);
189 mCharacteristics.add(chrc);
190 }
191 }
192
193 mIncludedServices = new ArrayList<BluetoothGattService>();
194
195 ArrayList<BluetoothGattIncludedService> inclSvcs =
196 in.createTypedArrayList(BluetoothGattIncludedService.CREATOR);
197 if (chrcs != null) {
198 for (BluetoothGattIncludedService isvc : inclSvcs) {
199 mIncludedServices.add(new BluetoothGattService(null, isvc.getUuid(),
Jack Hea355e5e2017-08-22 16:06:54 -0700200 isvc.getInstanceId(), isvc.getType()));
Jakub Pawlowski8d312a82016-03-01 18:50:27 -0800201 }
202 }
203 }
204
205 /**
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800206 * Returns the device associated with this service.
Jack Hea355e5e2017-08-22 16:06:54 -0700207 *
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800208 * @hide
209 */
210 /*package*/ BluetoothDevice getDevice() {
211 return mDevice;
212 }
213
214 /**
Jakub Pawlowski8d312a82016-03-01 18:50:27 -0800215 * Returns the device associated with this service.
Jack Hea355e5e2017-08-22 16:06:54 -0700216 *
Jakub Pawlowski8d312a82016-03-01 18:50:27 -0800217 * @hide
218 */
219 /*package*/ void setDevice(BluetoothDevice device) {
Jack He2992cd02017-08-22 21:21:23 -0700220 mDevice = device;
Jakub Pawlowski8d312a82016-03-01 18:50:27 -0800221 }
222
223 /**
Matthew Xieddf7e472013-03-01 18:41:02 -0800224 * Add an included service to this service.
225 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission.
226 *
227 * @param service The service to be added
228 * @return true, if the included service was added to the service
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800229 */
Matthew Xieddf7e472013-03-01 18:41:02 -0800230 public boolean addService(BluetoothGattService service) {
231 mIncludedServices.add(service);
232 return true;
233 }
234
235 /**
236 * Add a characteristic to this service.
237 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission.
238 *
239 * @param characteristic The characteristics to be added
240 * @return true, if the characteristic was added to the service
241 */
242 public boolean addCharacteristic(BluetoothGattCharacteristic characteristic) {
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800243 mCharacteristics.add(characteristic);
Matthew Xieddf7e472013-03-01 18:41:02 -0800244 characteristic.setService(this);
245 return true;
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800246 }
247
248 /**
249 * Get characteristic by UUID and instanceId.
Jack Hea355e5e2017-08-22 16:06:54 -0700250 *
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800251 * @hide
252 */
253 /*package*/ BluetoothGattCharacteristic getCharacteristic(UUID uuid, int instanceId) {
Jack Hea355e5e2017-08-22 16:06:54 -0700254 for (BluetoothGattCharacteristic characteristic : mCharacteristics) {
Andre Eisenbach25b9cf92013-07-08 23:58:16 -0700255 if (uuid.equals(characteristic.getUuid())
Jack Hea355e5e2017-08-22 16:06:54 -0700256 && characteristic.getInstanceId() == instanceId) {
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800257 return characteristic;
Jack Hea355e5e2017-08-22 16:06:54 -0700258 }
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800259 }
260 return null;
261 }
262
263 /**
Matthew Xieddf7e472013-03-01 18:41:02 -0800264 * Force the instance ID.
Jack Hea355e5e2017-08-22 16:06:54 -0700265 *
Matthew Xieddf7e472013-03-01 18:41:02 -0800266 * @hide
267 */
268 public void setInstanceId(int instanceId) {
269 mInstanceId = instanceId;
270 }
271
272 /**
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800273 * Get the handle count override (conformance testing.
Jack Hea355e5e2017-08-22 16:06:54 -0700274 *
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800275 * @hide
276 */
277 /*package*/ int getHandles() {
278 return mHandles;
279 }
280
281 /**
Matthew Xieddf7e472013-03-01 18:41:02 -0800282 * Force the number of handles to reserve for this service.
283 * This is needed for conformance testing only.
Jack Hea355e5e2017-08-22 16:06:54 -0700284 *
Matthew Xieddf7e472013-03-01 18:41:02 -0800285 * @hide
286 */
287 public void setHandles(int handles) {
288 mHandles = handles;
289 }
290
291 /**
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800292 * Add an included service to the internal map.
Jack Hea355e5e2017-08-22 16:06:54 -0700293 *
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800294 * @hide
295 */
Jakub Pawlowski8d312a82016-03-01 18:50:27 -0800296 public void addIncludedService(BluetoothGattService includedService) {
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800297 mIncludedServices.add(includedService);
298 }
299
300 /**
301 * Returns the UUID of this service
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800302 *
303 * @return UUID of this service
304 */
305 public UUID getUuid() {
306 return mUuid;
307 }
308
309 /**
310 * Returns the instance ID for this service
311 *
312 * <p>If a remote device offers multiple services with the same UUID
313 * (ex. multiple battery services for different batteries), the instance
314 * ID is used to distuinguish services.
315 *
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800316 * @return Instance ID of this service
317 */
318 public int getInstanceId() {
319 return mInstanceId;
320 }
321
322 /**
323 * Get the type of this service (primary/secondary)
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800324 */
325 public int getType() {
326 return mServiceType;
327 }
328
329 /**
Matthew Xieddf7e472013-03-01 18:41:02 -0800330 * Get the list of included GATT services for this service.
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800331 *
Jack Hea355e5e2017-08-22 16:06:54 -0700332 * @return List of included services or empty list if no included services were discovered.
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800333 */
334 public List<BluetoothGattService> getIncludedServices() {
335 return mIncludedServices;
336 }
337
338 /**
339 * Returns a list of characteristics included in this service.
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800340 *
341 * @return Characteristics included in this service
342 */
343 public List<BluetoothGattCharacteristic> getCharacteristics() {
344 return mCharacteristics;
345 }
346
347 /**
348 * Returns a characteristic with a given UUID out of the list of
349 * characteristics offered by this service.
350 *
351 * <p>This is a convenience function to allow access to a given characteristic
352 * without enumerating over the list returned by {@link #getCharacteristics}
353 * manually.
354 *
355 * <p>If a remote service offers multiple characteristics with the same
356 * UUID, the first instance of a characteristic with the given UUID
357 * is returned.
358 *
Jack Hea355e5e2017-08-22 16:06:54 -0700359 * @return GATT characteristic object or null if no characteristic with the given UUID was
360 * found.
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800361 */
362 public BluetoothGattCharacteristic getCharacteristic(UUID uuid) {
Jack Hea355e5e2017-08-22 16:06:54 -0700363 for (BluetoothGattCharacteristic characteristic : mCharacteristics) {
364 if (uuid.equals(characteristic.getUuid())) {
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800365 return characteristic;
Jack Hea355e5e2017-08-22 16:06:54 -0700366 }
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800367 }
368 return null;
369 }
Wei Wang18c76932013-10-29 21:05:37 -0700370
371 /**
372 * Returns whether the uuid of the service should be advertised.
Jack Hea355e5e2017-08-22 16:06:54 -0700373 *
Wei Wang18c76932013-10-29 21:05:37 -0700374 * @hide
375 */
376 public boolean isAdvertisePreferred() {
Jack Hea355e5e2017-08-22 16:06:54 -0700377 return mAdvertisePreferred;
Wei Wang18c76932013-10-29 21:05:37 -0700378 }
379
380 /**
381 * Set whether the service uuid should be advertised.
Jack Hea355e5e2017-08-22 16:06:54 -0700382 *
Wei Wang18c76932013-10-29 21:05:37 -0700383 * @hide
384 */
385 public void setAdvertisePreferred(boolean advertisePreferred) {
Jack He2992cd02017-08-22 21:21:23 -0700386 mAdvertisePreferred = advertisePreferred;
Wei Wang18c76932013-10-29 21:05:37 -0700387 }
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800388}