diff options
author | Nick Pelly <npelly@google.com> | 2009-06-01 19:09:37 -0700 |
---|---|---|
committer | Nick Pelly <npelly@google.com> | 2009-06-02 12:35:48 -0700 |
commit | 47e82dee6b18c33fab8c2cdf4f68b20d3663079e (patch) | |
tree | 090df26f4dd1e404b43b13cda9f7663626706c82 /core/jni/android_bluetooth_BluetoothSocket.cpp | |
parent | 4599184a1c1c3f54b396c49b5728e4031e559e18 (diff) | |
download | frameworks_base-47e82dee6b18c33fab8c2cdf4f68b20d3663079e.zip frameworks_base-47e82dee6b18c33fab8c2cdf4f68b20d3663079e.tar.gz frameworks_base-47e82dee6b18c33fab8c2cdf4f68b20d3663079e.tar.bz2 |
Implement bulk read and writes for Bluetooth sockets.
Before: 0.1 kB/s
After: 100 kB/s
(in my java BT speed test app)
Diffstat (limited to 'core/jni/android_bluetooth_BluetoothSocket.cpp')
-rw-r--r-- | core/jni/android_bluetooth_BluetoothSocket.cpp | 50 |
1 files changed, 39 insertions, 11 deletions
diff --git a/core/jni/android_bluetooth_BluetoothSocket.cpp b/core/jni/android_bluetooth_BluetoothSocket.cpp index dc4c1d4..e9c04a5 100644 --- a/core/jni/android_bluetooth_BluetoothSocket.cpp +++ b/core/jni/android_bluetooth_BluetoothSocket.cpp @@ -227,44 +227,72 @@ static jint availableNative(JNIEnv *env, jobject obj) { return -1; } -static jint readNative(JNIEnv *env, jobject obj) { +/** jb must not be null. offset and offset+length must be within array */ +static jint readNative(JNIEnv *env, jobject obj, jbyteArray jb, jint offset, + jint length) { #ifdef HAVE_BLUETOOTH LOGV(__FUNCTION__); - char buf; + int ret; + jbyte *b; struct asocket *s = get_socketData(env, obj); if (!s) return -1; - if (asocket_read(s, &buf, 1, -1) < 0) { + b = env->GetByteArrayElements(jb, NULL); + if (b == NULL) { + jniThrowIOException(env, EINVAL); + return -1; + } + + ret = asocket_read(s, &b[offset], length, -1); + if (ret < 0) { jniThrowIOException(env, errno); + env->ReleaseByteArrayElements(jb, b, JNI_ABORT); return -1; } - return (jint)buf; + env->ReleaseByteArrayElements(jb, b, 0); + return (jint)ret; #endif jniThrowIOException(env, ENOSYS); return -1; } -static void writeNative(JNIEnv *env, jobject obj, jint data) { +/** jb must not be null. offset and offset+length must be within array */ +static jint writeNative(JNIEnv *env, jobject obj, jbyteArray jb, jint offset, + jint length) { #ifdef HAVE_BLUETOOTH LOGV(__FUNCTION__); - const char buf = (char)data; + int ret; + jbyte *b; struct asocket *s = get_socketData(env, obj); if (!s) - return; + return -1; - if (asocket_write(s, &buf, 1, -1) < 0) + b = env->GetByteArrayElements(jb, NULL); + if (b == NULL) { + jniThrowIOException(env, EINVAL); + return -1; + } + + ret = asocket_write(s, &b[offset], length, -1); + if (ret < 0) { jniThrowIOException(env, errno); + env->ReleaseByteArrayElements(jb, b, JNI_ABORT); + return -1; + } + + env->ReleaseByteArrayElements(jb, b, JNI_ABORT); // no need to commit + return (jint)ret; - return; #endif jniThrowIOException(env, ENOSYS); + return -1; } static void closeNative(JNIEnv *env, jobject obj) { @@ -301,8 +329,8 @@ static JNINativeMethod sMethods[] = { {"bindListenNative", "(I)V", (void *) bindListenNative}, {"acceptNative", "(I)Landroid/bluetooth/BluetoothSocket;", (void *) acceptNative}, {"availableNative", "()I", (void *) availableNative}, - {"readNative", "()I", (void *) readNative}, - {"writeNative", "(I)V", (void *) writeNative}, + {"readNative", "([BII)I", (void *) readNative}, + {"writeNative", "([BII)I", (void *) writeNative}, {"closeNative", "()V", (void *) closeNative}, {"destroyNative", "()V", (void *) destroyNative}, }; |