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 android.app.Service; 20 import android.bluetooth.BluetoothAdapter; 21 import android.bluetooth.BluetoothDevice; 22 import android.bluetooth.BluetoothPan; 23 import android.bluetooth.BluetoothProfile; 24 import android.bluetooth.BluetoothUuid; 25 import android.os.ParcelUuid; 26 27 import com.googlecode.android_scripting.facade.FacadeManager; 28 import com.googlecode.android_scripting.jsonrpc.RpcReceiver; 29 import com.googlecode.android_scripting.rpc.Rpc; 30 import com.googlecode.android_scripting.rpc.RpcParameter; 31 32 import java.util.List; 33 34 public class BluetoothPanFacade extends RpcReceiver { 35 36 static final ParcelUuid[] UUIDS = { 37 BluetoothUuid.NAP, 38 BluetoothUuid.PANU, 39 }; 40 41 private final Service mService; 42 private final BluetoothAdapter mBluetoothAdapter; 43 44 private static boolean sIsPanReady = false; 45 private static BluetoothPan sPanProfile = null; 46 BluetoothPanFacade(FacadeManager manager)47 public BluetoothPanFacade(FacadeManager manager) { 48 super(manager); 49 mService = manager.getService(); 50 mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 51 mBluetoothAdapter.getProfileProxy(mService, new PanServiceListener(), 52 BluetoothProfile.PAN); 53 } 54 55 class PanServiceListener implements BluetoothProfile.ServiceListener { 56 @Override onServiceConnected(int profile, BluetoothProfile proxy)57 public void onServiceConnected(int profile, BluetoothProfile proxy) { 58 sPanProfile = (BluetoothPan) proxy; 59 sIsPanReady = true; 60 } 61 62 @Override onServiceDisconnected(int profile)63 public void onServiceDisconnected(int profile) { 64 sIsPanReady = false; 65 } 66 } 67 68 @Rpc(description = "Set Bluetooth Tethering") bluetoothPanSetBluetoothTethering( @pcParametername = "enable") Boolean enable)69 public void bluetoothPanSetBluetoothTethering( 70 @RpcParameter(name = "enable") Boolean enable) { 71 sPanProfile.setBluetoothTethering(enable); 72 } 73 panConnect(BluetoothDevice device)74 public Boolean panConnect(BluetoothDevice device) { 75 if (sPanProfile == null) return false; 76 return sPanProfile.connect(device); 77 } 78 panDisconnect(BluetoothDevice device)79 public Boolean panDisconnect(BluetoothDevice device) { 80 if (sPanProfile == null) return false; 81 return sPanProfile.disconnect(device); 82 } 83 84 @Rpc(description = "Is Pan profile ready.") bluetoothPanIsReady()85 public Boolean bluetoothPanIsReady() { 86 return sIsPanReady; 87 } 88 89 @Rpc(description = "Get all the devices connected through PAN") bluetoothPanGetConnectedDevices()90 public List<BluetoothDevice> bluetoothPanGetConnectedDevices() { 91 return sPanProfile.getConnectedDevices(); 92 } 93 94 @Rpc(description = "Is tethering on.") bluetoothPanIsTetheringOn()95 public Boolean bluetoothPanIsTetheringOn() { 96 if (!sIsPanReady || sPanProfile == null) { 97 return false; 98 } 99 return sPanProfile.isTetheringOn(); 100 } 101 shutdown()102 public void shutdown() { 103 } 104 } 105