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