BluetoothPan.java revision 74ef1199459629c5dd9f272f8cd706d82cdfeeb1
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/** 32 * This class provides the APIs to control the Bluetooth Pan 33 * Profile. 34 * 35 *<p>BluetoothPan is a proxy object for controlling the Bluetooth 36 * Service via IPC. Use {@link BluetoothAdapter#getProfileProxy} to get 37 * the BluetoothPan proxy object. 38 * 39 *<p>Each method is protected with its appropriate permission. 40 *@hide 41 */ 42public final class BluetoothPan implements BluetoothProfile { 43 private static final String TAG = "BluetoothPan"; 44 private static final boolean DBG = false; 45 46 /** 47 * Intent used to broadcast the change in connection state of the Pan 48 * profile. 49 * 50 * <p>This intent will have 4 extras: 51 * <ul> 52 * <li> {@link #EXTRA_STATE} - The current state of the profile. </li> 53 * <li> {@link #EXTRA_PREVIOUS_STATE}- The previous state of the profile.</li> 54 * <li> {@link BluetoothDevice#EXTRA_DEVICE} - The remote device. </li> 55 * <li> {@link #EXTRA_LOCAL_ROLE} - Which local role the remote device is 56 * bound to. </li> 57 * </ul> 58 * 59 * <p>{@link #EXTRA_STATE} or {@link #EXTRA_PREVIOUS_STATE} can be any of 60 * {@link #STATE_DISCONNECTED}, {@link #STATE_CONNECTING}, 61 * {@link #STATE_CONNECTED}, {@link #STATE_DISCONNECTING}. 62 * 63 * <p> {@link #EXTRA_LOCAL_ROLE} can be one of {@link #LOCAL_NAP_ROLE} or 64 * {@link #LOCAL_PANU_ROLE} 65 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission to 66 * receive. 67 */ 68 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) 69 public static final String ACTION_CONNECTION_STATE_CHANGED = 70 "android.bluetooth.pan.profile.action.CONNECTION_STATE_CHANGED"; 71 72 /** 73 * Extra for {@link #ACTION_CONNECTION_STATE_CHANGED} intent 74 * The local role of the PAN profile that the remote device is bound to. 75 * It can be one of {@link #LOCAL_NAP_ROLE} or {@link #LOCAL_PANU_ROLE}. 76 */ 77 public static final String EXTRA_LOCAL_ROLE = "android.bluetooth.pan.extra.LOCAL_ROLE"; 78 79 /** 80 * The local device is acting as a Network Access Point. 81 */ 82 public static final int LOCAL_NAP_ROLE = 1; 83 84 /** 85 * The local device is acting as a PAN User. 86 */ 87 public static final int LOCAL_PANU_ROLE = 2; 88 89 /** 90 * Return codes for the connect and disconnect Bluez / Dbus calls. 91 * @hide 92 */ 93 public static final int PAN_DISCONNECT_FAILED_NOT_CONNECTED = 1000; 94 95 /** 96 * @hide 97 */ 98 public static final int PAN_CONNECT_FAILED_ALREADY_CONNECTED = 1001; 99 100 /** 101 * @hide 102 */ 103 public static final int PAN_CONNECT_FAILED_ATTEMPT_FAILED = 1002; 104 105 /** 106 * @hide 107 */ 108 public static final int PAN_OPERATION_GENERIC_FAILURE = 1003; 109 110 /** 111 * @hide 112 */ 113 public static final int PAN_OPERATION_SUCCESS = 1004; 114 115 private ServiceListener mServiceListener; 116 private BluetoothAdapter mAdapter; 117 private IBluetooth mService; 118 119 /** 120 * Create a BluetoothPan proxy object for interacting with the local 121 * Bluetooth Service which handles the Pan profile 122 * 123 */ 124 /*package*/ BluetoothPan(Context mContext, ServiceListener l) { 125 IBinder b = ServiceManager.getService(BluetoothAdapter.BLUETOOTH_SERVICE); 126 mServiceListener = l; 127 mAdapter = BluetoothAdapter.getDefaultAdapter(); 128 if (b != null) { 129 mService = IBluetooth.Stub.asInterface(b); 130 if (mServiceListener != null) { 131 mServiceListener.onServiceConnected(BluetoothProfile.PAN, this); 132 } 133 } else { 134 Log.w(TAG, "Bluetooth Service not available!"); 135 136 // Instead of throwing an exception which prevents people from going 137 // into Wireless settings in the emulator. Let it crash later when it is actually used. 138 mService = null; 139 } 140 } 141 142 /** 143 * {@inheritDoc} 144 * @hide 145 */ 146 public boolean connect(BluetoothDevice device) { 147 if (DBG) log("connect(" + device + ")"); 148 if (mService != null && isEnabled() && 149 isValidDevice(device)) { 150 try { 151 return mService.connectPanDevice(device); 152 } catch (RemoteException e) { 153 Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); 154 return false; 155 } 156 } 157 if (mService == null) Log.w(TAG, "Proxy not attached to service"); 158 return false; 159 } 160 161 /** 162 * {@inheritDoc} 163 * @hide 164 */ 165 public boolean disconnect(BluetoothDevice device) { 166 if (DBG) log("disconnect(" + device + ")"); 167 if (mService != null && isEnabled() && 168 isValidDevice(device)) { 169 try { 170 return mService.disconnectPanDevice(device); 171 } catch (RemoteException e) { 172 Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); 173 return false; 174 } 175 } 176 if (mService == null) Log.w(TAG, "Proxy not attached to service"); 177 return false; 178 } 179 180 /** 181 * {@inheritDoc} 182 */ 183 public List<BluetoothDevice> getConnectedDevices() { 184 if (DBG) log("getConnectedDevices()"); 185 if (mService != null && isEnabled()) { 186 try { 187 return mService.getConnectedPanDevices(); 188 } catch (RemoteException e) { 189 Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); 190 return new ArrayList<BluetoothDevice>(); 191 } 192 } 193 if (mService == null) Log.w(TAG, "Proxy not attached to service"); 194 return new ArrayList<BluetoothDevice>(); 195 } 196 197 /** 198 * {@inheritDoc} 199 */ 200 public List<BluetoothDevice> getDevicesMatchingConnectionStates(int[] states) { 201 if (DBG) log("getDevicesMatchingStates()"); 202 if (mService != null && isEnabled()) { 203 try { 204 return mService.getPanDevicesMatchingConnectionStates(states); 205 } catch (RemoteException e) { 206 Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); 207 return new ArrayList<BluetoothDevice>(); 208 } 209 } 210 if (mService == null) Log.w(TAG, "Proxy not attached to service"); 211 return new ArrayList<BluetoothDevice>(); 212 } 213 214 /** 215 * {@inheritDoc} 216 */ 217 public int getConnectionState(BluetoothDevice device) { 218 if (DBG) log("getState(" + device + ")"); 219 if (mService != null && isEnabled() 220 && isValidDevice(device)) { 221 try { 222 return mService.getPanDeviceConnectionState(device); 223 } catch (RemoteException e) { 224 Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); 225 return BluetoothProfile.STATE_DISCONNECTED; 226 } 227 } 228 if (mService == null) Log.w(TAG, "Proxy not attached to service"); 229 return BluetoothProfile.STATE_DISCONNECTED; 230 } 231 232 /** 233 * {@inheritDoc} 234 * @hide 235 */ 236 public boolean setPriority(BluetoothDevice device, int priority) { 237 // Priorities are not supported for PAN devices - since we don't 238 // auto connect. 239 return false; 240 } 241 242 /** 243 * {@inheritDoc} 244 * @hide 245 */ 246 public int getPriority(BluetoothDevice device) { 247 if (DBG) log("getPriority(" + device + ")"); 248 // Priorities are not supported for PAN devices - since we don't 249 // auto connect. 250 return BluetoothProfile.PRIORITY_ON; 251 } 252 253 public void setBluetoothTethering(boolean value) { 254 if (DBG) log("setBluetoothTethering(" + value + ")"); 255 try { 256 mService.setBluetoothTethering(value); 257 } catch (RemoteException e) { 258 Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); 259 } 260 } 261 262 public boolean isTetheringOn() { 263 if (DBG) log("isTetheringOn()"); 264 try { 265 return mService.isTetheringOn(); 266 } catch (RemoteException e) { 267 Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); 268 return false; 269 } 270 } 271 272 private boolean isEnabled() { 273 if (mAdapter.getState() == BluetoothAdapter.STATE_ON) return true; 274 return false; 275 } 276 277 private boolean isValidDevice(BluetoothDevice device) { 278 if (device == null) return false; 279 280 if (BluetoothAdapter.checkBluetoothAddress(device.getAddress())) return true; 281 return false; 282 } 283 284 private static void log(String msg) { 285 Log.d(TAG, msg); 286 } 287}