1 /* 2 * Copyright (C) 2016 Google Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 * use this file except in compliance with the License. You may obtain a copy of 6 * 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, WITHOUT 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 * License for the specific language governing permissions and limitations under 14 * the License. 15 */ 16 17 package com.googlecode.android_scripting.facade.bluetooth; 18 19 import java.util.List; 20 21 import android.app.Service; 22 import android.bluetooth.BluetoothAdapter; 23 import android.bluetooth.BluetoothDevice; 24 import android.bluetooth.BluetoothPan; 25 import android.bluetooth.BluetoothProfile; 26 import android.bluetooth.BluetoothUuid; 27 import android.os.ParcelUuid; 28 29 import com.googlecode.android_scripting.Log; 30 import com.googlecode.android_scripting.facade.FacadeManager; 31 import com.googlecode.android_scripting.jsonrpc.RpcReceiver; 32 import com.googlecode.android_scripting.rpc.Rpc; 33 import com.googlecode.android_scripting.rpc.RpcParameter; 34 35 public class BluetoothPanFacade extends RpcReceiver { 36 37 static final ParcelUuid[] UUIDS = { 38 BluetoothUuid.NAP, 39 BluetoothUuid.PANU, 40 }; 41 42 private final Service mService; 43 private final BluetoothAdapter mBluetoothAdapter; 44 45 private static boolean sIsPanReady = false; 46 private static BluetoothPan sPanProfile = null; 47 BluetoothPanFacade(FacadeManager manager)48 public BluetoothPanFacade(FacadeManager manager) { 49 super(manager); 50 mService = manager.getService(); 51 mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 52 mBluetoothAdapter.getProfileProxy(mService, new PanServiceListener(), 53 BluetoothProfile.PAN); 54 } 55 56 class PanServiceListener implements BluetoothProfile.ServiceListener { 57 @Override onServiceConnected(int profile, BluetoothProfile proxy)58 public void onServiceConnected(int profile, BluetoothProfile proxy) { 59 sPanProfile = (BluetoothPan) proxy; 60 sIsPanReady = true; 61 } 62 63 @Override onServiceDisconnected(int profile)64 public void onServiceDisconnected(int profile) { 65 sIsPanReady = false; 66 } 67 } 68 69 @Rpc(description = "Set Bluetooth Tethering") bluetoothPanSetBluetoothTethering( @pcParametername = "enable") Boolean enable)70 public void bluetoothPanSetBluetoothTethering( 71 @RpcParameter(name = "enable") Boolean enable) { 72 sPanProfile.setBluetoothTethering(enable); 73 } 74 panConnect(BluetoothDevice device)75 public Boolean panConnect(BluetoothDevice device) { 76 if (sPanProfile == null) return false; 77 return sPanProfile.connect(device); 78 } 79 panDisconnect(BluetoothDevice device)80 public Boolean panDisconnect(BluetoothDevice device) { 81 if (sPanProfile == null) return false; 82 return sPanProfile.disconnect(device); 83 } 84 85 @Rpc(description = "Is Pan profile ready.") bluetoothPanIsReady()86 public Boolean bluetoothPanIsReady() { 87 return sIsPanReady; 88 } 89 90 @Rpc(description = "Get all the devices connected through PAN") bluetoothPanGetConnectedDevices()91 public List<BluetoothDevice> bluetoothPanGetConnectedDevices() { 92 return sPanProfile.getConnectedDevices(); 93 } 94 95 @Rpc(description = "Is tethering on.") bluetoothPanIsTetheringOn()96 public Boolean bluetoothPanIsTetheringOn() { 97 if (!sIsPanReady || sPanProfile == null) { 98 return false; 99 } 100 return sPanProfile.isTetheringOn(); 101 } 102 shutdown()103 public void shutdown() { 104 } 105 } 106