BluetoothPan.java revision 5200c8ab721b56025340306bdecca651e6bf2f12
1/* 2 * Copyright (C) 2008 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 17package android.bluetooth; 18 19import android.annotation.SdkConstant; 20import android.annotation.SdkConstant.SdkConstantType; 21import android.content.Context; 22import android.os.IBinder; 23import android.os.RemoteException; 24import android.os.ServiceManager; 25import android.util.Log; 26 27import java.util.ArrayList; 28import java.util.List; 29 30/** 31 * @hide 32 */ 33public final class BluetoothPan { 34 private static final String TAG = "BluetoothPan"; 35 private static final boolean DBG = false; 36 37 //TODO: This needs to inherit from BluetoothProfile like other profiles. 38 39 /** int extra for ACTION_PAN_STATE_CHANGED */ 40 public static final String EXTRA_PAN_STATE = "android.bluetooth.pan.extra.STATE"; 41 42 /** int extra for ACTION_PAN_STATE_CHANGED */ 43 public static final String EXTRA_PREVIOUS_PAN_STATE = 44 "android.bluetooth.pan.extra.PREVIOUS_STATE"; 45 46 /** int extra for ACTION_PAN_STATE_CHANGED */ 47 public static final String EXTRA_LOCAL_ROLE = "android.bluetooth.pan.extra.LOCAL_ROLE"; 48 49 public static final int LOCAL_NAP_ROLE = 1; 50 public static final int LOCAL_PANU_ROLE = 2; 51 52 /** 53 * Indicates the state of an PAN device has changed. 54 * This intent will always contain EXTRA_DEVICE_STATE, 55 * EXTRA_PREVIOUS_DEVICE_STATE, BluetoothDevice.EXTRA_DEVICE 56 * and EXTRA_LOCAL_ROLE. 57 * extras. 58 */ 59 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) 60 public static final String ACTION_PAN_STATE_CHANGED = 61 "android.bluetooth.pan.action.STATE_CHANGED"; 62 63 public static final String NAP_ROLE = "nap"; 64 public static final String NAP_BRIDGE = "pan1"; 65 66 public static final int MAX_CONNECTIONS = 7; 67 68 public static final int STATE_DISCONNECTED = 0; 69 public static final int STATE_CONNECTING = 1; 70 public static final int STATE_CONNECTED = 2; 71 public static final int STATE_DISCONNECTING = 3; 72 73 private final IBluetooth mService; 74 private final Context mContext; 75 76 /** 77 * Create a BluetoothPan proxy object for interacting with the local 78 * Bluetooth Pan service. 79 * @param c Context 80 */ 81 public BluetoothPan(Context c) { 82 mContext = c; 83 84 IBinder b = ServiceManager.getService(BluetoothAdapter.BLUETOOTH_SERVICE); 85 if (b != null) { 86 mService = IBluetooth.Stub.asInterface(b); 87 } else { 88 Log.w(TAG, "Bluetooth Service not available!"); 89 90 // Instead of throwing an exception which prevents people from going 91 // into Wireless settings in the emulator. Let it crash later 92 // when it is actually used. 93 mService = null; 94 } 95 } 96 97 /** 98 * Initiate a PAN connection. 99 * 100 * This function returns false on error and true if the connection 101 * attempt is being made. 102 * 103 * Listen for {@link #ACTION_PAN_STATE_CHANGED} to find out when the 104 * connection is completed. 105 * 106 * @param device Remote BT device. 107 * @return false on immediate error, true otherwise 108 * @hide 109 */ 110 public boolean connect(BluetoothDevice device) { 111 if (DBG) log("connect(" + device + ")"); 112 try { 113 return mService.connectPanDevice(device); 114 } catch (RemoteException e) { 115 Log.e(TAG, "", e); 116 return false; 117 } 118 } 119 120 /** 121 * Initiate disconnect from PAN. 122 * 123 * This function return false on error and true if the disconnection 124 * attempt is being made. 125 * 126 * Listen for {@link #ACTION_PAN_STATE_CHANGED} to find out when 127 * disconnect is completed. 128 * 129 * @param device Remote BT device. 130 * @return false on immediate error, true otherwise 131 * @hide 132 */ 133 public boolean disconnect(BluetoothDevice device) { 134 if (DBG) log("disconnect(" + device + ")"); 135 try { 136 return mService.disconnectPanDevice(device); 137 } catch (RemoteException e) { 138 Log.e(TAG, "", e); 139 return false; 140 } 141 } 142 143 /** 144 * Get the state of a PAN Device. 145 * 146 * This function returns an int representing the state of the PAN connection 147 * 148 * @param device Remote BT device. 149 * @return The current state of the PAN Device 150 * @hide 151 */ 152 public int getPanDeviceState(BluetoothDevice device) { 153 if (DBG) log("getPanDeviceState(" + device + ")"); 154 try { 155 return mService.getPanDeviceState(device); 156 } catch (RemoteException e) { 157 Log.e(TAG, "", e); 158 return STATE_DISCONNECTED; 159 } 160 } 161 162 /** 163 * Returns a set of all the connected PAN Devices 164 * 165 * Does not include devices that are currently connecting or disconnecting 166 * 167 * @return List of PAN devices or empty on Error 168 * @hide 169 */ 170 public List<BluetoothDevice> getConnectedDevices() { 171 if (DBG) log("getConnectedDevices"); 172 try { 173 return mService.getConnectedPanDevices(); 174 } catch (RemoteException e) { 175 Log.e(TAG, "", e); 176 return new ArrayList<BluetoothDevice>(); 177 } 178 } 179 180 private static void log(String msg) { 181 Log.d(TAG, msg); 182 } 183 184 public void setBluetoothTethering(boolean value) { 185 try { 186 mService.setBluetoothTethering(value); 187 } catch (RemoteException e) { 188 Log.e(TAG, "", e); 189 } 190 } 191 192 public boolean isTetheringOn() { 193 try { 194 return mService.isTetheringOn(); 195 } catch (RemoteException e) { 196 Log.e(TAG, "", e); 197 return false; 198 } 199 } 200} 201