BluetoothGattService.java revision bf0faed3597b92d950febafcf555ad05529559d6
19908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta/*
29908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta * Copyright (C) 2013 The Android Open Source Project
39908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta *
49908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta * Licensed under the Apache License, Version 2.0 (the "License");
59908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta * you may not use this file except in compliance with the License.
69908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta * You may obtain a copy of the License at
79908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta *
89908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta *      http://www.apache.org/licenses/LICENSE-2.0
99908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta *
109908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta * Unless required by applicable law or agreed to in writing, software
119908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta * distributed under the License is distributed on an "AS IS" BASIS,
129908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
139908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta * See the License for the specific language governing permissions and
149908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta * limitations under the License.
159908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta */
169908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Battapackage android.bluetooth;
179908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta
18bf0faed3597b92d950febafcf555ad05529559d6Jakub Pawlowskiimport android.os.Parcel;
19bf0faed3597b92d950febafcf555ad05529559d6Jakub Pawlowskiimport android.os.Parcelable;
20bf0faed3597b92d950febafcf555ad05529559d6Jakub Pawlowskiimport android.os.ParcelUuid;
219908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Battaimport java.util.ArrayList;
229908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Battaimport java.util.List;
239908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Battaimport java.util.UUID;
249908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta
259908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta/**
26ddf7e4756c31d0ed90802f98abeaa79df6d16b2aMatthew Xie * Represents a Bluetooth GATT Service
2733ec9840c70ddc7cd008ecf2660c441defc5f302Matthew Xie *
2833ec9840c70ddc7cd008ecf2660c441defc5f302Matthew Xie * <p> Gatt Service contains a collection of {@link BluetoothGattCharacteristic},
2933ec9840c70ddc7cd008ecf2660c441defc5f302Matthew Xie * as well as referenced services.
309908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta */
31bf0faed3597b92d950febafcf555ad05529559d6Jakub Pawlowskipublic class BluetoothGattService implements Parcelable {
329908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta
339908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta    /**
349908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta     * Primary service
359908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta     */
369908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta    public static final int SERVICE_TYPE_PRIMARY = 0;
379908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta
389908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta    /**
399908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta     * Secondary service (included by primary services)
409908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta     */
419908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta    public static final int SERVICE_TYPE_SECONDARY = 1;
429908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta
439908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta
449908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta    /**
459908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta     * The remote device his service is associated with.
469908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta     * This applies to client applications only.
479908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta     * @hide
489908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta     */
499908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta    protected BluetoothDevice mDevice;
509908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta
519908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta    /**
529908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta     * The UUID of this service.
539908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta     * @hide
549908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta     */
559908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta    protected UUID mUuid;
569908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta
579908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta    /**
589908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta     * Instance ID for this service.
599908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta     * @hide
609908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta     */
619908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta    protected int mInstanceId;
629908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta
639908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta    /**
649908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta     * Handle counter override (for conformance testing).
659908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta     * @hide
669908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta     */
679908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta    protected int mHandles = 0;
689908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta
699908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta    /**
709908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta     * Service type (Primary/Secondary).
719908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta     * @hide
729908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta     */
739908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta    protected int mServiceType;
749908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta
759908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta    /**
769908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta     * List of characteristics included in this service.
779908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta     */
789908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta    protected List<BluetoothGattCharacteristic> mCharacteristics;
799908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta
809908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta    /**
819908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta     * List of included services for this service.
829908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta     */
839908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta    protected List<BluetoothGattService> mIncludedServices;
849908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta
859908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta    /**
8618c76934f62410d1499e2eb63635b5dd8da5013aWei Wang     * Whether the service uuid should be advertised.
8718c76934f62410d1499e2eb63635b5dd8da5013aWei Wang     */
8818c76934f62410d1499e2eb63635b5dd8da5013aWei Wang    private boolean mAdvertisePreferred;
8918c76934f62410d1499e2eb63635b5dd8da5013aWei Wang
9018c76934f62410d1499e2eb63635b5dd8da5013aWei Wang    /**
919908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta     * Create a new BluetoothGattService.
92ddf7e4756c31d0ed90802f98abeaa79df6d16b2aMatthew Xie     * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission.
93ddf7e4756c31d0ed90802f98abeaa79df6d16b2aMatthew Xie     *
94ddf7e4756c31d0ed90802f98abeaa79df6d16b2aMatthew Xie     * @param uuid The UUID for this service
95ddf7e4756c31d0ed90802f98abeaa79df6d16b2aMatthew Xie     * @param serviceType The type of this service,
96ddf7e4756c31d0ed90802f98abeaa79df6d16b2aMatthew Xie     *        {@link BluetoothGattService#SERVICE_TYPE_PRIMARY} or
97ddf7e4756c31d0ed90802f98abeaa79df6d16b2aMatthew Xie     *        {@link BluetoothGattService#SERVICE_TYPE_SECONDARY}
989908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta     */
99ddf7e4756c31d0ed90802f98abeaa79df6d16b2aMatthew Xie    public BluetoothGattService(UUID uuid, int serviceType) {
1009908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta        mDevice = null;
1019908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta        mUuid = uuid;
1029908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta        mInstanceId = 0;
1039908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta        mServiceType = serviceType;
1049908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta        mCharacteristics = new ArrayList<BluetoothGattCharacteristic>();
1059908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta        mIncludedServices = new ArrayList<BluetoothGattService>();
1069908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta    }
1079908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta
1089908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta    /**
1099908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta     * Create a new BluetoothGattService
1109908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta     * @hide
1119908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta     */
1129908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta    /*package*/ BluetoothGattService(BluetoothDevice device, UUID uuid,
1139908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta                                     int instanceId, int serviceType) {
1149908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta        mDevice = device;
1159908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta        mUuid = uuid;
1169908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta        mInstanceId = instanceId;
1179908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta        mServiceType = serviceType;
1189908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta        mCharacteristics = new ArrayList<BluetoothGattCharacteristic>();
1199908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta        mIncludedServices = new ArrayList<BluetoothGattService>();
1209908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta    }
1219908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta
1229908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta    /**
123bf0faed3597b92d950febafcf555ad05529559d6Jakub Pawlowski     * Create a new BluetoothGattService
124bf0faed3597b92d950febafcf555ad05529559d6Jakub Pawlowski     * @hide
125bf0faed3597b92d950febafcf555ad05529559d6Jakub Pawlowski     */
126bf0faed3597b92d950febafcf555ad05529559d6Jakub Pawlowski    public BluetoothGattService(UUID uuid, int instanceId, int serviceType) {
127bf0faed3597b92d950febafcf555ad05529559d6Jakub Pawlowski        mDevice = null;
128bf0faed3597b92d950febafcf555ad05529559d6Jakub Pawlowski        mUuid = uuid;
129bf0faed3597b92d950febafcf555ad05529559d6Jakub Pawlowski        mInstanceId = instanceId;
130bf0faed3597b92d950febafcf555ad05529559d6Jakub Pawlowski        mServiceType = serviceType;
131bf0faed3597b92d950febafcf555ad05529559d6Jakub Pawlowski        mCharacteristics = new ArrayList<BluetoothGattCharacteristic>();
132bf0faed3597b92d950febafcf555ad05529559d6Jakub Pawlowski        mIncludedServices = new ArrayList<BluetoothGattService>();
133bf0faed3597b92d950febafcf555ad05529559d6Jakub Pawlowski    }
134bf0faed3597b92d950febafcf555ad05529559d6Jakub Pawlowski
135bf0faed3597b92d950febafcf555ad05529559d6Jakub Pawlowski    /**
136bf0faed3597b92d950febafcf555ad05529559d6Jakub Pawlowski     * @hide
137bf0faed3597b92d950febafcf555ad05529559d6Jakub Pawlowski     */
138bf0faed3597b92d950febafcf555ad05529559d6Jakub Pawlowski    public int describeContents() {
139bf0faed3597b92d950febafcf555ad05529559d6Jakub Pawlowski        return 0;
140bf0faed3597b92d950febafcf555ad05529559d6Jakub Pawlowski    }
141bf0faed3597b92d950febafcf555ad05529559d6Jakub Pawlowski
142bf0faed3597b92d950febafcf555ad05529559d6Jakub Pawlowski    @Override
143bf0faed3597b92d950febafcf555ad05529559d6Jakub Pawlowski    public void writeToParcel(Parcel out, int flags) {
144bf0faed3597b92d950febafcf555ad05529559d6Jakub Pawlowski        out.writeParcelable(new ParcelUuid(mUuid), 0);
145bf0faed3597b92d950febafcf555ad05529559d6Jakub Pawlowski        out.writeInt(mInstanceId);
146bf0faed3597b92d950febafcf555ad05529559d6Jakub Pawlowski        out.writeInt(mServiceType);
147bf0faed3597b92d950febafcf555ad05529559d6Jakub Pawlowski        out.writeTypedList(mCharacteristics);
148bf0faed3597b92d950febafcf555ad05529559d6Jakub Pawlowski
149bf0faed3597b92d950febafcf555ad05529559d6Jakub Pawlowski        ArrayList<BluetoothGattIncludedService> includedServices =
150bf0faed3597b92d950febafcf555ad05529559d6Jakub Pawlowski                new ArrayList<BluetoothGattIncludedService>(mIncludedServices.size());
151bf0faed3597b92d950febafcf555ad05529559d6Jakub Pawlowski        for(BluetoothGattService s : mIncludedServices) {
152bf0faed3597b92d950febafcf555ad05529559d6Jakub Pawlowski            includedServices.add(new BluetoothGattIncludedService(s.getUuid(),
153bf0faed3597b92d950febafcf555ad05529559d6Jakub Pawlowski                                                                  s.getInstanceId(), s.getType()));
154bf0faed3597b92d950febafcf555ad05529559d6Jakub Pawlowski        }
155bf0faed3597b92d950febafcf555ad05529559d6Jakub Pawlowski        out.writeTypedList(includedServices);
156bf0faed3597b92d950febafcf555ad05529559d6Jakub Pawlowski     }
157bf0faed3597b92d950febafcf555ad05529559d6Jakub Pawlowski
158bf0faed3597b92d950febafcf555ad05529559d6Jakub Pawlowski    public static final Parcelable.Creator<BluetoothGattService> CREATOR
159bf0faed3597b92d950febafcf555ad05529559d6Jakub Pawlowski            = new Parcelable.Creator<BluetoothGattService>() {
160bf0faed3597b92d950febafcf555ad05529559d6Jakub Pawlowski        public BluetoothGattService createFromParcel(Parcel in) {
161bf0faed3597b92d950febafcf555ad05529559d6Jakub Pawlowski            return new BluetoothGattService(in);
162bf0faed3597b92d950febafcf555ad05529559d6Jakub Pawlowski        }
163bf0faed3597b92d950febafcf555ad05529559d6Jakub Pawlowski
164bf0faed3597b92d950febafcf555ad05529559d6Jakub Pawlowski        public BluetoothGattService[] newArray(int size) {
165bf0faed3597b92d950febafcf555ad05529559d6Jakub Pawlowski            return new BluetoothGattService[size];
166bf0faed3597b92d950febafcf555ad05529559d6Jakub Pawlowski        }
167bf0faed3597b92d950febafcf555ad05529559d6Jakub Pawlowski    };
168bf0faed3597b92d950febafcf555ad05529559d6Jakub Pawlowski
169bf0faed3597b92d950febafcf555ad05529559d6Jakub Pawlowski    private BluetoothGattService(Parcel in) {
170bf0faed3597b92d950febafcf555ad05529559d6Jakub Pawlowski        mUuid = ((ParcelUuid)in.readParcelable(null)).getUuid();
171bf0faed3597b92d950febafcf555ad05529559d6Jakub Pawlowski        mInstanceId = in.readInt();
172bf0faed3597b92d950febafcf555ad05529559d6Jakub Pawlowski        mServiceType = in.readInt();
173bf0faed3597b92d950febafcf555ad05529559d6Jakub Pawlowski
174bf0faed3597b92d950febafcf555ad05529559d6Jakub Pawlowski        mCharacteristics = new ArrayList<BluetoothGattCharacteristic>();
175bf0faed3597b92d950febafcf555ad05529559d6Jakub Pawlowski
176bf0faed3597b92d950febafcf555ad05529559d6Jakub Pawlowski        ArrayList<BluetoothGattCharacteristic> chrcs =
177bf0faed3597b92d950febafcf555ad05529559d6Jakub Pawlowski                in.createTypedArrayList(BluetoothGattCharacteristic.CREATOR);
178bf0faed3597b92d950febafcf555ad05529559d6Jakub Pawlowski        if (chrcs != null) {
179bf0faed3597b92d950febafcf555ad05529559d6Jakub Pawlowski            for (BluetoothGattCharacteristic chrc : chrcs) {
180bf0faed3597b92d950febafcf555ad05529559d6Jakub Pawlowski                chrc.setService(this);
181bf0faed3597b92d950febafcf555ad05529559d6Jakub Pawlowski                mCharacteristics.add(chrc);
182bf0faed3597b92d950febafcf555ad05529559d6Jakub Pawlowski            }
183bf0faed3597b92d950febafcf555ad05529559d6Jakub Pawlowski        }
184bf0faed3597b92d950febafcf555ad05529559d6Jakub Pawlowski
185bf0faed3597b92d950febafcf555ad05529559d6Jakub Pawlowski        mIncludedServices = new ArrayList<BluetoothGattService>();
186bf0faed3597b92d950febafcf555ad05529559d6Jakub Pawlowski
187bf0faed3597b92d950febafcf555ad05529559d6Jakub Pawlowski        ArrayList<BluetoothGattIncludedService> inclSvcs =
188bf0faed3597b92d950febafcf555ad05529559d6Jakub Pawlowski                in.createTypedArrayList(BluetoothGattIncludedService.CREATOR);
189bf0faed3597b92d950febafcf555ad05529559d6Jakub Pawlowski        if (chrcs != null) {
190bf0faed3597b92d950febafcf555ad05529559d6Jakub Pawlowski            for (BluetoothGattIncludedService isvc : inclSvcs) {
191bf0faed3597b92d950febafcf555ad05529559d6Jakub Pawlowski                mIncludedServices.add(new BluetoothGattService(null, isvc.getUuid(),
192bf0faed3597b92d950febafcf555ad05529559d6Jakub Pawlowski                                                            isvc.getInstanceId(), isvc.getType()));
193bf0faed3597b92d950febafcf555ad05529559d6Jakub Pawlowski            }
194bf0faed3597b92d950febafcf555ad05529559d6Jakub Pawlowski        }
195bf0faed3597b92d950febafcf555ad05529559d6Jakub Pawlowski    }
196bf0faed3597b92d950febafcf555ad05529559d6Jakub Pawlowski
197bf0faed3597b92d950febafcf555ad05529559d6Jakub Pawlowski    /**
1989908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta     * Returns the device associated with this service.
1999908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta     * @hide
2009908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta     */
2019908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta    /*package*/ BluetoothDevice getDevice() {
2029908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta        return mDevice;
2039908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta    }
2049908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta
2059908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta    /**
206bf0faed3597b92d950febafcf555ad05529559d6Jakub Pawlowski     * Returns the device associated with this service.
207bf0faed3597b92d950febafcf555ad05529559d6Jakub Pawlowski     * @hide
208bf0faed3597b92d950febafcf555ad05529559d6Jakub Pawlowski     */
209bf0faed3597b92d950febafcf555ad05529559d6Jakub Pawlowski    /*package*/ void setDevice(BluetoothDevice device) {
210bf0faed3597b92d950febafcf555ad05529559d6Jakub Pawlowski        this.mDevice = device;
211bf0faed3597b92d950febafcf555ad05529559d6Jakub Pawlowski    }
212bf0faed3597b92d950febafcf555ad05529559d6Jakub Pawlowski
213bf0faed3597b92d950febafcf555ad05529559d6Jakub Pawlowski    /**
214ddf7e4756c31d0ed90802f98abeaa79df6d16b2aMatthew Xie     * Add an included service to this service.
215ddf7e4756c31d0ed90802f98abeaa79df6d16b2aMatthew Xie     * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission.
216ddf7e4756c31d0ed90802f98abeaa79df6d16b2aMatthew Xie     *
217ddf7e4756c31d0ed90802f98abeaa79df6d16b2aMatthew Xie     * @param service The service to be added
218ddf7e4756c31d0ed90802f98abeaa79df6d16b2aMatthew Xie     * @return true, if the included service was added to the service
219ddf7e4756c31d0ed90802f98abeaa79df6d16b2aMatthew Xie     */
220ddf7e4756c31d0ed90802f98abeaa79df6d16b2aMatthew Xie    public boolean addService(BluetoothGattService service) {
221ddf7e4756c31d0ed90802f98abeaa79df6d16b2aMatthew Xie        mIncludedServices.add(service);
222ddf7e4756c31d0ed90802f98abeaa79df6d16b2aMatthew Xie        return true;
223ddf7e4756c31d0ed90802f98abeaa79df6d16b2aMatthew Xie    }
224ddf7e4756c31d0ed90802f98abeaa79df6d16b2aMatthew Xie
225ddf7e4756c31d0ed90802f98abeaa79df6d16b2aMatthew Xie    /**
2269908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta     * Add a characteristic to this service.
227ddf7e4756c31d0ed90802f98abeaa79df6d16b2aMatthew Xie     * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission.
228ddf7e4756c31d0ed90802f98abeaa79df6d16b2aMatthew Xie     *
229ddf7e4756c31d0ed90802f98abeaa79df6d16b2aMatthew Xie     * @param characteristic The characteristics to be added
230ddf7e4756c31d0ed90802f98abeaa79df6d16b2aMatthew Xie     * @return true, if the characteristic was added to the service
2319908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta     */
232ddf7e4756c31d0ed90802f98abeaa79df6d16b2aMatthew Xie    public boolean addCharacteristic(BluetoothGattCharacteristic characteristic) {
2339908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta        mCharacteristics.add(characteristic);
234ddf7e4756c31d0ed90802f98abeaa79df6d16b2aMatthew Xie        characteristic.setService(this);
235ddf7e4756c31d0ed90802f98abeaa79df6d16b2aMatthew Xie        return true;
2369908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta    }
2379908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta
2389908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta    /**
2399908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta     * Get characteristic by UUID and instanceId.
2409908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta     * @hide
2419908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta     */
2429908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta    /*package*/ BluetoothGattCharacteristic getCharacteristic(UUID uuid, int instanceId) {
2439908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta        for(BluetoothGattCharacteristic characteristic : mCharacteristics) {
24425b9cf953bd3e97f726f8c27d7a752b27c9a2373Andre Eisenbach            if (uuid.equals(characteristic.getUuid())
24525b9cf953bd3e97f726f8c27d7a752b27c9a2373Andre Eisenbach             && characteristic.getInstanceId() == instanceId)
2469908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta                return characteristic;
2479908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta        }
2489908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta        return null;
2499908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta    }
2509908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta
2519908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta    /**
252ddf7e4756c31d0ed90802f98abeaa79df6d16b2aMatthew Xie     * Force the instance ID.
253ddf7e4756c31d0ed90802f98abeaa79df6d16b2aMatthew Xie     * This is needed for conformance testing only.
254ddf7e4756c31d0ed90802f98abeaa79df6d16b2aMatthew Xie     * @hide
255ddf7e4756c31d0ed90802f98abeaa79df6d16b2aMatthew Xie     */
256ddf7e4756c31d0ed90802f98abeaa79df6d16b2aMatthew Xie    public void setInstanceId(int instanceId) {
257ddf7e4756c31d0ed90802f98abeaa79df6d16b2aMatthew Xie        mInstanceId = instanceId;
258ddf7e4756c31d0ed90802f98abeaa79df6d16b2aMatthew Xie    }
259ddf7e4756c31d0ed90802f98abeaa79df6d16b2aMatthew Xie
260ddf7e4756c31d0ed90802f98abeaa79df6d16b2aMatthew Xie    /**
2619908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta     * Get the handle count override (conformance testing.
2629908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta     * @hide
2639908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta     */
2649908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta    /*package*/ int getHandles() {
2659908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta        return mHandles;
2669908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta    }
2679908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta
2689908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta    /**
269ddf7e4756c31d0ed90802f98abeaa79df6d16b2aMatthew Xie     * Force the number of handles to reserve for this service.
270ddf7e4756c31d0ed90802f98abeaa79df6d16b2aMatthew Xie     * This is needed for conformance testing only.
271ddf7e4756c31d0ed90802f98abeaa79df6d16b2aMatthew Xie     * @hide
272ddf7e4756c31d0ed90802f98abeaa79df6d16b2aMatthew Xie     */
273ddf7e4756c31d0ed90802f98abeaa79df6d16b2aMatthew Xie    public void setHandles(int handles) {
274ddf7e4756c31d0ed90802f98abeaa79df6d16b2aMatthew Xie        mHandles = handles;
275ddf7e4756c31d0ed90802f98abeaa79df6d16b2aMatthew Xie    }
276ddf7e4756c31d0ed90802f98abeaa79df6d16b2aMatthew Xie
277ddf7e4756c31d0ed90802f98abeaa79df6d16b2aMatthew Xie    /**
2789908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta     * Add an included service to the internal map.
2799908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta     * @hide
2809908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta     */
281bf0faed3597b92d950febafcf555ad05529559d6Jakub Pawlowski    public void addIncludedService(BluetoothGattService includedService) {
2829908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta        mIncludedServices.add(includedService);
2839908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta    }
2849908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta
2859908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta    /**
2869908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta     * Returns the UUID of this service
2879908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta     *
2889908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta     * @return UUID of this service
2899908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta     */
2909908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta    public UUID getUuid() {
2919908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta        return mUuid;
2929908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta    }
2939908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta
2949908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta    /**
2959908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta     * Returns the instance ID for this service
2969908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta     *
2979908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta     * <p>If a remote device offers multiple services with the same UUID
2989908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta     * (ex. multiple battery services for different batteries), the instance
2999908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta     * ID is used to distuinguish services.
3009908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta     *
3019908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta     * @return Instance ID of this service
3029908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta     */
3039908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta    public int getInstanceId() {
3049908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta        return mInstanceId;
3059908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta    }
3069908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta
3079908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta    /**
3089908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta     * Get the type of this service (primary/secondary)
3099908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta     */
3109908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta    public int getType() {
3119908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta        return mServiceType;
3129908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta    }
3139908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta
3149908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta    /**
315ddf7e4756c31d0ed90802f98abeaa79df6d16b2aMatthew Xie     * Get the list of included GATT services for this service.
3169908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta     *
3179908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta     * @return List of included services or empty list if no included services
3189908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta     *         were discovered.
3199908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta     */
3209908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta    public List<BluetoothGattService> getIncludedServices() {
3219908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta        return mIncludedServices;
3229908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta    }
3239908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta
3249908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta    /**
3259908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta     * Returns a list of characteristics included in this service.
3269908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta     *
3279908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta     * @return Characteristics included in this service
3289908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta     */
3299908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta    public List<BluetoothGattCharacteristic> getCharacteristics() {
3309908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta        return mCharacteristics;
3319908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta    }
3329908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta
3339908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta    /**
3349908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta     * Returns a characteristic with a given UUID out of the list of
3359908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta     * characteristics offered by this service.
3369908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta     *
3379908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta     * <p>This is a convenience function to allow access to a given characteristic
3389908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta     * without enumerating over the list returned by {@link #getCharacteristics}
3399908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta     * manually.
3409908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta     *
3419908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta     * <p>If a remote service offers multiple characteristics with the same
3429908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta     * UUID, the first instance of a characteristic with the given UUID
3439908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta     * is returned.
3449908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta     *
345ddf7e4756c31d0ed90802f98abeaa79df6d16b2aMatthew Xie     * @return GATT characteristic object or null if no characteristic with the
3469908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta     *         given UUID was found.
3479908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta     */
3489908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta    public BluetoothGattCharacteristic getCharacteristic(UUID uuid) {
3499908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta        for(BluetoothGattCharacteristic characteristic : mCharacteristics) {
3509908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta            if (uuid.equals(characteristic.getUuid()))
3519908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta                return characteristic;
3529908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta        }
3539908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta        return null;
3549908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta    }
35518c76934f62410d1499e2eb63635b5dd8da5013aWei Wang
35618c76934f62410d1499e2eb63635b5dd8da5013aWei Wang    /**
35718c76934f62410d1499e2eb63635b5dd8da5013aWei Wang     * Returns whether the uuid of the service should be advertised.
35818c76934f62410d1499e2eb63635b5dd8da5013aWei Wang     * @hide
35918c76934f62410d1499e2eb63635b5dd8da5013aWei Wang     */
36018c76934f62410d1499e2eb63635b5dd8da5013aWei Wang    public boolean isAdvertisePreferred() {
36118c76934f62410d1499e2eb63635b5dd8da5013aWei Wang      return mAdvertisePreferred;
36218c76934f62410d1499e2eb63635b5dd8da5013aWei Wang    }
36318c76934f62410d1499e2eb63635b5dd8da5013aWei Wang
36418c76934f62410d1499e2eb63635b5dd8da5013aWei Wang    /**
36518c76934f62410d1499e2eb63635b5dd8da5013aWei Wang     * Set whether the service uuid should be advertised.
36618c76934f62410d1499e2eb63635b5dd8da5013aWei Wang     * @hide
36718c76934f62410d1499e2eb63635b5dd8da5013aWei Wang     */
36818c76934f62410d1499e2eb63635b5dd8da5013aWei Wang    public void setAdvertisePreferred(boolean advertisePreferred) {
36918c76934f62410d1499e2eb63635b5dd8da5013aWei Wang      this.mAdvertisePreferred = advertisePreferred;
37018c76934f62410d1499e2eb63635b5dd8da5013aWei Wang    }
3719908112fd085d8b0d91e0562d32eebd1884f09a5Ganesh Ganapathi Batta}
372