Home | History | Annotate | Download | only in bluetooth
      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 
     17 package android.bluetooth;
     18 import android.os.ParcelUuid;
     19 import android.os.RemoteException;
     20 import android.util.Log;
     21 
     22 import java.util.Collections;
     23 import java.util.List;
     24 
     25 
     26 /**
     27  * This class provides the public APIs to set advertising and scan response data when BLE device
     28  * operates in peripheral mode. <br>
     29  * The exact format is defined in Bluetooth 4.0 specification, Volume 3, Part C, Section 11
     30  * @hide
     31  */
     32 public final class BluetoothAdvScanData {
     33 
     34   /**
     35    * Available data types of {@link BluetoothAdvScanData}.
     36    */
     37   public static final int AD = 0;  // Advertising Data
     38   public static final int SCAN_RESPONSE = 1;  // Scan Response
     39   public static final int EIR = 2;  // Extended Inquiry Response
     40 
     41   private static final String TAG = "BluetoothAdvScanData";
     42 
     43   /**
     44    * Data type of BluetoothAdvScanData.
     45    */
     46   private final int mDataType;
     47   /**
     48    * Bluetooth Gatt Service.
     49    */
     50   private IBluetoothGatt mBluetoothGatt;
     51 
     52   /**
     53    * @param mBluetoothGatt
     54    * @param dataType
     55    */
     56   public BluetoothAdvScanData(IBluetoothGatt mBluetoothGatt, int dataType) {
     57     this.mBluetoothGatt = mBluetoothGatt;
     58     this.mDataType = dataType;
     59   }
     60 
     61   /**
     62    * @return advertising data type.
     63    */
     64   public int getDataType() {
     65     return mDataType;
     66   }
     67 
     68   /**
     69    * Set manufactureCode and manufactureData.
     70    * Returns true if manufacturer data is set, false if there is no enough room to set
     71    * manufacturer data or the data is already set.
     72    * @param manufacturerCode - unique identifier for the manufacturer
     73    * @param manufacturerData - data associated with the specific manufacturer.
     74    */
     75   public boolean setManufacturerData(int manufacturerCode, byte[] manufacturerData) {
     76     if (mDataType != AD) return false;
     77     try {
     78       return mBluetoothGatt.setAdvManufacturerCodeAndData(manufacturerCode, manufacturerData);
     79     } catch (RemoteException e) {
     80       Log.e(TAG, "Unable to set manufacturer id and data.", e);
     81       return false;
     82     }
     83   }
     84 
     85   /**
     86    * Set service data.  Note the service data can only be set when the data type is {@code AD};
     87    * @param serviceData
     88    */
     89   public boolean setServiceData(byte[] serviceData) {
     90 
     91     if (mDataType != AD) return false;
     92     if (serviceData == null) return false;
     93     try {
     94       return mBluetoothGatt.setAdvServiceData(serviceData);
     95     } catch (RemoteException e) {
     96       Log.e(TAG, "Unable to set service data.", e);
     97       return false;
     98     }
     99   }
    100 
    101   /**
    102    * Returns an immutable list of service uuids that will be advertised.
    103    */
    104   public List<ParcelUuid> getServiceUuids() {
    105     try {
    106       return Collections.unmodifiableList(mBluetoothGatt.getAdvServiceUuids());
    107     } catch (RemoteException e) {
    108       Log.e(TAG, "Unable to get service uuids.", e);
    109       return null;
    110     }
    111   }
    112 
    113   /**
    114    * Returns manufacturer data.
    115    */
    116   public byte[] getManufacturerData() {
    117     if (mBluetoothGatt == null) return null;
    118     try {
    119       return mBluetoothGatt.getAdvManufacturerData();
    120     } catch (RemoteException e) {
    121       Log.e(TAG, "Unable to get manufacturer data.", e);
    122       return null;
    123     }
    124   }
    125 
    126   /**
    127    * Returns service data.
    128    */
    129   public byte[] getServiceData() {
    130     if (mBluetoothGatt == null) return null;
    131     try {
    132       return mBluetoothGatt.getAdvServiceData();
    133     } catch (RemoteException e) {
    134       Log.e(TAG, "Unable to get service data.", e);
    135       return null;
    136     }
    137   }
    138 
    139   /**
    140    * Remove manufacturer data based on given manufacturer code.
    141    * @param manufacturerCode
    142    */
    143   public void removeManufacturerCodeAndData(int manufacturerCode) {
    144     if (mBluetoothGatt != null) {
    145       try {
    146         mBluetoothGatt.removeAdvManufacturerCodeAndData(manufacturerCode);
    147       } catch (RemoteException e) {
    148         Log.e(TAG, "Unable to remove manufacturer : " + manufacturerCode, e);
    149       }
    150     }
    151   }
    152 }
    153