Home | History | Annotate | Download | only in bluetooth
      1 /*
      2  * Copyright (C) 2017 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.googlecode.android_scripting.facade.bluetooth;
     18 
     19 import java.util.List;
     20 import java.util.ArrayList;
     21 
     22 import android.app.Service;
     23 import android.bluetooth.BluetoothPbapClient;
     24 import android.bluetooth.BluetoothAdapter;
     25 import android.bluetooth.BluetoothDevice;
     26 import android.bluetooth.BluetoothProfile;
     27 import android.bluetooth.BluetoothUuid;
     28 import android.os.ParcelUuid;
     29 
     30 import com.googlecode.android_scripting.Log;
     31 import com.googlecode.android_scripting.facade.FacadeManager;
     32 import com.googlecode.android_scripting.jsonrpc.RpcReceiver;
     33 import com.googlecode.android_scripting.rpc.Rpc;
     34 import com.googlecode.android_scripting.rpc.RpcParameter;
     35 
     36 public class BluetoothPbapClientFacade extends RpcReceiver {
     37   static final ParcelUuid[] UUIDS = {
     38     BluetoothUuid.PBAP_PSE,
     39   };
     40 
     41   private final Service mService;
     42   private final BluetoothAdapter mBluetoothAdapter;
     43 
     44   private static boolean sIsPbapClientReady = false;
     45   private static BluetoothPbapClient sPbapClientProfile = null;
     46 
     47   public BluetoothPbapClientFacade(FacadeManager manager) {
     48     super(manager);
     49     mService = manager.getService();
     50     mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
     51     mBluetoothAdapter.getProfileProxy(mService, new PbapClientServiceListener(),
     52         BluetoothProfile.PBAP_CLIENT);
     53   }
     54 
     55   class PbapClientServiceListener implements BluetoothProfile.ServiceListener {
     56     @Override
     57     public void onServiceConnected(int profile, BluetoothProfile proxy) {
     58       sPbapClientProfile = (BluetoothPbapClient) proxy;
     59       sIsPbapClientReady = true;
     60     }
     61 
     62     @Override
     63     public void onServiceDisconnected(int profile) {
     64       sIsPbapClientReady = false;
     65     }
     66   }
     67 
     68   public Boolean pbapClientConnect(BluetoothDevice device) {
     69     if (sPbapClientProfile == null) return false;
     70     return sPbapClientProfile.connect(device);
     71   }
     72 
     73   public Boolean pbapClientDisconnect(BluetoothDevice device) {
     74     if (sPbapClientProfile == null) return false;
     75     return sPbapClientProfile.disconnect(device);
     76   }
     77 
     78   @Rpc(description = "Is PbapClient profile ready.")
     79   public Boolean bluetoothPbapClientIsReady() {
     80     return sIsPbapClientReady;
     81   }
     82 
     83   @Rpc(description = "Set priority of the profile")
     84   public void bluetoothPbapClientSetPriority(
     85       @RpcParameter(name = "device", description = "Mac address of a BT device.")
     86       String deviceStr,
     87       @RpcParameter(name = "priority", description = "Priority that needs to be set.")
     88       Integer priority)
     89       throws Exception {
     90     if (sPbapClientProfile == null) return;
     91     BluetoothDevice device =
     92         BluetoothFacade.getDevice(mBluetoothAdapter.getBondedDevices(), deviceStr);
     93     Log.d("Changing priority of device " + device.getAliasName() + " p: " + priority);
     94     sPbapClientProfile.setPriority(device, priority);
     95   }
     96 
     97   @Rpc(description = "Get priority of the profile")
     98   public Integer bluetoothPbapClientGetPriority(
     99       @RpcParameter(name = "device", description = "Mac address of a BT device.")
    100       String deviceStr)
    101       throws Exception {
    102     if (sPbapClientProfile == null) return BluetoothProfile.PRIORITY_UNDEFINED;
    103     BluetoothDevice device =
    104         BluetoothFacade.getDevice(mBluetoothAdapter.getBondedDevices(), deviceStr);
    105     return sPbapClientProfile.getPriority(device);
    106   }
    107 
    108   @Rpc(description = "Connect to an PBAP Client device.")
    109   public Boolean bluetoothPbapClientConnect(
    110       @RpcParameter(name = "device", description = "Name or MAC address of a bluetooth device.")
    111       String deviceStr)
    112       throws Exception {
    113     if (sPbapClientProfile == null) return false;
    114     try {
    115       BluetoothDevice device =
    116           BluetoothFacade.getDevice(mBluetoothAdapter.getBondedDevices(), deviceStr);
    117       Log.d("Connecting to device " + device.getAliasName());
    118       return pbapClientConnect(device);
    119     } catch (Exception e) {
    120         Log.e("bluetoothPbapClientConnect failed on getDevice " + deviceStr + " with " + e);
    121         return false;
    122     }
    123   }
    124 
    125   @Rpc(description = "Disconnect an PBAP Client device.")
    126   public Boolean bluetoothPbapClientDisconnect(
    127       @RpcParameter(name = "device", description = "Name or MAC address of a device.")
    128       String deviceStr) {
    129     if (sPbapClientProfile == null) return false;
    130     Log.d("Connected devices: " + sPbapClientProfile.getConnectedDevices());
    131     try {
    132         BluetoothDevice device =
    133             BluetoothFacade.getDevice(sPbapClientProfile.getConnectedDevices(), deviceStr);
    134         return pbapClientDisconnect(device);
    135     } catch (Exception e) {
    136         // Do nothing since it is disconnect and the above call should force disconnect.
    137         Log.e("bluetoothPbapClientConnect getDevice failed " + e);
    138     }
    139     return false;
    140   }
    141 
    142   @Rpc(description = "Get all the devices connected through PBAP Client.")
    143   public List<BluetoothDevice> bluetoothPbapClientGetConnectedDevices() {
    144     if (sPbapClientProfile == null) return new ArrayList<BluetoothDevice>();
    145     return sPbapClientProfile.getConnectedDevices();
    146   }
    147 
    148   @Rpc(description = "Get the connection status of a device.")
    149   public Integer bluetoothPbapClientGetConnectionStatus(
    150           @RpcParameter(name = "deviceID",
    151                         description = "Name or MAC address of a bluetooth device.")
    152           String deviceID) {
    153       if (sPbapClientProfile == null) {
    154           return BluetoothProfile.STATE_DISCONNECTED;
    155       }
    156       List<BluetoothDevice> deviceList = sPbapClientProfile.getConnectedDevices();
    157       BluetoothDevice device;
    158       try {
    159           device = BluetoothFacade.getDevice(deviceList, deviceID);
    160       } catch (Exception e) {
    161           Log.e(e);
    162           return BluetoothProfile.STATE_DISCONNECTED;
    163       }
    164       return sPbapClientProfile.getConnectionState(device);
    165   }
    166 
    167   @Override
    168   public void shutdown() {
    169   }
    170 }
    171