1 /* 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 */ 16 package android.bluetooth; 17 18 import android.bluetooth.BluetoothDevice; 19 20 import java.util.ArrayList; 21 import java.util.List; 22 import java.util.UUID; 23 24 /** 25 * Represents a Bluetooth GATT Service 26 * 27 * <p> Gatt Service contains a collection of {@link BluetoothGattCharacteristic}, 28 * as well as referenced services. 29 */ 30 public class BluetoothGattService { 31 32 /** 33 * Primary service 34 */ 35 public static final int SERVICE_TYPE_PRIMARY = 0; 36 37 /** 38 * Secondary service (included by primary services) 39 */ 40 public static final int SERVICE_TYPE_SECONDARY = 1; 41 42 43 /** 44 * The remote device his service is associated with. 45 * This applies to client applications only. 46 * @hide 47 */ 48 protected BluetoothDevice mDevice; 49 50 /** 51 * The UUID of this service. 52 * @hide 53 */ 54 protected UUID mUuid; 55 56 /** 57 * Instance ID for this service. 58 * @hide 59 */ 60 protected int mInstanceId; 61 62 /** 63 * Handle counter override (for conformance testing). 64 * @hide 65 */ 66 protected int mHandles = 0; 67 68 /** 69 * Service type (Primary/Secondary). 70 * @hide 71 */ 72 protected int mServiceType; 73 74 /** 75 * List of characteristics included in this service. 76 */ 77 protected List<BluetoothGattCharacteristic> mCharacteristics; 78 79 /** 80 * List of included services for this service. 81 */ 82 protected List<BluetoothGattService> mIncludedServices; 83 84 /** 85 * Create a new BluetoothGattService. 86 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission. 87 * 88 * @param uuid The UUID for this service 89 * @param serviceType The type of this service, 90 * {@link BluetoothGattService#SERVICE_TYPE_PRIMARY} or 91 * {@link BluetoothGattService#SERVICE_TYPE_SECONDARY} 92 */ 93 public BluetoothGattService(UUID uuid, int serviceType) { 94 mDevice = null; 95 mUuid = uuid; 96 mInstanceId = 0; 97 mServiceType = serviceType; 98 mCharacteristics = new ArrayList<BluetoothGattCharacteristic>(); 99 mIncludedServices = new ArrayList<BluetoothGattService>(); 100 } 101 102 /** 103 * Create a new BluetoothGattService 104 * @hide 105 */ 106 /*package*/ BluetoothGattService(BluetoothDevice device, UUID uuid, 107 int instanceId, int serviceType) { 108 mDevice = device; 109 mUuid = uuid; 110 mInstanceId = instanceId; 111 mServiceType = serviceType; 112 mCharacteristics = new ArrayList<BluetoothGattCharacteristic>(); 113 mIncludedServices = new ArrayList<BluetoothGattService>(); 114 } 115 116 /** 117 * Returns the device associated with this service. 118 * @hide 119 */ 120 /*package*/ BluetoothDevice getDevice() { 121 return mDevice; 122 } 123 124 /** 125 * Add an included service to this service. 126 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission. 127 * 128 * @param service The service to be added 129 * @return true, if the included service was added to the service 130 */ 131 public boolean addService(BluetoothGattService service) { 132 mIncludedServices.add(service); 133 return true; 134 } 135 136 /** 137 * Add a characteristic to this service. 138 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission. 139 * 140 * @param characteristic The characteristics to be added 141 * @return true, if the characteristic was added to the service 142 */ 143 public boolean addCharacteristic(BluetoothGattCharacteristic characteristic) { 144 mCharacteristics.add(characteristic); 145 characteristic.setService(this); 146 return true; 147 } 148 149 /** 150 * Get characteristic by UUID and instanceId. 151 * @hide 152 */ 153 /*package*/ BluetoothGattCharacteristic getCharacteristic(UUID uuid, int instanceId) { 154 for(BluetoothGattCharacteristic characteristic : mCharacteristics) { 155 if (uuid.equals(characteristic.getUuid()) 156 && characteristic.getInstanceId() == instanceId) 157 return characteristic; 158 } 159 return null; 160 } 161 162 /** 163 * Force the instance ID. 164 * This is needed for conformance testing only. 165 * @hide 166 */ 167 public void setInstanceId(int instanceId) { 168 mInstanceId = instanceId; 169 } 170 171 /** 172 * Get the handle count override (conformance testing. 173 * @hide 174 */ 175 /*package*/ int getHandles() { 176 return mHandles; 177 } 178 179 /** 180 * Force the number of handles to reserve for this service. 181 * This is needed for conformance testing only. 182 * @hide 183 */ 184 public void setHandles(int handles) { 185 mHandles = handles; 186 } 187 188 /** 189 * Add an included service to the internal map. 190 * @hide 191 */ 192 /*package*/ void addIncludedService(BluetoothGattService includedService) { 193 mIncludedServices.add(includedService); 194 } 195 196 /** 197 * Returns the UUID of this service 198 * 199 * @return UUID of this service 200 */ 201 public UUID getUuid() { 202 return mUuid; 203 } 204 205 /** 206 * Returns the instance ID for this service 207 * 208 * <p>If a remote device offers multiple services with the same UUID 209 * (ex. multiple battery services for different batteries), the instance 210 * ID is used to distuinguish services. 211 * 212 * @return Instance ID of this service 213 */ 214 public int getInstanceId() { 215 return mInstanceId; 216 } 217 218 /** 219 * Get the type of this service (primary/secondary) 220 */ 221 public int getType() { 222 return mServiceType; 223 } 224 225 /** 226 * Get the list of included GATT services for this service. 227 * 228 * @return List of included services or empty list if no included services 229 * were discovered. 230 */ 231 public List<BluetoothGattService> getIncludedServices() { 232 return mIncludedServices; 233 } 234 235 /** 236 * Returns a list of characteristics included in this service. 237 * 238 * @return Characteristics included in this service 239 */ 240 public List<BluetoothGattCharacteristic> getCharacteristics() { 241 return mCharacteristics; 242 } 243 244 /** 245 * Returns a characteristic with a given UUID out of the list of 246 * characteristics offered by this service. 247 * 248 * <p>This is a convenience function to allow access to a given characteristic 249 * without enumerating over the list returned by {@link #getCharacteristics} 250 * manually. 251 * 252 * <p>If a remote service offers multiple characteristics with the same 253 * UUID, the first instance of a characteristic with the given UUID 254 * is returned. 255 * 256 * @return GATT characteristic object or null if no characteristic with the 257 * given UUID was found. 258 */ 259 public BluetoothGattCharacteristic getCharacteristic(UUID uuid) { 260 for(BluetoothGattCharacteristic characteristic : mCharacteristics) { 261 if (uuid.equals(characteristic.getUuid())) 262 return characteristic; 263 } 264 return null; 265 } 266 } 267