Home | History | Annotate | Download | only in bluetooth
      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.content.Context;
     25 import android.util.Log;
     26 
     27 import com.android.settings.R;
     28 
     29 import java.util.HashMap;
     30 import java.util.List;
     31 
     32 /**
     33  * PBAPServer Profile
     34  */
     35 final class PbapServerProfile implements LocalBluetoothProfile {
     36     private static final String TAG = "PbapServerProfile";
     37     private static boolean V = true;
     38 
     39     private BluetoothPbap mService;
     40     private boolean mIsProfileReady;
     41 
     42     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     // These callbacks run on the main thread.
     48     private final class PbapServiceListener
     49             implements BluetoothPbap.ServiceListener {
     50 
     51         public void onServiceConnected(BluetoothPbap proxy) {
     52             if (V) Log.d(TAG,"Bluetooth service connected");
     53             mService = (BluetoothPbap) proxy;
     54             mIsProfileReady=true;
     55         }
     56 
     57         public void onServiceDisconnected() {
     58             if (V) Log.d(TAG,"Bluetooth service disconnected");
     59             mIsProfileReady=false;
     60         }
     61     }
     62 
     63     public boolean isProfileReady() {
     64         return mIsProfileReady;
     65     }
     66 
     67     PbapServerProfile(Context context) {
     68         BluetoothPbap pbap = new BluetoothPbap(context, new PbapServiceListener());
     69     }
     70 
     71     public boolean isConnectable() {
     72         return true;
     73     }
     74 
     75     public boolean isAutoConnectable() {
     76         return false;
     77     }
     78 
     79     public boolean connect(BluetoothDevice device) {
     80         /*Can't connect from server */
     81         return false;
     82 
     83     }
     84 
     85     public boolean disconnect(BluetoothDevice device) {
     86         if (mService == null) return false;
     87         return mService.disconnect();
     88     }
     89 
     90     public int getConnectionStatus(BluetoothDevice device) {
     91         if (mService == null) {
     92             return BluetoothProfile.STATE_DISCONNECTED;
     93         }
     94         if (mService.isConnected(device))
     95             return BluetoothProfile.STATE_CONNECTED;
     96         else
     97             return BluetoothProfile.STATE_DISCONNECTED;
     98     }
     99 
    100     public boolean isPreferred(BluetoothDevice device) {
    101         return false;
    102     }
    103 
    104     public int getPreferred(BluetoothDevice device) {
    105         return -1;
    106     }
    107 
    108     public void setPreferred(BluetoothDevice device, boolean preferred) {
    109         // ignore: isPreferred is always true for PBAP
    110     }
    111 
    112     public String toString() {
    113         return NAME;
    114     }
    115 
    116     public int getOrdinal() {
    117         return ORDINAL;
    118     }
    119 
    120     public int getNameResource(BluetoothDevice device) {
    121       return 0;
    122     }
    123 
    124     public int getSummaryResourceForDevice(BluetoothDevice device) {
    125         return 0;
    126     }
    127 
    128     public int getDrawableResource(BluetoothClass btClass) {
    129         return 0;
    130     }
    131     protected void finalize() {
    132         if (V) Log.d(TAG, "finalize()");
    133         if (mService != null) {
    134             try {
    135                 mService.close();
    136                 mService = null;
    137             }catch (Throwable t) {
    138                 Log.w(TAG, "Error cleaning up PBAP proxy", t);
    139             }
    140         }
    141     }
    142 }
    143