Home | History | Annotate | Download | only in car
      1 /*
      2  * Copyright (C) 2017 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.car;
     17 
     18 import android.Manifest;
     19 import android.annotation.IntDef;
     20 import android.annotation.RequiresPermission;
     21 import android.bluetooth.BluetoothDevice;
     22 import android.car.CarLibLog;
     23 import android.car.CarManagerBase;
     24 import android.car.CarNotConnectedException;
     25 import android.car.ICarBluetooth;
     26 import android.content.Context;
     27 import android.os.IBinder;
     28 import android.os.RemoteException;
     29 import android.util.Log;
     30 
     31 import java.lang.annotation.Retention;
     32 import java.lang.annotation.RetentionPolicy;
     33 
     34 /**
     35  * APIs for setting Car specific Bluetooth Connection Management policy
     36  *
     37  * @hide
     38  */
     39 public final class CarBluetoothManager implements CarManagerBase {
     40     private static final String TAG = "CarBluetoothManager";
     41     private final Context mContext;
     42     private final ICarBluetooth mService;
     43 
     44     @Retention(RetentionPolicy.SOURCE)
     45     @IntDef({BLUETOOTH_DEVICE_CONNECTION_PRIORITY_0,
     46             BLUETOOTH_DEVICE_CONNECTION_PRIORITY_1})
     47     public @interface PriorityType {
     48     }
     49 
     50     public static final int BLUETOOTH_DEVICE_CONNECTION_PRIORITY_0 = 0;
     51     public static final int BLUETOOTH_DEVICE_CONNECTION_PRIORITY_1 = 1;
     52     // Write an empty string to clear a Primary or Secondary device.
     53     public static final String BLUETOOTH_NO_PRIORITY_DEVICE = "";
     54 
     55     /**
     56      * Set the Auto Connect priority for a paired Bluetooth Device.
     57      * For example, if a device is tagged as a Primary device (Priority 0) for a supported
     58      * Bluetooth profile, every new Auto Connect attempt would start with trying to connect to
     59      * *that* device. This priority is set at a Bluetooth profile granularity.
     60      *
     61      * @param deviceToSet   - Device to set priority (Tag)
     62      * @param profileToSet  - BluetoothProfile to set priority for
     63      * @param priorityToSet - What priority level to set to
     64      * @hide
     65      */
     66     @RequiresPermission(Manifest.permission.BLUETOOTH_ADMIN)
     67     public void setBluetoothDeviceConnectionPriority(BluetoothDevice deviceToSet, int profileToSet,
     68             @PriorityType int priorityToSet) throws CarNotConnectedException {
     69         try {
     70             mService.setBluetoothDeviceConnectionPriority(deviceToSet, profileToSet, priorityToSet);
     71         } catch (RemoteException e) {
     72             Log.e(CarLibLog.TAG_CAR, "setBluetoothDeviceConnectionPriority failed", e);
     73             throw new CarNotConnectedException(e);
     74         }
     75     }
     76 
     77     /**
     78      * Unset the Auto Connect priority for the given profile
     79      *
     80      * @param profileToClear  - Profile to unset priority
     81      * @param priorityToClear - Which priority to clear (Primary or Secondary)
     82      * @hide
     83      */
     84     @RequiresPermission(Manifest.permission.BLUETOOTH_ADMIN)
     85     public void clearBluetoothDeviceConnectionPriority(int profileToClear,
     86             @PriorityType int priorityToClear) throws CarNotConnectedException {
     87         try {
     88             mService.clearBluetoothDeviceConnectionPriority(profileToClear, priorityToClear);
     89         } catch (RemoteException e) {
     90             Log.e(CarLibLog.TAG_CAR, "clearBluetoothDeviceConnectionPriority failed", e);
     91             throw new CarNotConnectedException(e);
     92         }
     93     }
     94 
     95     /**
     96      * Returns if there is a device that has been tagged with the given priority for the given
     97      * profile.
     98      *
     99      * @param profile         - BluetoothProfile
    100      * @param priorityToCheck - Priority to check
    101      * @return true if there is a device present with the given priority, false if not
    102      * @hide
    103      */
    104     @RequiresPermission(Manifest.permission.BLUETOOTH_ADMIN)
    105     public boolean isPriorityDevicePresent(int profile, @PriorityType int priorityToCheck)
    106             throws CarNotConnectedException {
    107         try {
    108             return mService.isPriorityDevicePresent(profile, priorityToCheck);
    109         } catch (RemoteException e) {
    110             Log.e(CarLibLog.TAG_CAR, "isPrioritySet failed", e);
    111             throw new CarNotConnectedException(e);
    112         }
    113     }
    114 
    115     /**
    116      * Returns the Bluetooth device address as a String that has been tagged with the given priority
    117      * for the given profile.
    118      *
    119      * @param profile         - BluetoothProfile
    120      * @param priorityToCheck - Priority to check
    121      * @return BluetoothDevice address if present, null if absent
    122      * @hide
    123      */
    124     @RequiresPermission(Manifest.permission.BLUETOOTH_ADMIN)
    125     public String getDeviceNameWithPriority(int profile, @PriorityType int priorityToCheck)
    126             throws CarNotConnectedException {
    127         try {
    128             return mService.getDeviceNameWithPriority(profile, priorityToCheck);
    129         } catch (RemoteException e) {
    130             Log.e(CarLibLog.TAG_CAR, "getDeviceNameWithPriority failed", e);
    131             throw new CarNotConnectedException(e);
    132         }
    133     }
    134 
    135     /** @hide */
    136     public CarBluetoothManager(IBinder service, Context context) {
    137         mContext = context;
    138         mService = ICarBluetooth.Stub.asInterface(service);
    139     }
    140 
    141     @Override
    142     public void onCarDisconnected() {
    143     }
    144 }
    145