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