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/BluetoothOutputStream.java | 57 ++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 framework/java/android/bluetooth/BluetoothOutputStream.java (limited to 'framework/java/android/bluetooth/BluetoothOutputStream.java') diff --git a/framework/java/android/bluetooth/BluetoothOutputStream.java b/framework/java/android/bluetooth/BluetoothOutputStream.java new file mode 100644 index 0000000000..32e6d17c82 --- /dev/null +++ b/framework/java/android/bluetooth/BluetoothOutputStream.java @@ -0,0 +1,57 @@ +/* + * 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.OutputStream; + +/** + * BluetoothOutputStream. + * + * Used to read from a Bluetooth socket. + * + * TODO: Implement bulk reads (instead of one byte at a time). + * @hide + */ +/*package*/ final class BluetoothOutputStream extends OutputStream { + private BluetoothSocket mSocket; + + /*package*/ BluetoothOutputStream(BluetoothSocket s) { + mSocket = s; + } + + /** + * Close this output stream and the socket associated with it. + */ + public void close() throws IOException { + mSocket.close(); + } + + /** + * Writes a single byte to this stream. Only the least significant byte of + * the integer {@code oneByte} is written to the stream. + * + * @param oneByte + * the byte to be written. + * @throws IOException + * if an error occurs while writing to this stream. + * @since Android 1.0 + */ + public void write(int oneByte) throws IOException { + mSocket.writeNative(oneByte); + } +} -- 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/BluetoothOutputStream.java | 34 ++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) (limited to 'framework/java/android/bluetooth/BluetoothOutputStream.java') diff --git a/framework/java/android/bluetooth/BluetoothOutputStream.java b/framework/java/android/bluetooth/BluetoothOutputStream.java index 32e6d17c82..7e2ead478a 100644 --- a/framework/java/android/bluetooth/BluetoothOutputStream.java +++ b/framework/java/android/bluetooth/BluetoothOutputStream.java @@ -24,7 +24,6 @@ import java.io.OutputStream; * * Used to read from a Bluetooth socket. * - * TODO: Implement bulk reads (instead of one byte at a time). * @hide */ /*package*/ final class BluetoothOutputStream extends OutputStream { @@ -52,6 +51,37 @@ import java.io.OutputStream; * @since Android 1.0 */ public void write(int oneByte) throws IOException { - mSocket.writeNative(oneByte); + byte b[] = new byte[1]; + b[0] = (byte)oneByte; + mSocket.writeNative(b, 0, 1); + } + + /** + * Writes {@code count} bytes from the byte array {@code buffer} starting + * at position {@code offset} to this stream. + * + * @param b + * the buffer to be written. + * @param offset + * the start position in {@code buffer} from where to get bytes. + * @param count + * the number of bytes from {@code buffer} to write to this + * stream. + * @throws IOException + * if an error occurs while writing to this stream. + * @throws IndexOutOfBoundsException + * if {@code offset < 0} or {@code count < 0}, or if + * {@code offset + count} is bigger than the length of + * {@code buffer}. + * @since Android 1.0 + */ + public void write(byte[] b, int offset, int count) throws IOException { + if (b == null) { + throw new NullPointerException("buffer is null"); + } + if ((offset | count) < 0 || count > b.length - offset) { + throw new IndexOutOfBoundsException("invalid offset or length"); + } + mSocket.writeNative(b, offset, count); } } -- 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/BluetoothOutputStream.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'framework/java/android/bluetooth/BluetoothOutputStream.java') diff --git a/framework/java/android/bluetooth/BluetoothOutputStream.java b/framework/java/android/bluetooth/BluetoothOutputStream.java index 7e2ead478a..62242a2672 100644 --- a/framework/java/android/bluetooth/BluetoothOutputStream.java +++ b/framework/java/android/bluetooth/BluetoothOutputStream.java @@ -53,7 +53,7 @@ import java.io.OutputStream; public void write(int oneByte) throws IOException { byte b[] = new byte[1]; b[0] = (byte)oneByte; - mSocket.writeNative(b, 0, 1); + mSocket.write(b, 0, 1); } /** @@ -82,6 +82,6 @@ import java.io.OutputStream; if ((offset | count) < 0 || count > b.length - offset) { throw new IndexOutOfBoundsException("invalid offset or length"); } - mSocket.writeNative(b, offset, count); + mSocket.write(b, offset, count); } } -- cgit v1.2.3 From f6fcd9b26b8a1df94a02a0ff27f4f0fa3fcf7339 Mon Sep 17 00:00:00 2001 From: zzy Date: Tue, 16 Apr 2013 17:17:37 -0700 Subject: Added flush() for bluetooth output stream Bug 8498784 Zebra QL420 Plus Bluetooth printer fails on Android 4.2.2 --- framework/java/android/bluetooth/BluetoothOutputStream.java | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'framework/java/android/bluetooth/BluetoothOutputStream.java') diff --git a/framework/java/android/bluetooth/BluetoothOutputStream.java b/framework/java/android/bluetooth/BluetoothOutputStream.java index 62242a2672..117dd47c1a 100644 --- a/framework/java/android/bluetooth/BluetoothOutputStream.java +++ b/framework/java/android/bluetooth/BluetoothOutputStream.java @@ -84,4 +84,15 @@ import java.io.OutputStream; } mSocket.write(b, offset, count); } + /** + * Wait until the data in sending queue is emptied. A polling version + * for flush implementation. Use it to ensure the writing data afterwards will + * be packed in the new RFCOMM frame. + * @throws IOException + * if an i/o error occurs. + * @since Android 4.2.3 + */ + public void flush() throws IOException { + mSocket.flush(); + } } -- 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/BluetoothOutputStream.java | 34 +++++++++------------- 1 file changed, 13 insertions(+), 21 deletions(-) (limited to 'framework/java/android/bluetooth/BluetoothOutputStream.java') diff --git a/framework/java/android/bluetooth/BluetoothOutputStream.java b/framework/java/android/bluetooth/BluetoothOutputStream.java index 117dd47c1a..cecd3dbfff 100644 --- a/framework/java/android/bluetooth/BluetoothOutputStream.java +++ b/framework/java/android/bluetooth/BluetoothOutputStream.java @@ -44,15 +44,13 @@ import java.io.OutputStream; * Writes a single byte to this stream. Only the least significant byte of * the integer {@code oneByte} is written to the stream. * - * @param oneByte - * the byte to be written. - * @throws IOException - * if an error occurs while writing to this stream. + * @param oneByte the byte to be written. + * @throws IOException if an error occurs while writing to this stream. * @since Android 1.0 */ public void write(int oneByte) throws IOException { byte b[] = new byte[1]; - b[0] = (byte)oneByte; + b[0] = (byte) oneByte; mSocket.write(b, 0, 1); } @@ -60,19 +58,12 @@ import java.io.OutputStream; * Writes {@code count} bytes from the byte array {@code buffer} starting * at position {@code offset} to this stream. * - * @param b - * the buffer to be written. - * @param offset - * the start position in {@code buffer} from where to get bytes. - * @param count - * the number of bytes from {@code buffer} to write to this - * stream. - * @throws IOException - * if an error occurs while writing to this stream. - * @throws IndexOutOfBoundsException - * if {@code offset < 0} or {@code count < 0}, or if - * {@code offset + count} is bigger than the length of - * {@code buffer}. + * @param b the buffer to be written. + * @param offset the start position in {@code buffer} from where to get bytes. + * @param count the number of bytes from {@code buffer} to write to this stream. + * @throws IOException if an error occurs while writing to this stream. + * @throws IndexOutOfBoundsException if {@code offset < 0} or {@code count < 0}, or if {@code + * offset + count} is bigger than the length of {@code buffer}. * @since Android 1.0 */ public void write(byte[] b, int offset, int count) throws IOException { @@ -84,15 +75,16 @@ import java.io.OutputStream; } mSocket.write(b, offset, count); } + /** * Wait until the data in sending queue is emptied. A polling version * for flush implementation. Use it to ensure the writing data afterwards will * be packed in the new RFCOMM frame. - * @throws IOException - * if an i/o error occurs. + * + * @throws IOException if an i/o error occurs. * @since Android 4.2.3 */ - public void flush() throws IOException { + public void flush() throws IOException { mSocket.flush(); } } -- 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/BluetoothOutputStream.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'framework/java/android/bluetooth/BluetoothOutputStream.java') diff --git a/framework/java/android/bluetooth/BluetoothOutputStream.java b/framework/java/android/bluetooth/BluetoothOutputStream.java index cecd3dbfff..dfec4e102f 100644 --- a/framework/java/android/bluetooth/BluetoothOutputStream.java +++ b/framework/java/android/bluetooth/BluetoothOutputStream.java @@ -49,7 +49,7 @@ import java.io.OutputStream; * @since Android 1.0 */ public void write(int oneByte) throws IOException { - byte b[] = new byte[1]; + byte[] b = new byte[1]; b[0] = (byte) oneByte; mSocket.write(b, 0, 1); } -- cgit v1.2.3 From 0388ad1f72c70ca96cabc8b1a4a107877752b187 Mon Sep 17 00:00:00 2001 From: Chris Wailes Date: Wed, 21 Aug 2019 17:22:54 -0700 Subject: Remove a misleading "flush" function. This patch removes LocalSocketImpl.flush(). In practice this function was simply a wrapper around `Thread.sleep(10)`. All direct calls to this function have been removed. The `flush()` function is still called on several objects that wrap a SocketOutputStream. This will make booting a device 20ms faster than it currently is. Bug: 139192244 Test: Build -> flash -> boot -> launch app Change-Id: I0a96f4bc72461670370f61e847349f32af5ac774 --- framework/java/android/bluetooth/BluetoothOutputStream.java | 12 ------------ 1 file changed, 12 deletions(-) (limited to 'framework/java/android/bluetooth/BluetoothOutputStream.java') diff --git a/framework/java/android/bluetooth/BluetoothOutputStream.java b/framework/java/android/bluetooth/BluetoothOutputStream.java index dfec4e102f..a0aa2dee9d 100644 --- a/framework/java/android/bluetooth/BluetoothOutputStream.java +++ b/framework/java/android/bluetooth/BluetoothOutputStream.java @@ -75,16 +75,4 @@ import java.io.OutputStream; } mSocket.write(b, offset, count); } - - /** - * Wait until the data in sending queue is emptied. A polling version - * for flush implementation. Use it to ensure the writing data afterwards will - * be packed in the new RFCOMM frame. - * - * @throws IOException if an i/o error occurs. - * @since Android 4.2.3 - */ - public void flush() throws IOException { - mSocket.flush(); - } } -- cgit v1.2.3 From 3d30e625e338d452c2a9e91dc2ad477e8500e5eb Mon Sep 17 00:00:00 2001 From: Ian Kasprzak Date: Sat, 31 Aug 2019 21:28:50 +0000 Subject: Revert "Remove a misleading "flush" function." This reverts commit 0388ad1f72c70ca96cabc8b1a4a107877752b187. Reason for revert: Driodcop: aosp-master test-mapping showing multiple failures (b/140336855). Change-Id: If44e273dd111802db8b44db1e5a67a4628c72e3c --- framework/java/android/bluetooth/BluetoothOutputStream.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'framework/java/android/bluetooth/BluetoothOutputStream.java') diff --git a/framework/java/android/bluetooth/BluetoothOutputStream.java b/framework/java/android/bluetooth/BluetoothOutputStream.java index a0aa2dee9d..dfec4e102f 100644 --- a/framework/java/android/bluetooth/BluetoothOutputStream.java +++ b/framework/java/android/bluetooth/BluetoothOutputStream.java @@ -75,4 +75,16 @@ import java.io.OutputStream; } mSocket.write(b, offset, count); } + + /** + * Wait until the data in sending queue is emptied. A polling version + * for flush implementation. Use it to ensure the writing data afterwards will + * be packed in the new RFCOMM frame. + * + * @throws IOException if an i/o error occurs. + * @since Android 4.2.3 + */ + public void flush() throws IOException { + mSocket.flush(); + } } -- cgit v1.2.3 From 4fd500310f8738adfb2d7dd6e32b576ab7b00ab8 Mon Sep 17 00:00:00 2001 From: Christian Wailes Date: Wed, 4 Sep 2019 23:35:54 +0000 Subject: Revert "Revert "Remove a misleading "flush" function."" This reverts commit 3d30e625e338d452c2a9e91dc2ad477e8500e5eb. Reason for revert: Fixed the test broken by the original commit Bug: 139192244 Bug: 140336855 Test: m -> flash -> boot Test: atest CtsJvmtiAttachingHostTestCases Change-Id: I4c67ad8709652c4710ef24564e0240f74f817f8c --- framework/java/android/bluetooth/BluetoothOutputStream.java | 12 ------------ 1 file changed, 12 deletions(-) (limited to 'framework/java/android/bluetooth/BluetoothOutputStream.java') diff --git a/framework/java/android/bluetooth/BluetoothOutputStream.java b/framework/java/android/bluetooth/BluetoothOutputStream.java index dfec4e102f..a0aa2dee9d 100644 --- a/framework/java/android/bluetooth/BluetoothOutputStream.java +++ b/framework/java/android/bluetooth/BluetoothOutputStream.java @@ -75,16 +75,4 @@ import java.io.OutputStream; } mSocket.write(b, offset, count); } - - /** - * Wait until the data in sending queue is emptied. A polling version - * for flush implementation. Use it to ensure the writing data afterwards will - * be packed in the new RFCOMM frame. - * - * @throws IOException if an i/o error occurs. - * @since Android 4.2.3 - */ - public void flush() throws IOException { - mSocket.flush(); - } } -- 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/BluetoothOutputStream.java | 3 +++ 1 file changed, 3 insertions(+) (limited to 'framework/java/android/bluetooth/BluetoothOutputStream.java') diff --git a/framework/java/android/bluetooth/BluetoothOutputStream.java b/framework/java/android/bluetooth/BluetoothOutputStream.java index a0aa2dee9d..ac2b3edb0e 100644 --- a/framework/java/android/bluetooth/BluetoothOutputStream.java +++ b/framework/java/android/bluetooth/BluetoothOutputStream.java @@ -16,6 +16,8 @@ package android.bluetooth; +import android.annotation.SuppressLint; + import java.io.IOException; import java.io.OutputStream; @@ -26,6 +28,7 @@ import java.io.OutputStream; * * @hide */ +@SuppressLint("AndroidFrameworkBluetoothPermission") /*package*/ final class BluetoothOutputStream extends OutputStream { private BluetoothSocket mSocket; -- cgit v1.2.3