blob: 8e740ee387d63e2225e9ef11f8d709ff97d66869 [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
Mathew Inwood7acad5e2018-08-01 15:00:35 +010018import android.annotation.UnsupportedAppUsage;
Jakub Pawlowski8d312a82016-03-01 18:50:27 -080019import android.os.Parcel;
Jakub Pawlowski8d312a82016-03-01 18:50:27 -080020import android.os.ParcelUuid;
Jack Hea355e5e2017-08-22 16:06:54 -070021import android.os.Parcelable;
22
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -080023import java.util.ArrayList;
24import java.util.List;
25import java.util.UUID;
26
27/**
Matthew Xieddf7e472013-03-01 18:41:02 -080028 * Represents a Bluetooth GATT Service
Matthew Xie33ec9842013-04-03 00:29:27 -070029 *
30 * <p> Gatt Service contains a collection of {@link BluetoothGattCharacteristic},
31 * as well as referenced services.
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -080032 */
Jakub Pawlowski8d312a82016-03-01 18:50:27 -080033public class BluetoothGattService implements Parcelable {
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -080034
35 /**
36 * Primary service
37 */
38 public static final int SERVICE_TYPE_PRIMARY = 0;
39
40 /**
41 * Secondary service (included by primary services)
42 */
43 public static final int SERVICE_TYPE_SECONDARY = 1;
44
45
46 /**
47 * The remote device his service is associated with.
48 * This applies to client applications only.
Jack Hea355e5e2017-08-22 16:06:54 -070049 *
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -080050 * @hide
51 */
Mathew Inwood7acad5e2018-08-01 15:00:35 +010052 @UnsupportedAppUsage
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -080053 protected BluetoothDevice mDevice;
54
55 /**
56 * The UUID of this service.
Jack Hea355e5e2017-08-22 16:06:54 -070057 *
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -080058 * @hide
59 */
60 protected UUID mUuid;
61
62 /**
63 * Instance ID for this service.
Jack Hea355e5e2017-08-22 16:06:54 -070064 *
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -080065 * @hide
66 */
67 protected int mInstanceId;
68
69 /**
70 * Handle counter override (for conformance testing).
Jack Hea355e5e2017-08-22 16:06:54 -070071 *
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -080072 * @hide
73 */
74 protected int mHandles = 0;
75
76 /**
77 * Service type (Primary/Secondary).
Jack Hea355e5e2017-08-22 16:06:54 -070078 *
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -080079 * @hide
80 */
81 protected int mServiceType;
82
83 /**
84 * List of characteristics included in this service.
85 */
86 protected List<BluetoothGattCharacteristic> mCharacteristics;
87
88 /**
89 * List of included services for this service.
90 */
91 protected List<BluetoothGattService> mIncludedServices;
92
93 /**
Wei Wang18c76932013-10-29 21:05:37 -070094 * Whether the service uuid should be advertised.
95 */
96 private boolean mAdvertisePreferred;
97
98 /**
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -080099 * Create a new BluetoothGattService.
Matthew Xieddf7e472013-03-01 18:41:02 -0800100 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission.
101 *
102 * @param uuid The UUID for this service
103 * @param serviceType The type of this service,
Jack Hea355e5e2017-08-22 16:06:54 -0700104 * {@link BluetoothGattService#SERVICE_TYPE_PRIMARY}
105 * or {@link BluetoothGattService#SERVICE_TYPE_SECONDARY}
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800106 */
Matthew Xieddf7e472013-03-01 18:41:02 -0800107 public BluetoothGattService(UUID uuid, int serviceType) {
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800108 mDevice = null;
109 mUuid = uuid;
110 mInstanceId = 0;
111 mServiceType = serviceType;
112 mCharacteristics = new ArrayList<BluetoothGattCharacteristic>();
113 mIncludedServices = new ArrayList<BluetoothGattService>();
114 }
115
116 /**
117 * Create a new BluetoothGattService
Jack Hea355e5e2017-08-22 16:06:54 -0700118 *
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800119 * @hide
120 */
121 /*package*/ BluetoothGattService(BluetoothDevice device, UUID uuid,
Jack Hea355e5e2017-08-22 16:06:54 -0700122 int instanceId, int serviceType) {
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800123 mDevice = device;
124 mUuid = uuid;
125 mInstanceId = instanceId;
126 mServiceType = serviceType;
127 mCharacteristics = new ArrayList<BluetoothGattCharacteristic>();
128 mIncludedServices = new ArrayList<BluetoothGattService>();
129 }
130
131 /**
Jakub Pawlowski8d312a82016-03-01 18:50:27 -0800132 * Create a new BluetoothGattService
Jack Hea355e5e2017-08-22 16:06:54 -0700133 *
Jakub Pawlowski8d312a82016-03-01 18:50:27 -0800134 * @hide
135 */
136 public BluetoothGattService(UUID uuid, int instanceId, int serviceType) {
137 mDevice = null;
138 mUuid = uuid;
139 mInstanceId = instanceId;
140 mServiceType = serviceType;
141 mCharacteristics = new ArrayList<BluetoothGattCharacteristic>();
142 mIncludedServices = new ArrayList<BluetoothGattService>();
143 }
144
145 /**
146 * @hide
147 */
148 public int describeContents() {
149 return 0;
150 }
151
152 @Override
153 public void writeToParcel(Parcel out, int flags) {
154 out.writeParcelable(new ParcelUuid(mUuid), 0);
155 out.writeInt(mInstanceId);
156 out.writeInt(mServiceType);
157 out.writeTypedList(mCharacteristics);
158
159 ArrayList<BluetoothGattIncludedService> includedServices =
160 new ArrayList<BluetoothGattIncludedService>(mIncludedServices.size());
Jack Hea355e5e2017-08-22 16:06:54 -0700161 for (BluetoothGattService s : mIncludedServices) {
Jakub Pawlowski8d312a82016-03-01 18:50:27 -0800162 includedServices.add(new BluetoothGattIncludedService(s.getUuid(),
Jack Hea355e5e2017-08-22 16:06:54 -0700163 s.getInstanceId(), s.getType()));
Jakub Pawlowski8d312a82016-03-01 18:50:27 -0800164 }
165 out.writeTypedList(includedServices);
Jack Hea355e5e2017-08-22 16:06:54 -0700166 }
Jakub Pawlowski8d312a82016-03-01 18:50:27 -0800167
Jack He2992cd02017-08-22 21:21:23 -0700168 public static final Parcelable.Creator<BluetoothGattService> CREATOR =
169 new Parcelable.Creator<BluetoothGattService>() {
Jakub Pawlowski8d312a82016-03-01 18:50:27 -0800170 public BluetoothGattService createFromParcel(Parcel in) {
171 return new BluetoothGattService(in);
172 }
173
174 public BluetoothGattService[] newArray(int size) {
175 return new BluetoothGattService[size];
176 }
177 };
178
179 private BluetoothGattService(Parcel in) {
Jack Hea355e5e2017-08-22 16:06:54 -0700180 mUuid = ((ParcelUuid) in.readParcelable(null)).getUuid();
Jakub Pawlowski8d312a82016-03-01 18:50:27 -0800181 mInstanceId = in.readInt();
182 mServiceType = in.readInt();
183
184 mCharacteristics = new ArrayList<BluetoothGattCharacteristic>();
185
186 ArrayList<BluetoothGattCharacteristic> chrcs =
187 in.createTypedArrayList(BluetoothGattCharacteristic.CREATOR);
188 if (chrcs != null) {
189 for (BluetoothGattCharacteristic chrc : chrcs) {
190 chrc.setService(this);
191 mCharacteristics.add(chrc);
192 }
193 }
194
195 mIncludedServices = new ArrayList<BluetoothGattService>();
196
197 ArrayList<BluetoothGattIncludedService> inclSvcs =
198 in.createTypedArrayList(BluetoothGattIncludedService.CREATOR);
199 if (chrcs != null) {
200 for (BluetoothGattIncludedService isvc : inclSvcs) {
201 mIncludedServices.add(new BluetoothGattService(null, isvc.getUuid(),
Jack Hea355e5e2017-08-22 16:06:54 -0700202 isvc.getInstanceId(), isvc.getType()));
Jakub Pawlowski8d312a82016-03-01 18:50:27 -0800203 }
204 }
205 }
206
207 /**
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800208 * Returns the device associated with this service.
Jack Hea355e5e2017-08-22 16:06:54 -0700209 *
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800210 * @hide
211 */
212 /*package*/ BluetoothDevice getDevice() {
213 return mDevice;
214 }
215
216 /**
Jakub Pawlowski8d312a82016-03-01 18:50:27 -0800217 * Returns the device associated with this service.
Jack Hea355e5e2017-08-22 16:06:54 -0700218 *
Jakub Pawlowski8d312a82016-03-01 18:50:27 -0800219 * @hide
220 */
221 /*package*/ void setDevice(BluetoothDevice device) {
Jack He2992cd02017-08-22 21:21:23 -0700222 mDevice = device;
Jakub Pawlowski8d312a82016-03-01 18:50:27 -0800223 }
224
225 /**
Matthew Xieddf7e472013-03-01 18:41:02 -0800226 * Add an included service to this service.
227 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission.
228 *
229 * @param service The service to be added
230 * @return true, if the included service was added to the service
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800231 */
Matthew Xieddf7e472013-03-01 18:41:02 -0800232 public boolean addService(BluetoothGattService service) {
233 mIncludedServices.add(service);
234 return true;
235 }
236
237 /**
238 * Add a characteristic to this service.
239 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission.
240 *
241 * @param characteristic The characteristics to be added
242 * @return true, if the characteristic was added to the service
243 */
244 public boolean addCharacteristic(BluetoothGattCharacteristic characteristic) {
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800245 mCharacteristics.add(characteristic);
Matthew Xieddf7e472013-03-01 18:41:02 -0800246 characteristic.setService(this);
247 return true;
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800248 }
249
250 /**
251 * Get characteristic by UUID and instanceId.
Jack Hea355e5e2017-08-22 16:06:54 -0700252 *
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800253 * @hide
254 */
255 /*package*/ BluetoothGattCharacteristic getCharacteristic(UUID uuid, int instanceId) {
Jack Hea355e5e2017-08-22 16:06:54 -0700256 for (BluetoothGattCharacteristic characteristic : mCharacteristics) {
Andre Eisenbach25b9cf92013-07-08 23:58:16 -0700257 if (uuid.equals(characteristic.getUuid())
Jack Hea355e5e2017-08-22 16:06:54 -0700258 && characteristic.getInstanceId() == instanceId) {
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800259 return characteristic;
Jack Hea355e5e2017-08-22 16:06:54 -0700260 }
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800261 }
262 return null;
263 }
264
265 /**
Matthew Xieddf7e472013-03-01 18:41:02 -0800266 * Force the instance ID.
Jack Hea355e5e2017-08-22 16:06:54 -0700267 *
Matthew Xieddf7e472013-03-01 18:41:02 -0800268 * @hide
269 */
Mathew Inwood7acad5e2018-08-01 15:00:35 +0100270 @UnsupportedAppUsage
Matthew Xieddf7e472013-03-01 18:41:02 -0800271 public void setInstanceId(int instanceId) {
272 mInstanceId = instanceId;
273 }
274
275 /**
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800276 * Get the handle count override (conformance testing.
Jack Hea355e5e2017-08-22 16:06:54 -0700277 *
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800278 * @hide
279 */
280 /*package*/ int getHandles() {
281 return mHandles;
282 }
283
284 /**
Matthew Xieddf7e472013-03-01 18:41:02 -0800285 * Force the number of handles to reserve for this service.
286 * This is needed for conformance testing only.
Jack Hea355e5e2017-08-22 16:06:54 -0700287 *
Matthew Xieddf7e472013-03-01 18:41:02 -0800288 * @hide
289 */
290 public void setHandles(int handles) {
291 mHandles = handles;
292 }
293
294 /**
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800295 * Add an included service to the internal map.
Jack Hea355e5e2017-08-22 16:06:54 -0700296 *
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800297 * @hide
298 */
Jakub Pawlowski8d312a82016-03-01 18:50:27 -0800299 public void addIncludedService(BluetoothGattService includedService) {
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800300 mIncludedServices.add(includedService);
301 }
302
303 /**
304 * Returns the UUID of this service
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800305 *
306 * @return UUID of this service
307 */
308 public UUID getUuid() {
309 return mUuid;
310 }
311
312 /**
313 * Returns the instance ID for this service
314 *
315 * <p>If a remote device offers multiple services with the same UUID
316 * (ex. multiple battery services for different batteries), the instance
317 * ID is used to distuinguish services.
318 *
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800319 * @return Instance ID of this service
320 */
321 public int getInstanceId() {
322 return mInstanceId;
323 }
324
325 /**
326 * Get the type of this service (primary/secondary)
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800327 */
328 public int getType() {
329 return mServiceType;
330 }
331
332 /**
Matthew Xieddf7e472013-03-01 18:41:02 -0800333 * Get the list of included GATT services for this service.
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800334 *
Jack Hea355e5e2017-08-22 16:06:54 -0700335 * @return List of included services or empty list if no included services were discovered.
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800336 */
337 public List<BluetoothGattService> getIncludedServices() {
338 return mIncludedServices;
339 }
340
341 /**
342 * Returns a list of characteristics included in this service.
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800343 *
344 * @return Characteristics included in this service
345 */
346 public List<BluetoothGattCharacteristic> getCharacteristics() {
347 return mCharacteristics;
348 }
349
350 /**
351 * Returns a characteristic with a given UUID out of the list of
352 * characteristics offered by this service.
353 *
354 * <p>This is a convenience function to allow access to a given characteristic
355 * without enumerating over the list returned by {@link #getCharacteristics}
356 * manually.
357 *
358 * <p>If a remote service offers multiple characteristics with the same
359 * UUID, the first instance of a characteristic with the given UUID
360 * is returned.
361 *
Jack Hea355e5e2017-08-22 16:06:54 -0700362 * @return GATT characteristic object or null if no characteristic with the given UUID was
363 * found.
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800364 */
365 public BluetoothGattCharacteristic getCharacteristic(UUID uuid) {
Jack Hea355e5e2017-08-22 16:06:54 -0700366 for (BluetoothGattCharacteristic characteristic : mCharacteristics) {
367 if (uuid.equals(characteristic.getUuid())) {
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800368 return characteristic;
Jack Hea355e5e2017-08-22 16:06:54 -0700369 }
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800370 }
371 return null;
372 }
Wei Wang18c76932013-10-29 21:05:37 -0700373
374 /**
375 * Returns whether the uuid of the service should be advertised.
Jack Hea355e5e2017-08-22 16:06:54 -0700376 *
Wei Wang18c76932013-10-29 21:05:37 -0700377 * @hide
378 */
379 public boolean isAdvertisePreferred() {
Jack Hea355e5e2017-08-22 16:06:54 -0700380 return mAdvertisePreferred;
Wei Wang18c76932013-10-29 21:05:37 -0700381 }
382
383 /**
384 * Set whether the service uuid should be advertised.
Jack Hea355e5e2017-08-22 16:06:54 -0700385 *
Wei Wang18c76932013-10-29 21:05:37 -0700386 * @hide
387 */
Mathew Inwood7acad5e2018-08-01 15:00:35 +0100388 @UnsupportedAppUsage
Wei Wang18c76932013-10-29 21:05:37 -0700389 public void setAdvertisePreferred(boolean advertisePreferred) {
Jack He2992cd02017-08-22 21:21:23 -0700390 mAdvertisePreferred = advertisePreferred;
Wei Wang18c76932013-10-29 21:05:37 -0700391 }
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800392}