PbapServerProfile.java revision 7ce96b9e610de2782ec5f2af806e7bc0f90c8578
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
17package com.android.settingslib.bluetooth;
18
19import android.bluetooth.BluetoothAdapter;
20import android.bluetooth.BluetoothClass;
21import android.bluetooth.BluetoothDevice;
22import android.bluetooth.BluetoothPbap;
23import android.bluetooth.BluetoothProfile;
24import android.bluetooth.BluetoothUuid;
25import android.content.Context;
26import android.os.ParcelUuid;
27import android.util.Log;
28
29import com.android.settingslib.R;
30
31import java.util.HashMap;
32import java.util.List;
33
34/**
35 * PBAPServer Profile
36 */
37public final class PbapServerProfile implements LocalBluetoothProfile {
38    private static final String TAG = "PbapServerProfile";
39    private static boolean V = true;
40
41    private BluetoothPbap mService;
42    private boolean mIsProfileReady;
43
44    static final String NAME = "PBAP Server";
45
46    // Order of this profile in device profiles list
47    private static final int ORDINAL = 6;
48
49    // The UUIDs indicate that remote device might access pbap server
50    static final ParcelUuid[] PBAB_CLIENT_UUIDS = {
51        BluetoothUuid.HSP,
52        BluetoothUuid.Handsfree,
53        BluetoothUuid.PBAP_PCE
54    };
55
56    // These callbacks run on the main thread.
57    private final class PbapServiceListener
58            implements BluetoothPbap.ServiceListener {
59
60        public void onServiceConnected(BluetoothPbap proxy) {
61            if (V) Log.d(TAG,"Bluetooth service connected");
62            mService = (BluetoothPbap) proxy;
63            mIsProfileReady=true;
64        }
65
66        public void onServiceDisconnected() {
67            if (V) Log.d(TAG,"Bluetooth service disconnected");
68            mIsProfileReady=false;
69        }
70    }
71
72    public boolean isProfileReady() {
73        return mIsProfileReady;
74    }
75
76    PbapServerProfile(Context context) {
77        BluetoothPbap pbap = new BluetoothPbap(context, new PbapServiceListener());
78    }
79
80    public boolean isConnectable() {
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();
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 R.drawable.ic_bt_cellphone;
139    }
140
141    protected void finalize() {
142        if (V) 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