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