FileDocCategorySizeDatePackage
BluetoothGattService.javaAPI DocAndroid 5.1 API8071Thu Mar 12 22:22:10 GMT 2015android.bluetooth

BluetoothGattService

public class BluetoothGattService extends Object
Represents a Bluetooth GATT Service

Gatt Service contains a collection of {@link BluetoothGattCharacteristic}, as well as referenced services.

Fields Summary
public static final int
SERVICE_TYPE_PRIMARY
Primary service
public static final int
SERVICE_TYPE_SECONDARY
Secondary service (included by primary services)
protected BluetoothDevice
mDevice
The remote device his service is associated with. This applies to client applications only.
protected UUID
mUuid
The UUID of this service.
protected int
mInstanceId
Instance ID for this service.
protected int
mHandles
Handle counter override (for conformance testing).
protected int
mServiceType
Service type (Primary/Secondary).
protected List
mCharacteristics
List of characteristics included in this service.
protected List
mIncludedServices
List of included services for this service.
private boolean
mAdvertisePreferred
Whether the service uuid should be advertised.
Constructors Summary
public BluetoothGattService(UUID uuid, int serviceType)
Create a new BluetoothGattService.

Requires {@link android.Manifest.permission#BLUETOOTH} permission.

param
uuid The UUID for this service
param
serviceType The type of this service, {@link BluetoothGattService#SERVICE_TYPE_PRIMARY} or {@link BluetoothGattService#SERVICE_TYPE_SECONDARY}


                                                  
         
        mDevice = null;
        mUuid = uuid;
        mInstanceId = 0;
        mServiceType = serviceType;
        mCharacteristics = new ArrayList<BluetoothGattCharacteristic>();
        mIncludedServices = new ArrayList<BluetoothGattService>();
    
BluetoothGattService(BluetoothDevice device, UUID uuid, int instanceId, int serviceType)
Create a new BluetoothGattService

hide

        mDevice = device;
        mUuid = uuid;
        mInstanceId = instanceId;
        mServiceType = serviceType;
        mCharacteristics = new ArrayList<BluetoothGattCharacteristic>();
        mIncludedServices = new ArrayList<BluetoothGattService>();
    
Methods Summary
public booleanaddCharacteristic(BluetoothGattCharacteristic characteristic)
Add a characteristic to this service.

Requires {@link android.Manifest.permission#BLUETOOTH} permission.

param
characteristic The characteristics to be added
return
true, if the characteristic was added to the service

        mCharacteristics.add(characteristic);
        characteristic.setService(this);
        return true;
    
voidaddIncludedService(android.bluetooth.BluetoothGattService includedService)
Add an included service to the internal map.

hide

        mIncludedServices.add(includedService);
    
public booleanaddService(android.bluetooth.BluetoothGattService service)
Add an included service to this service.

Requires {@link android.Manifest.permission#BLUETOOTH} permission.

param
service The service to be added
return
true, if the included service was added to the service

        mIncludedServices.add(service);
        return true;
    
public BluetoothGattCharacteristicgetCharacteristic(java.util.UUID uuid)
Returns a characteristic with a given UUID out of the list of characteristics offered by this service.

This is a convenience function to allow access to a given characteristic without enumerating over the list returned by {@link #getCharacteristics} manually.

If a remote service offers multiple characteristics with the same UUID, the first instance of a characteristic with the given UUID is returned.

return
GATT characteristic object or null if no characteristic with the given UUID was found.

        for(BluetoothGattCharacteristic characteristic : mCharacteristics) {
            if (uuid.equals(characteristic.getUuid()))
                return characteristic;
        }
        return null;
    
BluetoothGattCharacteristicgetCharacteristic(java.util.UUID uuid, int instanceId)
Get characteristic by UUID and instanceId.

hide

        for(BluetoothGattCharacteristic characteristic : mCharacteristics) {
            if (uuid.equals(characteristic.getUuid())
             && characteristic.getInstanceId() == instanceId)
                return characteristic;
        }
        return null;
    
public java.util.ListgetCharacteristics()
Returns a list of characteristics included in this service.

return
Characteristics included in this service

        return mCharacteristics;
    
BluetoothDevicegetDevice()
Returns the device associated with this service.

hide

        return mDevice;
    
intgetHandles()
Get the handle count override (conformance testing.

hide

        return mHandles;
    
public java.util.ListgetIncludedServices()
Get the list of included GATT services for this service.

return
List of included services or empty list if no included services were discovered.

        return mIncludedServices;
    
public intgetInstanceId()
Returns the instance ID for this service

If a remote device offers multiple services with the same UUID (ex. multiple battery services for different batteries), the instance ID is used to distuinguish services.

return
Instance ID of this service

        return mInstanceId;
    
public intgetType()
Get the type of this service (primary/secondary)

        return mServiceType;
    
public java.util.UUIDgetUuid()
Returns the UUID of this service

return
UUID of this service

        return mUuid;
    
public booleanisAdvertisePreferred()
Returns whether the uuid of the service should be advertised.

hide

      return mAdvertisePreferred;
    
public voidsetAdvertisePreferred(boolean advertisePreferred)
Set whether the service uuid should be advertised.

hide

      this.mAdvertisePreferred = advertisePreferred;
    
public voidsetHandles(int handles)
Force the number of handles to reserve for this service. This is needed for conformance testing only.

hide

        mHandles = handles;
    
public voidsetInstanceId(int instanceId)
Force the instance ID. This is needed for conformance testing only.

hide

        mInstanceId = instanceId;