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 package com.intel.bluetooth;
26
27 import java.io.IOException;
28
29 import javax.bluetooth.L2CAPConnection;
30 import javax.bluetooth.RemoteDevice;
31 import javax.bluetooth.ServiceRecord;
32
33
34
35
36
37 abstract class BluetoothL2CAPConnection implements L2CAPConnection, BluetoothConnectionAccess {
38
39 protected BluetoothStack bluetoothStack;
40
41 protected volatile long handle;
42
43 protected int securityOpt;
44
45 private RemoteDevice remoteDevice;
46
47 private boolean isClosed;
48
49 protected BluetoothL2CAPConnection(BluetoothStack bluetoothStack, long handle) {
50 this.bluetoothStack = bluetoothStack;
51 this.handle = handle;
52 this.isClosed = false;
53 }
54
55
56
57
58
59
60 public long getRemoteAddress() throws IOException {
61 if (isClosed) {
62 throw new IOException("Connection closed");
63 }
64 return bluetoothStack.l2RemoteAddress(handle);
65 }
66
67
68
69
70
71
72 public int getReceiveMTU() throws IOException {
73 if (isClosed) {
74 throw new IOException("Connection closed");
75 }
76 return bluetoothStack.l2GetReceiveMTU(handle);
77 }
78
79
80
81
82
83
84 public int getTransmitMTU() throws IOException {
85 if (isClosed) {
86 throw new IOException("Connection closed");
87 }
88 return bluetoothStack.l2GetTransmitMTU(handle);
89 }
90
91
92
93
94
95
96 public boolean ready() throws IOException {
97 if (isClosed) {
98 throw new IOException("Connection closed");
99 }
100 return bluetoothStack.l2Ready(handle);
101 }
102
103
104
105
106
107
108 public int receive(byte[] inBuf) throws IOException {
109 if (isClosed) {
110 throw new IOException("Connection closed");
111 }
112 if (inBuf == null) {
113 throw new NullPointerException("inBuf is null");
114 }
115 return bluetoothStack.l2Receive(handle, inBuf);
116 }
117
118
119
120
121
122
123 public void send(byte[] data) throws IOException {
124 if (isClosed) {
125 throw new IOException("Connection closed");
126 }
127 if (data == null) {
128 throw new NullPointerException("data is null");
129 }
130 bluetoothStack.l2Send(handle, data);
131 }
132
133 abstract void closeConnectionHandle(long handle) throws IOException;
134
135
136
137
138
139
140 public void close() throws IOException {
141 if (isClosed) {
142 return;
143 }
144
145 isClosed = true;
146 shutdown();
147 }
148
149
150
151
152
153
154 public void shutdown() throws IOException {
155 if (handle != 0) {
156 DebugLog.debug("closing L2CAP Connection", handle);
157
158 long synchronizedHandle;
159 synchronized (this) {
160 synchronizedHandle = handle;
161 handle = 0;
162 }
163 if (synchronizedHandle != 0) {
164 closeConnectionHandle(synchronizedHandle);
165 }
166 }
167 }
168
169 protected void finalize() {
170 try {
171 close();
172 } catch (IOException e) {
173 }
174 }
175
176
177
178
179
180
181 public boolean isClosed() {
182 return isClosed;
183 }
184
185
186
187
188
189
190 public void markAuthenticated() {
191 if (this.securityOpt == ServiceRecord.NOAUTHENTICATE_NOENCRYPT) {
192 this.securityOpt = ServiceRecord.AUTHENTICATE_NOENCRYPT;
193 }
194 }
195
196
197
198
199
200
201 public int getSecurityOpt() {
202 try {
203 this.securityOpt = bluetoothStack.l2GetSecurityOpt(this.handle, this.securityOpt);
204 } catch (IOException notChanged) {
205 }
206 return this.securityOpt;
207 }
208
209
210
211
212
213
214
215 public boolean encrypt(long address, boolean on) throws IOException {
216 if (isClosed) {
217 throw new IOException("L2CAP Connection is already closed");
218 }
219 boolean changed = bluetoothStack.l2Encrypt(address, this.handle, on);
220 if (changed) {
221 if (on) {
222 this.securityOpt = ServiceRecord.AUTHENTICATE_ENCRYPT;
223 } else {
224 this.securityOpt = ServiceRecord.AUTHENTICATE_NOENCRYPT;
225 }
226 }
227 return changed;
228 }
229
230
231
232
233
234
235 public RemoteDevice getRemoteDevice() {
236 return this.remoteDevice;
237 }
238
239
240
241
242
243
244 public void setRemoteDevice(RemoteDevice remoteDevice) {
245 this.remoteDevice = remoteDevice;
246 }
247
248
249
250
251
252
253 public BluetoothStack getBluetoothStack() {
254 return bluetoothStack;
255 }
256
257 }