PbapServerProfile.java revision 08c7116ab9cd04ad6dd3c04aa1017237e7f409ac
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.BluetoothClass;
20import android.bluetooth.BluetoothDevice;
21import android.bluetooth.BluetoothPbap;
22import android.bluetooth.BluetoothProfile;
23import android.bluetooth.BluetoothUuid;
24import android.content.Context;
25import android.os.ParcelUuid;
26import android.util.Log;
27
28import com.android.settingslib.R;
29
30/**
31 * PBAPServer Profile
32 */
33public 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
56        public void onServiceConnected(BluetoothPbap proxy) {
57            if (V) Log.d(TAG,"Bluetooth service connected");
58            mService = (BluetoothPbap) proxy;
59            mIsProfileReady=true;
60        }
61
62        public void onServiceDisconnected() {
63            if (V) Log.d(TAG,"Bluetooth service disconnected");
64            mIsProfileReady=false;
65        }
66    }
67
68    public boolean isProfileReady() {
69        return mIsProfileReady;
70    }
71
72    PbapServerProfile(Context context) {
73        BluetoothPbap pbap = new BluetoothPbap(context, new PbapServiceListener());
74    }
75
76    public boolean isConnectable() {
77        return true;
78    }
79
80    public boolean isAutoConnectable() {
81        return false;
82    }
83
84    public boolean connect(BluetoothDevice device) {
85        /*Can't connect from server */
86        return false;
87
88    }
89
90    public boolean disconnect(BluetoothDevice device) {
91        if (mService == null) return false;
92        return mService.disconnect();
93    }
94
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
105    public boolean isPreferred(BluetoothDevice device) {
106        return false;
107    }
108
109    public int getPreferred(BluetoothDevice device) {
110        return -1;
111    }
112
113    public void setPreferred(BluetoothDevice device, boolean preferred) {
114        // ignore: isPreferred is always true for PBAP
115    }
116
117    public String toString() {
118        return NAME;
119    }
120
121    public int getOrdinal() {
122        return ORDINAL;
123    }
124
125    public int getNameResource(BluetoothDevice device) {
126        return R.string.bluetooth_profile_pbap;
127    }
128
129    public int getSummaryResourceForDevice(BluetoothDevice device) {
130        return R.string.bluetooth_profile_pbap_summary;
131    }
132
133    public int getDrawableResource(BluetoothClass btClass) {
134        return R.drawable.ic_bt_cellphone;
135    }
136
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