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