Skip to content

Commit c790096

Browse files
author
Nick Pelly
committed
New BluetoothSocket API.
Modeled on blocking java.net.Socket and java.net.ServerSocket library. Public interface is: public final class BluetoothSocket implements Closeable { public static BluetoothSocket createRfcommSocket(String address, int port) throws IOException; public static BluetoothSocket createInsecureRfcommSocket(String address, int port) throws IOException; public void connect() throws IOException; public void close() throws IOException; public String getAddress(); public InputStream getInputStream() throws IOException; public OutputStream getOutputStream() throws IOException; } public final class BluetoothServerSocket implements Closeable { public static BluetoothServerSocket listenUsingRfcommOn(int port) throws IOException; public static BluetoothServerSocket listenUsingUnsecureRfcommOn(int port) throws IOException; public BluetoothSocket accept() throws IOException; public BluetoothSocket accept(int timeout) throws IOException; public void close() throws IOException; }
1 parent 9b50ade commit c790096

11 files changed

+750
-1301
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright (C) 2009 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+
17+
package android.bluetooth;
18+
19+
import java.io.IOException;
20+
import java.io.InputStream;
21+
22+
/**
23+
* BluetoothInputStream.
24+
*
25+
* Used to write to a Bluetooth socket.
26+
*
27+
* TODO: Implement bulk writes (instead of one byte at a time).
28+
* @hide
29+
*/
30+
/*package*/ final class BluetoothInputStream extends InputStream {
31+
private BluetoothSocket mSocket;
32+
33+
/*package*/ BluetoothInputStream(BluetoothSocket s) {
34+
mSocket = s;
35+
}
36+
37+
/**
38+
* Return number of bytes available before this stream will block.
39+
*/
40+
public int available() throws IOException {
41+
return mSocket.availableNative();
42+
}
43+
44+
public void close() throws IOException {
45+
mSocket.close();
46+
}
47+
48+
/**
49+
* Reads a single byte from this stream and returns it as an integer in the
50+
* range from 0 to 255. Returns -1 if the end of the stream has been
51+
* reached. Blocks until one byte has been read, the end of the source
52+
* stream is detected or an exception is thrown.
53+
*
54+
* @return the byte read or -1 if the end of stream has been reached.
55+
* @throws IOException
56+
* if the stream is closed or another IOException occurs.
57+
* @since Android 1.0
58+
*/
59+
public int read() throws IOException {
60+
return mSocket.readNative();
61+
}
62+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright (C) 2009 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+
17+
package android.bluetooth;
18+
19+
import java.io.IOException;
20+
import java.io.OutputStream;
21+
22+
/**
23+
* BluetoothOutputStream.
24+
*
25+
* Used to read from a Bluetooth socket.
26+
*
27+
* TODO: Implement bulk reads (instead of one byte at a time).
28+
* @hide
29+
*/
30+
/*package*/ final class BluetoothOutputStream extends OutputStream {
31+
private BluetoothSocket mSocket;
32+
33+
/*package*/ BluetoothOutputStream(BluetoothSocket s) {
34+
mSocket = s;
35+
}
36+
37+
/**
38+
* Close this output stream and the socket associated with it.
39+
*/
40+
public void close() throws IOException {
41+
mSocket.close();
42+
}
43+
44+
/**
45+
* Writes a single byte to this stream. Only the least significant byte of
46+
* the integer {@code oneByte} is written to the stream.
47+
*
48+
* @param oneByte
49+
* the byte to be written.
50+
* @throws IOException
51+
* if an error occurs while writing to this stream.
52+
* @since Android 1.0
53+
*/
54+
public void write(int oneByte) throws IOException {
55+
mSocket.writeNative(oneByte);
56+
}
57+
}
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
/*
2+
* Copyright (C) 2009 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+
17+
package android.bluetooth;
18+
19+
import java.io.Closeable;
20+
import java.io.IOException;
21+
22+
/**
23+
* Server (listening) Bluetooth Socket.
24+
*
25+
* Currently only supports RFCOMM sockets.
26+
*
27+
* RFCOMM is a connection orientated, streaming transport over Bluetooth. It is
28+
* also known as the Serial Port Profile (SPP).
29+
*
30+
* TODO: Consider implementing SCO and L2CAP sockets.
31+
* TODO: Clean up javadoc grammer and formatting.
32+
* TODO: Remove @hide
33+
* @hide
34+
*/
35+
public final class BluetoothServerSocket implements Closeable {
36+
private final BluetoothSocket mSocket;
37+
38+
/**
39+
* Construct a listening, secure RFCOMM server socket.
40+
* The remote device connecting to this socket will be authenticated and
41+
* communication on this socket will be encrypted.
42+
* Call #accept to retrieve connections to this socket.
43+
* @return An RFCOMM BluetoothServerSocket
44+
* @throws IOException On error, for example Bluetooth not available, or
45+
* insufficient permissions.
46+
*/
47+
public static BluetoothServerSocket listenUsingRfcommOn(int port) throws IOException {
48+
BluetoothServerSocket socket = new BluetoothServerSocket(true, true);
49+
try {
50+
socket.mSocket.bindListenNative(port);
51+
} catch (IOException e) {
52+
try {
53+
socket.close();
54+
} catch (IOException e2) { }
55+
throw e;
56+
}
57+
return socket;
58+
}
59+
60+
/**
61+
* Construct an unencrypted, unauthenticated, RFCOMM server socket.
62+
* Call #accept to retrieve connections to this socket.
63+
* @return An RFCOMM BluetoothServerSocket
64+
* @throws IOException On error, for example Bluetooth not available, or
65+
* insufficient permissions.
66+
*/
67+
public static BluetoothServerSocket listenUsingInsecureRfcommOn(int port) throws IOException {
68+
BluetoothServerSocket socket = new BluetoothServerSocket(false, false);
69+
try {
70+
socket.mSocket.bindListenNative(port);
71+
} catch (IOException e) {
72+
try {
73+
socket.close();
74+
} catch (IOException e2) { }
75+
throw e;
76+
}
77+
return socket;
78+
}
79+
80+
/**
81+
* Construct a socket for incoming connections.
82+
* @param auth Require the remote device to be authenticated
83+
* @param encrypt Require the connection to be encrypted
84+
* @throws IOException On error, for example Bluetooth not available, or
85+
* insufficient priveleges
86+
*/
87+
private BluetoothServerSocket(boolean auth, boolean encrypt) throws IOException {
88+
mSocket = new BluetoothSocket(-1, auth, encrypt, null, -1);
89+
}
90+
91+
/**
92+
* Block until a connection is established.
93+
* Returns a connected #BluetoothSocket. This server socket can be reused
94+
* for subsequent incoming connections by calling #accept repeatedly.
95+
* #close can be used to abort this call from another thread.
96+
* @return A connected #BluetoothSocket
97+
* @throws IOException On error, for example this call was aborted
98+
*/
99+
public BluetoothSocket accept() throws IOException {
100+
return accept(-1);
101+
}
102+
103+
/**
104+
* Block until a connection is established, with timeout.
105+
* Returns a connected #BluetoothSocket. This server socket can be reused
106+
* for subsequent incoming connections by calling #accept repeatedly.
107+
* #close can be used to abort this call from another thread.
108+
* @return A connected #BluetoothSocket
109+
* @throws IOException On error, for example this call was aborted, or
110+
* timeout
111+
*/
112+
public BluetoothSocket accept(int timeout) throws IOException {
113+
return mSocket.acceptNative(timeout);
114+
}
115+
116+
/**
117+
* Closes this socket.
118+
* This will cause other blocking calls on this socket to immediately
119+
* throw an IOException.
120+
*/
121+
public void close() throws IOException {
122+
mSocket.closeNative();
123+
}
124+
}

0 commit comments

Comments
 (0)