Home | History | Annotate | Download | only in bluetooth
      1 /*
      2  * Copyright (C) 2011 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 com.android.settingslib.bluetooth;
     18 
     19 import android.bluetooth.BluetoothClass;
     20 import android.bluetooth.BluetoothDevice;
     21 import android.bluetooth.BluetoothPbap;
     22 import android.bluetooth.BluetoothProfile;
     23 import android.bluetooth.BluetoothUuid;
     24 import android.content.Context;
     25 import android.os.ParcelUuid;
     26 import android.util.Log;
     27 
     28 import com.android.internal.annotations.VisibleForTesting;
     29 import com.android.settingslib.R;
     30 
     31 /**
     32  * PBAPServer Profile
     33  */
     34 public class PbapServerProfile implements LocalBluetoothProfile {
     35     private static final String TAG = "PbapServerProfile";
     36 
     37     private BluetoothPbap mService;
     38     private boolean mIsProfileReady;
     39 
     40     @VisibleForTesting
     41     public static final String NAME = "PBAP Server";
     42 
     43     // Order of this profile in device profiles list
     44     private static final int ORDINAL = 6;
     45 
     46     // The UUIDs indicate that remote device might access pbap server
     47     static final ParcelUuid[] PBAB_CLIENT_UUIDS = {
     48         BluetoothUuid.HSP,
     49         BluetoothUuid.Handsfree,
     50         BluetoothUuid.PBAP_PCE
     51     };
     52 
     53     // These callbacks run on the main thread.
     54     private final class PbapServiceListener
     55             implements BluetoothPbap.ServiceListener {
     56 
     57         public void onServiceConnected(BluetoothPbap proxy) {
     58             mService = (BluetoothPbap) proxy;
     59             mIsProfileReady=true;
     60         }
     61 
     62         public void onServiceDisconnected() {
     63             mIsProfileReady=false;
     64         }
     65     }
     66 
     67     public boolean isProfileReady() {
     68         return mIsProfileReady;
     69     }
     70 
     71     @Override
     72     public int getProfileId() {
     73         return BluetoothProfile.PBAP;
     74     }
     75 
     76     PbapServerProfile(Context context) {
     77         BluetoothPbap pbap = new BluetoothPbap(context, new PbapServiceListener());
     78     }
     79 
     80     public boolean accessProfileEnabled() {
     81         return true;
     82     }
     83 
     84     public boolean isAutoConnectable() {
     85         return false;
     86     }
     87 
     88     public boolean connect(BluetoothDevice device) {
     89         /*Can't connect from server */
     90         return false;
     91 
     92     }
     93 
     94     public boolean disconnect(BluetoothDevice device) {
     95         if (mService == null) return false;
     96         return mService.disconnect(device);
     97     }
     98 
     99     public int getConnectionStatus(BluetoothDevice device) {
    100         if (mService == null) {
    101             return BluetoothProfile.STATE_DISCONNECTED;
    102         }
    103         if (mService.isConnected(device))
    104             return BluetoothProfile.STATE_CONNECTED;
    105         else
    106             return BluetoothProfile.STATE_DISCONNECTED;
    107     }
    108 
    109     public boolean isPreferred(BluetoothDevice device) {
    110         return false;
    111     }
    112 
    113     public int getPreferred(BluetoothDevice device) {
    114         return -1;
    115     }
    116 
    117     public void setPreferred(BluetoothDevice device, boolean preferred) {
    118         // ignore: isPreferred is always true for PBAP
    119     }
    120 
    121     public String toString() {
    122         return NAME;
    123     }
    124 
    125     public int getOrdinal() {
    126         return ORDINAL;
    127     }
    128 
    129     public int getNameResource(BluetoothDevice device) {
    130         return R.string.bluetooth_profile_pbap;
    131     }
    132 
    133     public int getSummaryResourceForDevice(BluetoothDevice device) {
    134         return R.string.bluetooth_profile_pbap_summary;
    135     }
    136 
    137     public int getDrawableResource(BluetoothClass btClass) {
    138         return com.android.internal.R.drawable.ic_phone;
    139     }
    140 
    141     protected void finalize() {
    142         Log.d(TAG, "finalize()");
    143         if (mService != null) {
    144             try {
    145                 mService.close();
    146                 mService = null;
    147             }catch (Throwable t) {
    148                 Log.w(TAG, "Error cleaning up PBAP proxy", t);
    149             }
    150         }
    151     }
    152 }
    153