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