From 53f441bf8d89667e099302d506c6f07ed1201db1 Mon Sep 17 00:00:00 2001 From: Nick Pelly Date: Tue, 26 May 2009 19:13:43 -0700 Subject: 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; } --- .../android/bluetooth/BluetoothInputStream.java | 62 ++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 framework/java/android/bluetooth/BluetoothInputStream.java (limited to 'framework/java/android/bluetooth/BluetoothInputStream.java') diff --git a/framework/java/android/bluetooth/BluetoothInputStream.java b/framework/java/android/bluetooth/BluetoothInputStream.java new file mode 100644 index 0000000000..ceae70c586 --- /dev/null +++ b/framework/java/android/bluetooth/BluetoothInputStream.java @@ -0,0 +1,62 @@ +/* + * Copyright (C) 2009 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 java.io.IOException; +import java.io.InputStream; + +/** + * BluetoothInputStream. + * + * Used to write to a Bluetooth socket. + * + * TODO: Implement bulk writes (instead of one byte at a time). + * @hide + */ +/*package*/ final class BluetoothInputStream extends InputStream { + private BluetoothSocket mSocket; + + /*package*/ BluetoothInputStream(BluetoothSocket s) { + mSocket = s; + } + + /** + * Return number of bytes available before this stream will block. + */ + public int available() throws IOException { + return mSocket.availableNative(); + } + + public void close() throws IOException { + mSocket.close(); + } + + /** + * Reads a single byte from this stream and returns it as an integer in the + * range from 0 to 255. Returns -1 if the end of the stream has been + * reached. Blocks until one byte has been read, the end of the source + * stream is detected or an exception is thrown. + * + * @return the byte read or -1 if the end of stream has been reached. + * @throws IOException + * if the stream is closed or another IOException occurs. + * @since Android 1.0 + */ + public int read() throws IOException { + return mSocket.readNative(); + } +} -- cgit v1.2.3 From 731bcb12e10d3527cf6c15d5e0da6161482787c0 Mon Sep 17 00:00:00 2001 From: Nick Pelly Date: Mon, 1 Jun 2009 19:09:37 -0700 Subject: Implement bulk read and writes for Bluetooth sockets. Before: 0.1 kB/s After: 100 kB/s (in my java BT speed test app) --- .../android/bluetooth/BluetoothInputStream.java | 42 ++++++++++++++++++++-- 1 file changed, 39 insertions(+), 3 deletions(-) (limited to 'framework/java/android/bluetooth/BluetoothInputStream.java') diff --git a/framework/java/android/bluetooth/BluetoothInputStream.java b/framework/java/android/bluetooth/BluetoothInputStream.java index ceae70c586..e6f501c9b6 100644 --- a/framework/java/android/bluetooth/BluetoothInputStream.java +++ b/framework/java/android/bluetooth/BluetoothInputStream.java @@ -24,7 +24,6 @@ import java.io.InputStream; * * Used to write to a Bluetooth socket. * - * TODO: Implement bulk writes (instead of one byte at a time). * @hide */ /*package*/ final class BluetoothInputStream extends InputStream { @@ -54,9 +53,46 @@ import java.io.InputStream; * @return the byte read or -1 if the end of stream has been reached. * @throws IOException * if the stream is closed or another IOException occurs. - * @since Android 1.0 + * @since Android 1.5 */ public int read() throws IOException { - return mSocket.readNative(); + byte b[] = new byte[1]; + int ret = mSocket.readNative(b, 0, 1); + if (ret == 1) { + return (int)b[0]; + } else { + return -1; + } + } + + /** + * Reads at most {@code length} bytes from this stream and stores them in + * the byte array {@code b} starting at {@code offset}. + * + * @param b + * the byte array in which to store the bytes read. + * @param offset + * the initial position in {@code buffer} to store the bytes + * read from this stream. + * @param length + * the maximum number of bytes to store in {@code b}. + * @return the number of bytes actually read or -1 if the end of the stream + * has been reached. + * @throws IndexOutOfBoundsException + * if {@code offset < 0} or {@code length < 0}, or if + * {@code offset + length} is greater than the length of + * {@code b}. + * @throws IOException + * if the stream is closed or another IOException occurs. + * @since Android 1.5 + */ + public int read(byte[] b, int offset, int length) throws IOException { + if (b == null) { + throw new NullPointerException("byte array is null"); + } + if ((offset | length) < 0 || length > b.length - offset) { + throw new ArrayIndexOutOfBoundsException("invalid offset or length"); + } + return mSocket.readNative(b, offset, length); } } -- cgit v1.2.3 From c232d9a813a65d22787b37c0b866759a9020de80 Mon Sep 17 00:00:00 2001 From: Nick Pelly Date: Thu, 18 Jun 2009 16:23:15 -0700 Subject: Fix bug in BluetoothInputStream.read(). InputStream.read() must return values in range [0, 255]. But the previous code would sign extend when casting to int so return [-128, 127]. Bitwise AND with 0xff to remove sign extension. --- framework/java/android/bluetooth/BluetoothInputStream.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'framework/java/android/bluetooth/BluetoothInputStream.java') diff --git a/framework/java/android/bluetooth/BluetoothInputStream.java b/framework/java/android/bluetooth/BluetoothInputStream.java index e6f501c9b6..c060f3263e 100644 --- a/framework/java/android/bluetooth/BluetoothInputStream.java +++ b/framework/java/android/bluetooth/BluetoothInputStream.java @@ -59,7 +59,7 @@ import java.io.InputStream; byte b[] = new byte[1]; int ret = mSocket.readNative(b, 0, 1); if (ret == 1) { - return (int)b[0]; + return (int)b[0] & 0xff; } else { return -1; } -- cgit v1.2.3 From 4cd2cd92746e80798c79161791a4042251ed356a Mon Sep 17 00:00:00 2001 From: Nick Pelly Date: Wed, 2 Sep 2009 11:51:35 -0700 Subject: Immediately destroy BluetoothSocket's on close(). Unfortunatley, shutdown() on the underlying fd does not actually stop a listening socket from listening. You need to call close() on the fd to do this. There is no way around it. So this means the Java BluetoothSocket code has to call destroyNative() during BluetoothSocket.close(). Since native methods cannot be called after destroyNative(), add a ReadWrite lock and mClosed field to protect access to native methods. This fixes the "resource busy" error when Bluetooth OPP and Bluetooth PBAP tried to resume listening after turning BT off and then on. --- framework/java/android/bluetooth/BluetoothInputStream.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'framework/java/android/bluetooth/BluetoothInputStream.java') diff --git a/framework/java/android/bluetooth/BluetoothInputStream.java b/framework/java/android/bluetooth/BluetoothInputStream.java index c060f3263e..03af95337c 100644 --- a/framework/java/android/bluetooth/BluetoothInputStream.java +++ b/framework/java/android/bluetooth/BluetoothInputStream.java @@ -37,7 +37,7 @@ import java.io.InputStream; * Return number of bytes available before this stream will block. */ public int available() throws IOException { - return mSocket.availableNative(); + return mSocket.available(); } public void close() throws IOException { @@ -57,7 +57,7 @@ import java.io.InputStream; */ public int read() throws IOException { byte b[] = new byte[1]; - int ret = mSocket.readNative(b, 0, 1); + int ret = mSocket.read(b, 0, 1); if (ret == 1) { return (int)b[0] & 0xff; } else { @@ -93,6 +93,6 @@ import java.io.InputStream; if ((offset | length) < 0 || length > b.length - offset) { throw new ArrayIndexOutOfBoundsException("invalid offset or length"); } - return mSocket.readNative(b, offset, length); + return mSocket.read(b, offset, length); } } -- cgit v1.2.3 From 910201beb0bde1dcf6b33e4ec5d1eb60042419d8 Mon Sep 17 00:00:00 2001 From: Jack He Date: Tue, 22 Aug 2017 16:06:54 -0700 Subject: Fix checkstyle errors (1/2) * Automatic style corrections through IDE Bug: 63596319 Test: make checkbuild, no manual changes, no functional changes Change-Id: I2397d55abc34c9b7a9b748bec6137778df3421a7 --- .../android/bluetooth/BluetoothInputStream.java | 28 ++++++++-------------- 1 file changed, 10 insertions(+), 18 deletions(-) (limited to 'framework/java/android/bluetooth/BluetoothInputStream.java') diff --git a/framework/java/android/bluetooth/BluetoothInputStream.java b/framework/java/android/bluetooth/BluetoothInputStream.java index 03af95337c..062e4de0a7 100644 --- a/framework/java/android/bluetooth/BluetoothInputStream.java +++ b/framework/java/android/bluetooth/BluetoothInputStream.java @@ -51,15 +51,14 @@ import java.io.InputStream; * stream is detected or an exception is thrown. * * @return the byte read or -1 if the end of stream has been reached. - * @throws IOException - * if the stream is closed or another IOException occurs. + * @throws IOException if the stream is closed or another IOException occurs. * @since Android 1.5 */ public int read() throws IOException { byte b[] = new byte[1]; int ret = mSocket.read(b, 0, 1); if (ret == 1) { - return (int)b[0] & 0xff; + return (int) b[0] & 0xff; } else { return -1; } @@ -69,21 +68,14 @@ import java.io.InputStream; * Reads at most {@code length} bytes from this stream and stores them in * the byte array {@code b} starting at {@code offset}. * - * @param b - * the byte array in which to store the bytes read. - * @param offset - * the initial position in {@code buffer} to store the bytes - * read from this stream. - * @param length - * the maximum number of bytes to store in {@code b}. - * @return the number of bytes actually read or -1 if the end of the stream - * has been reached. - * @throws IndexOutOfBoundsException - * if {@code offset < 0} or {@code length < 0}, or if - * {@code offset + length} is greater than the length of - * {@code b}. - * @throws IOException - * if the stream is closed or another IOException occurs. + * @param b the byte array in which to store the bytes read. + * @param offset the initial position in {@code buffer} to store the bytes read from this + * stream. + * @param length the maximum number of bytes to store in {@code b}. + * @return the number of bytes actually read or -1 if the end of the stream has been reached. + * @throws IndexOutOfBoundsException if {@code offset < 0} or {@code length < 0}, or if {@code + * offset + length} is greater than the length of {@code b}. + * @throws IOException if the stream is closed or another IOException occurs. * @since Android 1.5 */ public int read(byte[] b, int offset, int length) throws IOException { -- cgit v1.2.3 From 9e045d26d0128826b40520f523307d8d16473779 Mon Sep 17 00:00:00 2001 From: Jack He Date: Tue, 22 Aug 2017 21:21:23 -0700 Subject: Fix checkstyle errors (2/2) * Manual style corrections with IDE assistance * Variable name refactors are done through IDE * Corrected general style errors such as: - "final private var" -> "private final var" - "&&", "+", "||" should not be at the end of line - Non-static private variable should be like "mVar" - Private static variable should be like "sVar" - Code file should always end with newline - Inherited methods should be annotated with @Override and no @hide tags - Public methods should always have a JavaDoc entry - "int[] array" is preferred over "int array[]" - private methods should be accessed without "this." when there is no name collisions. - "boolean ? true : false" -> boolean - "boolean ? false : true" -> !boolean - "boolean == true" OR "boolean != false" -> boolean - "boolean != true" OR "boolean == false" -> !boolean Bug: 63596319 Test: make checkbuild, no functional changes Change-Id: Iabdc2be912a32dd63a53213d175cf1bfef268ccd --- framework/java/android/bluetooth/BluetoothInputStream.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'framework/java/android/bluetooth/BluetoothInputStream.java') diff --git a/framework/java/android/bluetooth/BluetoothInputStream.java b/framework/java/android/bluetooth/BluetoothInputStream.java index 062e4de0a7..8eb79b248d 100644 --- a/framework/java/android/bluetooth/BluetoothInputStream.java +++ b/framework/java/android/bluetooth/BluetoothInputStream.java @@ -55,7 +55,7 @@ import java.io.InputStream; * @since Android 1.5 */ public int read() throws IOException { - byte b[] = new byte[1]; + byte[] b = new byte[1]; int ret = mSocket.read(b, 0, 1); if (ret == 1) { return (int) b[0] & 0xff; -- cgit v1.2.3 From 5ba8bfca7e9adf5c6d8ee8180aebad6f04037d6c Mon Sep 17 00:00:00 2001 From: Jeff Sharkey Date: Fri, 16 Apr 2021 09:53:23 -0600 Subject: More Bluetooth API annotation updates. This change adds a "BluetoothPermissionChecker" that ensures that all Bluetooth permission annotations are consistent. In addition, it verifies that all Bluetooth public APIs have been audited to be permission protected where relevant. We've currently standardized on saying that APIs that return device or Bluetooth state information (without sharing details about any particular remote Bluetooth device) do not need to be permission protected. This change is only annotations and has no behavior changes. Bug: 183626724 Test: ./build/soong/soong_ui.bash --make-mode Bluetooth RUN_ERROR_PRONE=true Change-Id: Ie80b15b058359bf1e9a6ee881b89cb3e5b584ca1 --- framework/java/android/bluetooth/BluetoothInputStream.java | 3 +++ 1 file changed, 3 insertions(+) (limited to 'framework/java/android/bluetooth/BluetoothInputStream.java') diff --git a/framework/java/android/bluetooth/BluetoothInputStream.java b/framework/java/android/bluetooth/BluetoothInputStream.java index 8eb79b248d..95f9229f04 100644 --- a/framework/java/android/bluetooth/BluetoothInputStream.java +++ b/framework/java/android/bluetooth/BluetoothInputStream.java @@ -16,6 +16,8 @@ package android.bluetooth; +import android.annotation.SuppressLint; + import java.io.IOException; import java.io.InputStream; @@ -26,6 +28,7 @@ import java.io.InputStream; * * @hide */ +@SuppressLint("AndroidFrameworkBluetoothPermission") /*package*/ final class BluetoothInputStream extends InputStream { private BluetoothSocket mSocket; -- cgit v1.2.3