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
|
package android.bluetooth;
import java.lang.Thread;
import android.os.Message;
import android.os.Handler;
import android.util.Log;
/**
* Listen's for incoming RFCOMM connection for the headset / handsfree service.
*
* This class is planned for deletion, in favor of a generic Rfcomm class.
*
* @hide
*/
public class BluetoothAudioGateway {
private static final String TAG = "BT Audio Gateway";
private static final boolean DBG = false;
private int mNativeData;
static { classInitNative(); }
private BluetoothDevice mBluetooth;
/* in */
private int mHandsfreeAgRfcommChannel = -1;
private int mHeadsetAgRfcommChannel = -1;
/* out */
private String mConnectingHeadsetAddress;
private int mConnectingHeadsetRfcommChannel; /* -1 when not connected */
private int mConnectingHeadsetSocketFd;
private String mConnectingHandsfreeAddress;
private int mConnectingHandsfreeRfcommChannel; /* -1 when not connected */
private int mConnectingHandsfreeSocketFd;
private int mTimeoutRemainingMs; /* in/out */
public static final int DEFAULT_HF_AG_CHANNEL = 10;
public static final int DEFAULT_HS_AG_CHANNEL = 11;
public BluetoothAudioGateway(BluetoothDevice bluetooth) {
this(bluetooth, DEFAULT_HF_AG_CHANNEL, DEFAULT_HS_AG_CHANNEL);
}
public BluetoothAudioGateway(BluetoothDevice bluetooth,
int handsfreeAgRfcommChannel,
int headsetAgRfcommChannel) {
mBluetooth = bluetooth;
mHandsfreeAgRfcommChannel = handsfreeAgRfcommChannel;
mHeadsetAgRfcommChannel = headsetAgRfcommChannel;
initializeNativeDataNative();
}
private Thread mConnectThead;
private volatile boolean mInterrupted;
private static final int SELECT_WAIT_TIMEOUT = 1000;
private Handler mCallback;
public class IncomingConnectionInfo {
IncomingConnectionInfo(BluetoothDevice bluetooth, String address, int socketFd,
int rfcommChan) {
mBluetooth = bluetooth;
mAddress = address;
mSocketFd = socketFd;
mRfcommChan = rfcommChan;
}
public BluetoothDevice mBluetooth;
public String mAddress;
public int mSocketFd;
public int mRfcommChan;
}
public static final int MSG_INCOMING_HEADSET_CONNECTION = 100;
public static final int MSG_INCOMING_HANDSFREE_CONNECTION = 101;
public synchronized boolean start(Handler callback) {
if (mConnectThead == null) {
mCallback = callback;
mConnectThead = new Thread(TAG) {
public void run() {
if (DBG) log("Connect Thread starting");
while (!mInterrupted) {
//Log.i(TAG, "waiting for connect");
mConnectingHeadsetRfcommChannel = -1;
mConnectingHandsfreeRfcommChannel = -1;
if (waitForHandsfreeConnectNative(SELECT_WAIT_TIMEOUT) == false) {
if (mTimeoutRemainingMs > 0) {
try {
Log.i(TAG, "select thread timed out, but " +
mTimeoutRemainingMs + "ms of waiting remain.");
Thread.sleep(mTimeoutRemainingMs);
} catch (InterruptedException e) {
Log.i(TAG, "select thread was interrupted (2), exiting");
mInterrupted = true;
}
}
}
else {
Log.i(TAG, "connect notification!");
/* A device connected (most likely just one, but
it is possible for two separate devices, one
a headset and one a handsfree, to connect
simultaneously.
*/
if (mConnectingHeadsetRfcommChannel >= 0) {
Log.i(TAG, "Incoming connection from headset " +
mConnectingHeadsetAddress + " on channel " +
mConnectingHeadsetRfcommChannel);
Message msg = Message.obtain(mCallback);
msg.what = MSG_INCOMING_HEADSET_CONNECTION;
msg.obj =
new IncomingConnectionInfo(
mBluetooth,
mConnectingHeadsetAddress,
mConnectingHeadsetSocketFd,
mConnectingHeadsetRfcommChannel);
msg.sendToTarget();
}
if (mConnectingHandsfreeRfcommChannel >= 0) {
Log.i(TAG, "Incoming connection from handsfree " +
mConnectingHandsfreeAddress + " on channel " +
mConnectingHandsfreeRfcommChannel);
Message msg = Message.obtain();
msg.setTarget(mCallback);
msg.what = MSG_INCOMING_HANDSFREE_CONNECTION;
msg.obj =
new IncomingConnectionInfo(
mBluetooth,
mConnectingHandsfreeAddress,
mConnectingHandsfreeSocketFd,
mConnectingHandsfreeRfcommChannel);
msg.sendToTarget();
}
}
}
if (DBG) log("Connect Thread finished");
}
};
if (setUpListeningSocketsNative() == false) {
Log.e(TAG, "Could not set up listening socket, exiting");
return false;
}
mInterrupted = false;
mConnectThead.start();
}
return true;
}
public synchronized void stop() {
if (mConnectThead != null) {
if (DBG) log("stopping Connect Thread");
mInterrupted = true;
try {
mConnectThead.interrupt();
if (DBG) log("waiting for thread to terminate");
mConnectThead.join();
mConnectThead = null;
mCallback = null;
tearDownListeningSocketsNative();
} catch (InterruptedException e) {
Log.w(TAG, "Interrupted waiting for Connect Thread to join");
}
}
}
protected void finalize() throws Throwable {
try {
cleanupNativeDataNative();
} finally {
super.finalize();
}
}
private static native void classInitNative();
private native void initializeNativeDataNative();
private native void cleanupNativeDataNative();
private native boolean waitForHandsfreeConnectNative(int timeoutMs);
private native boolean setUpListeningSocketsNative();
private native void tearDownListeningSocketsNative();
private static void log(String msg) {
Log.d(TAG, msg);
}
}
|