• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
PbapServerProfile(Context context)74     PbapServerProfile(Context context) {
75         BluetoothPbap pbap = new BluetoothPbap(context, new PbapServiceListener());
76     }
77 
isConnectable()78     public boolean isConnectable() {
79         return true;
80     }
81 
isAutoConnectable()82     public boolean isAutoConnectable() {
83         return false;
84     }
85 
connect(BluetoothDevice device)86     public boolean connect(BluetoothDevice device) {
87         /*Can't connect from server */
88         return false;
89 
90     }
91 
disconnect(BluetoothDevice device)92     public boolean disconnect(BluetoothDevice device) {
93         if (mService == null) return false;
94         return mService.disconnect();
95     }
96 
getConnectionStatus(BluetoothDevice device)97     public int getConnectionStatus(BluetoothDevice device) {
98         if (mService == null) {
99             return BluetoothProfile.STATE_DISCONNECTED;
100         }
101         if (mService.isConnected(device))
102             return BluetoothProfile.STATE_CONNECTED;
103         else
104             return BluetoothProfile.STATE_DISCONNECTED;
105     }
106 
isPreferred(BluetoothDevice device)107     public boolean isPreferred(BluetoothDevice device) {
108         return false;
109     }
110 
getPreferred(BluetoothDevice device)111     public int getPreferred(BluetoothDevice device) {
112         return -1;
113     }
114 
setPreferred(BluetoothDevice device, boolean preferred)115     public void setPreferred(BluetoothDevice device, boolean preferred) {
116         // ignore: isPreferred is always true for PBAP
117     }
118 
toString()119     public String toString() {
120         return NAME;
121     }
122 
getOrdinal()123     public int getOrdinal() {
124         return ORDINAL;
125     }
126 
getNameResource(BluetoothDevice device)127     public int getNameResource(BluetoothDevice device) {
128         return R.string.bluetooth_profile_pbap;
129     }
130 
getSummaryResourceForDevice(BluetoothDevice device)131     public int getSummaryResourceForDevice(BluetoothDevice device) {
132         return R.string.bluetooth_profile_pbap_summary;
133     }
134 
getDrawableResource(BluetoothClass btClass)135     public int getDrawableResource(BluetoothClass btClass) {
136         return R.drawable.ic_bt_cellphone;
137     }
138 
finalize()139     protected void finalize() {
140         if (V) Log.d(TAG, "finalize()");
141         if (mService != null) {
142             try {
143                 mService.close();
144                 mService = null;
145             }catch (Throwable t) {
146                 Log.w(TAG, "Error cleaning up PBAP proxy", t);
147             }
148         }
149     }
150 }
151