Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fix read().

Fix example.
  • Loading branch information...
commit d3f1ae21e29553f5ec48129b4ed56e042863a3ee 1 parent 866a662
Hüseyin Kozan authored
6 Bluetooth.js
@@ -297,9 +297,11 @@ cordova.define("cordova/plugin/bluetooth", function(require, exports, module) {
297 297 * failureCallback(message) : function to be called when there was a problem while \
298 298 * reading or after socket has been closed.
299 299 * socketId : connected socket id
  300 + * bufferSize : internal buffer size while reading, default is 1024
300 301 */
301   - Bluetooth.prototype.read = function(successCallback,failureCallback,socketId) {
302   - return exec(successCallback, failureCallback, 'BluetoothPlugin', 'read', [socketId]);
  302 + Bluetooth.prototype.read = function(successCallback,failureCallback,socketId,bufferSize) {
  303 + bufferSize = typeof bufferSize !== 'undefined' ? bufferSize : 1024;
  304 + return exec(successCallback, failureCallback, 'BluetoothPlugin', 'read', [socketId,bufferSize]);
303 305 }
304 306
305 307 /**
40 BluetoothPlugin.java
@@ -27,8 +27,10 @@
27 27 import org.apache.cordova.api.PluginResult;
28 28 import org.json.JSONArray;
29 29
  30 +import java.io.BufferedReader;
30 31 import java.io.IOException;
31 32 import java.io.InputStream;
  33 +import java.io.InputStreamReader;
32 34 import java.io.OutputStream;
33 35 import java.lang.reflect.Field;
34 36 import java.lang.reflect.Method;
@@ -409,11 +411,12 @@ else if ( ACTION_CANCEL_LISTENING.equals(action) )
409 411 else if ( ACTION_READ.equals(action) )
410 412 {
411 413 final int socketId = args.getInt(0);
  414 + final int bufferSize = args.getInt(1);
412 415 this.callback_read = callbackContext;
413   - m_readThreads.add(
414   - new ReadThread(
415   - m_sockets.get(socketId),
416   - socketId));
  416 + ReadThread readThread = new ReadThread(
  417 + m_sockets.get(socketId),socketId,bufferSize);
  418 + readThread.start();
  419 + m_readThreads.add(readThread);
417 420 PluginResult pluginResult = new PluginResult(
418 421 PluginResult.Status.NO_RESULT);
419 422 pluginResult.setKeepCallback(true);
@@ -593,7 +596,6 @@ private void logErr(String msg) {
593 596
594 597 /**
595 598 * Listen Thread
596   - * @author hk
597 599 */
598 600 private class ListenThread extends Thread {
599 601 private final BluetoothServerSocket mm_serverSocket;
@@ -680,33 +682,43 @@ public void cancel() {
680 682 /**
681 683 * CommThread to read and write to socket.
682 684 * Auto shuts itself after a socket error.
683   - * @author huseyin
684 685 */
685 686 private class ReadThread extends Thread {
686   - private final InputStream mm_inputStream;
  687 + private final BufferedReader mm_bufferedReader;
687 688 public final int socketId;
  689 + public final int mm_bufferSize;
688 690
689   - public ReadThread(BluetoothSocket socket, int socketId) {
  691 + public ReadThread(BluetoothSocket socket, int socketId, int bufferSize) {
690 692 this.socketId = socketId;
  693 + mm_bufferSize = bufferSize;
691 694 InputStream in = null;
692 695 try {
693 696 in = socket.getInputStream();
694 697 } catch (IOException e) {
695 698 logErr("Cannot create read scokets");
696 699 }
697   - mm_inputStream = in;
  700 + InputStreamReader isr = new InputStreamReader(in);
  701 + mm_bufferedReader = new BufferedReader(isr);
698 702 }
699 703
700 704 @Override
701 705 public void run() {
  706 + logDbg("ReadThread::run()");
702 707 while(true) {
703 708 try {
704   - JSONArray ja = new JSONArray();
705   - int available = mm_inputStream.available();
706   - for (int i = 0; i < available; i++) {
707   - ja.put(mm_inputStream.read());
  709 + char[] buffer = new char [mm_bufferSize];
  710 + if (mm_bufferedReader.read(buffer) >= 0) {
  711 + JSONArray ja = new JSONArray();
  712 + // logDbg("read = " + buffer.toString());
  713 + for (int i = 0; i < buffer.length; i++) {
  714 + ja.put((int)buffer[i]);
  715 + }
  716 + // logDbg("ja = " + ja.toString());
  717 + PluginResult result = new PluginResult(
  718 + PluginResult.Status.OK, ja);
  719 + result.setKeepCallback(true);
  720 + callback_read.sendPluginResult(result);
708 721 }
709   - callback_read.success(ja);
710 722 } catch (IOException e) {
711 723 /* socket disconnected */
712 724 logErr(e.toString() + " / " + e.getMessage());
5 README.md
Source Rendered
... ... @@ -1,12 +1,9 @@
1   -# Phonegap Bluetooth Eklentisi #
  1 +# Phonegap Bluetooth Eklentisi #
2 2
3 3 Projeyi geliştirmeyi durdurdum. Kodları referans olması için bırakıyorum.
4 4
5   -Phonegap (Cordova) 2.2.x için bir eklentidir.
6   -
7 5 For English please see [README_EN.md].
8 6
9   -
10 7 # Nasıl Kullanılır #
11 8 Örnek projeye bakın : *assets/www/index.html*
12 9
4 README_EN.md
Source Rendered
... ... @@ -1,9 +1,7 @@
1   -# Phonegap Bluetooth Plugin #
  1 +# Phonegap Bluetooth Plugin #
2 2
3 3 I have stopped development this plugin. The codes are here for reference.
4 4
5   -Plugin for Phonegap (Cordova) 2.2.x
6   -
7 5 # How to Use #
8 6 See example : *assets/www/index.html*
9 7
6 example/assets/www/Bluetooth.js
@@ -297,9 +297,11 @@ cordova.define("cordova/plugin/bluetooth", function(require, exports, module) {
297 297 * failureCallback(message) : function to be called when there was a problem while \
298 298 * reading or after socket has been closed.
299 299 * socketId : connected socket id
  300 + * bufferSize : internal buffer size while reading, default is 1024
300 301 */
301   - Bluetooth.prototype.read = function(successCallback,failureCallback,socketId) {
302   - return exec(successCallback, failureCallback, 'BluetoothPlugin', 'read', [socketId]);
  302 + Bluetooth.prototype.read = function(successCallback,failureCallback,socketId,bufferSize) {
  303 + bufferSize = typeof bufferSize !== 'undefined' ? bufferSize : 1024;
  304 + return exec(successCallback, failureCallback, 'BluetoothPlugin', 'read', [socketId,bufferSize]);
303 305 }
304 306
305 307 /**
12 example/assets/www/index.js
@@ -407,10 +407,14 @@ function toggleListen(item) {
407 407 function(sockId){
408 408 item.disabled = false;
409 409 item.value = "Cancel";
410   - document.getElementById("listen-textare").value = "Connected socket:" + sockId + "\n";
  410 + document.getElementById("listen-textarea").value = "Connected socket:" + sockId + "\n";
411 411 bluetoothPugin.read(
412 412 function( p_data ) {
413   - document.getElementById("listen-textarea").value += "Data : >" + p_data + "<\n";
  413 + var str = "";
  414 + for ( var i = 0; i < p_data.length; i++) {
  415 + str += String.fromCharCode(p_data[i]);
  416 + }
  417 + document.getElementById("listen-textarea").value += "Data : >" + str + "<\n";
414 418 },
415 419 function( error ) {
416 420 document.getElementById("listen-textarea").value += "Error : >" + error + "<\n";
@@ -461,8 +465,8 @@ function sendMessage() {
461 465 for (var i=0; i<d.length; ++i) {
462 466 arr[i] = d.charCodeAt(i);
463 467 }
464   - jarr += "";
465   -
  468 + var newLine = "\n";
  469 + arr[arr.length] = newLine.charCodeAt(0);
466 470
467 471
468 472 bluetoothPugin.write(
9 example/bin/AndroidManifest.xml
@@ -5,7 +5,7 @@
5 5
6 6 <uses-sdk
7 7 android:minSdkVersion="8"
8   - android:targetSdkVersion="15" />
  8 + android:targetSdkVersion="17" />
9 9
10 10 <uses-permission android:name="android.permission.BLUETOOTH" />
11 11 <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
@@ -17,18 +17,19 @@
17 17 android:resizeable="true"
18 18 android:smallScreens="true" />
19 19
  20 + <uses-permission android:name="android.permission.CAMERA" />
20 21 <uses-permission android:name="android.permission.VIBRATE" />
21 22 <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
22 23 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
23 24 <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
24   - <uses-permission android:name="android.permission.READ_PHONE_STATE" />
25 25 <uses-permission android:name="android.permission.INTERNET" />
26 26 <uses-permission android:name="android.permission.RECEIVE_SMS" />
27 27 <uses-permission android:name="android.permission.RECORD_AUDIO" />
  28 + <uses-permission android:name="android.permission.RECORD_VIDEO"/>
28 29 <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
29 30 <uses-permission android:name="android.permission.READ_CONTACTS" />
30   - <uses-permission android:name="android.permission.WRITE_CONTACTS" />
31   - <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
  31 + <uses-permission android:name="android.permission.WRITE_CONTACTS" />
  32 + <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
32 33 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
33 34 <uses-permission android:name="android.permission.GET_ACCOUNTS" />
34 35 <uses-permission android:name="android.permission.BROADCAST_STICKY" />
2  example/project.properties
@@ -11,4 +11,4 @@
11 11 #proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt
12 12
13 13 # Project target.
14   -target=android-16
  14 +target=android-17
40 example/src/org/apache/cordova/plugin/BluetoothPlugin.java
@@ -27,8 +27,10 @@
27 27 import org.apache.cordova.api.PluginResult;
28 28 import org.json.JSONArray;
29 29
  30 +import java.io.BufferedReader;
30 31 import java.io.IOException;
31 32 import java.io.InputStream;
  33 +import java.io.InputStreamReader;
32 34 import java.io.OutputStream;
33 35 import java.lang.reflect.Field;
34 36 import java.lang.reflect.Method;
@@ -409,11 +411,12 @@ else if ( ACTION_CANCEL_LISTENING.equals(action) )
409 411 else if ( ACTION_READ.equals(action) )
410 412 {
411 413 final int socketId = args.getInt(0);
  414 + final int bufferSize = args.getInt(1);
412 415 this.callback_read = callbackContext;
413   - m_readThreads.add(
414   - new ReadThread(
415   - m_sockets.get(socketId),
416   - socketId));
  416 + ReadThread readThread = new ReadThread(
  417 + m_sockets.get(socketId),socketId,bufferSize);
  418 + readThread.start();
  419 + m_readThreads.add(readThread);
417 420 PluginResult pluginResult = new PluginResult(
418 421 PluginResult.Status.NO_RESULT);
419 422 pluginResult.setKeepCallback(true);
@@ -593,7 +596,6 @@ private void logErr(String msg) {
593 596
594 597 /**
595 598 * Listen Thread
596   - * @author hk
597 599 */
598 600 private class ListenThread extends Thread {
599 601 private final BluetoothServerSocket mm_serverSocket;
@@ -680,33 +682,43 @@ public void cancel() {
680 682 /**
681 683 * CommThread to read and write to socket.
682 684 * Auto shuts itself after a socket error.
683   - * @author huseyin
684 685 */
685 686 private class ReadThread extends Thread {
686   - private final InputStream mm_inputStream;
  687 + private final BufferedReader mm_bufferedReader;
687 688 public final int socketId;
  689 + public final int mm_bufferSize;
688 690
689   - public ReadThread(BluetoothSocket socket, int socketId) {
  691 + public ReadThread(BluetoothSocket socket, int socketId, int bufferSize) {
690 692 this.socketId = socketId;
  693 + mm_bufferSize = bufferSize;
691 694 InputStream in = null;
692 695 try {
693 696 in = socket.getInputStream();
694 697 } catch (IOException e) {
695 698 logErr("Cannot create read scokets");
696 699 }
697   - mm_inputStream = in;
  700 + InputStreamReader isr = new InputStreamReader(in);
  701 + mm_bufferedReader = new BufferedReader(isr);
698 702 }
699 703
700 704 @Override
701 705 public void run() {
  706 + logDbg("ReadThread::run()");
702 707 while(true) {
703 708 try {
704   - JSONArray ja = new JSONArray();
705   - int available = mm_inputStream.available();
706   - for (int i = 0; i < available; i++) {
707   - ja.put(mm_inputStream.read());
  709 + char[] buffer = new char [mm_bufferSize];
  710 + if (mm_bufferedReader.read(buffer) >= 0) {
  711 + JSONArray ja = new JSONArray();
  712 + // logDbg("read = " + buffer.toString());
  713 + for (int i = 0; i < buffer.length; i++) {
  714 + ja.put((int)buffer[i]);
  715 + }
  716 + // logDbg("ja = " + ja.toString());
  717 + PluginResult result = new PluginResult(
  718 + PluginResult.Status.OK, ja);
  719 + result.setKeepCallback(true);
  720 + callback_read.sendPluginResult(result);
708 721 }
709   - callback_read.success(ja);
710 722 } catch (IOException e) {
711 723 /* socket disconnected */
712 724 logErr(e.toString() + " / " + e.getMessage());

0 comments on commit d3f1ae2

sankethm

the data sent from one phone should appear as popup in the other phone.How to do this?please please please help

Hüseyin Kozan
Hüseyin Kozan
Please sign in to comment.
Something went wrong with that request. Please try again.