Comments (14)
I found the reason.
In bluetoothService.java, when reading message from input stream. the code below:
public void run() {
byte[] buffer;
ArrayList<Integer> arr_byte = new ArrayList<Integer>();
// Keep listening to the InputStream while connected
while (true) {
try {
int data = mmInStream.read();
if(data == 0x0A) {
} else if(data == 0x0D) {
buffer = new byte[arr_byte.size()];
for(int i = 0 ; i < arr_byte.size() ; i++) {
buffer[i] = arr_byte.get(i).byteValue();
}
// Send the obtained bytes to the UI Activity
mHandler.obtainMessage(BluetoothState.MESSAGE_READ
, buffer.length, -1, buffer).sendToTarget();
arr_byte = new ArrayList<Integer>();
} else {
arr_byte.add(data);
}
} catch (IOException e) {
connectionLost();
// Start the service over to restart listening mode
BluetoothService.this.start(BluetoothService.this.isAndroid);
break;
}
}
}
When the byte equals '0x0D' which means ENTER, it will notify the handler.I debuged here, it entered with the message, but it not notified the method onDataReceived since there is no byte '0x0D'. When I send the message contains '0x0D' (ENTER), it works.
from bluetoothspplibrary.
add '\r' end,and it really works!
from bluetoothspplibrary.
Hi,
It may be too late, but this may help someone else.
I had the same problem and solved. Somehow the "setOnDataReceivedListener" method gets called more than once, and the mDataReceivedListener points two different objects. You can fix it as:
Find the following part in BluetoothSPP.java:
public void setOnDataReceivedListener (OnDataReceivedListener listener) {
mDataReceivedListener = listener;
}
modify it:
public void setOnDataReceivedListener (OnDataReceivedListener listener) {
if (mDataReceivedListener == null)
mDataReceivedListener = listener;
}
from bluetoothspplibrary.
Not sure that works... what happens if you want to add again a new receiver? The function will reject it because it was previously set.
from bluetoothspplibrary.
Well, I accept there may be some side effects, but I am 100% sure that it works this way, if there are no other errors. By the way, I used the autoconnect example provided in the library itself adding datareceivedlistener to it.
Answering your question:
I can't think of any case having more than one event handler (or listener) in the same code for the same event that is useful. Even so, the library programmer would have provided an array of "mDataReceivedListener" and invoke all upon the event occurrence, i.e., upon data reception.
from bluetoothspplibrary.
Well, the library does not offer an array parameter. Maybe you can't think of any case... but I certainly do and actually use. Just think of a program that passes different messages back and forth... for each message you would expect a different response, so one think you can do is re-program the receiver. I agree there are other ways to do it, though.
Besides, coming back to the original question from @mschopra , there are many reasons why you can't receive, but without further analysis it's impossible to come up with an answer. I don't know where @lazy21tr second call to setOnDataReceivedListener comes from... but there are other users (look in the isses section) that report that you need an end-of-line sent for the receiver to pass the message through. There is an Android BlueTooth Debug option in your phone that dumps the buffers to a file in the SD Card, maybe you can try that out.
from bluetoothspplibrary.
Thank you @naevtamarkus for your thorough responses.
Of course there may be plenty of causes preventing data reception. I use the same hardware as @mschopra, the HC05 module. Yes you are right that the receiver needs to send end-of-line characters since the library does a check (in BluetoothService.java, ConnectedThread's run section) for end-of-line before invoking the datareceivedlistener. But I think this is not an issue to fix, just a preference of the library programmer.
I don't have any problem regarding data reception anymore, that is why I commented here. I share my source files here as it may help someone who got stuck with the issue.
Update: P.S.: The same problem happens for the setBluetoothConnectionListener method either. I figured out today that this is happening as I set listener methods in onCreate method. For some reason (e.g., upon orientation changes) onCreate gets called again. Someone may come up with another solution to the issue, but I solved it in this way: preventing duplicate assignment.
from bluetoothspplibrary.
Hehe, this is interesting... I use HC05 too! Any Arduinos behind? :)
I actually have a problem with the setBluetoothConnectionListener... you gave me the idea with the setOnDataReceivedListener, funny that you mentioned setBluetoothConnectionListener just now!! It's very weird:
The thing is that, when I use this BT module from a normal activity it works like a charm (I mean, setting all those callbacks) but when I do it from a background thread (a BroadcastReceiver, to be specific) the callbacks somehow disappear! It looks like, in your case, someone-or-something is wiping them without my consent!
I will try your workaround when I have some time and, if that worked, I am going to send you flowers :) Not that is a solution, though... we need to find where this setBluetoothConnectionListener(null) comes from!
from bluetoothspplibrary.
淡定
------------------ 原始邮件 ------------------
发件人: "Pablo Fernandez";[email protected];
发送时间: 2015年10月16日(星期五) 下午2:46
收件人: "akexorcist/Android-BluetoothSPPLibrary"[email protected];
主题: Re: [Android-BluetoothSPPLibrary] OnDataReceived (#30)
Hehe, this is interesting... I use HC05 too! Any Arduinos behind? :)
I actually have a problem with the setBluetoothConnectionListener... you gave me the idea with the setOnDataReceivedListener, funny that you mentioned setBluetoothConnectionListener just now!! It's very weird:
The thing is that, when I use this BT module from a normal activity it works like a charm (I mean, setting all those callbacks) but when I do it from a background thread (a BroadcastReceiver, to be specific) the callbacks somehow disappear! It looks like, in your case, someone-or-something is wiping them without my consent!
I will try your workaround when I have some time and, if that worked, I am going to send you flowers :) Not that is a solution, though... we need to find where this setBluetoothConnectionListener(null) comes from!
—
Reply to this email directly or view it on GitHub.
from bluetoothspplibrary.
the setBluetoothConnectionListener was not listed as an issue by @mschopra or someone else (afaik), that's why i didn't mentioned it before.
hope you come up with a good solution :)
from bluetoothspplibrary.
Yeah, sorry, I was using this thread as a chat :-)
from bluetoothspplibrary.
Hi,
it seems that in my case, even after reading your comments above, using @lazy21tr 's patch, or using @lazy21tr 's shared "working files", I still don't receive data with BluetoothSPP. The BluetoothState.MESSAGE_READ never happens.
My BT SPP device is receiving the data I sent, and replies back, and I can see the reply in the Bluetooth HCI snoop log (so definitely is received by my Android tablet), but the event is never raised in the BluetoothSPPLibrary.
I am using:
compileSdkVersion 23, buildToolsVersion "25.0.2", minSdkVersion 23, targetSdkVersion 23
Any thoughts?
from bluetoothspplibrary.
public void run() {
byte[] buffer;
ArrayList arr_byte = new ArrayList();
// Keep listening to the InputStream while connected
while (true) {
try {
int data = mmInStream.read();
if(data == 0x0A) {
} else if(data == 0x0D) {
buffer = new byte[arr_byte.size()];
for(int i = 0 ; i < arr_byte.size() ; i++) {
buffer[i] = arr_byte.get(i).byteValue();
}
// Send the obtained bytes to the UI Activity
mHandler.obtainMessage(BluetoothState.MESSAGE_READ
, buffer.length, -1, buffer).sendToTarget();
arr_byte = new ArrayList<Integer>();
} else {
arr_byte.add(data);
}
} catch (IOException e) {
connectionLost();
// Start the service over to restart listening mode
BluetoothService.this.start(BluetoothService.this.isAndroid);
break;
}
}
}
I have same code but still it is not working.
from bluetoothspplibrary.
any one solved the problem
from bluetoothspplibrary.
Related Issues (20)
- 이 블루투스 라이브러리를 사용하려는 한국인들을 위해 HOT 1
- transfer intent
- take multiple masseges on recived data
- Bt
- Context HOT 1
- NullPointerException at app.akexorcist.bluetotohspp.library.BluetoothService$AcceptThread.run (BluetoothService.java:2)
- Not scanning or discoverying nearby bluetooth devices. (startActivityForResult) HOT 2
- Prematurely terminating data packets with byte values of 10 (LF) and 13 (CR) HOT 2
- Can't read data from a BT barcode reader
- Activity crashed A/libc: Fatal signal 11 (SIGSEGV), code 1 HOT 1
- thank u
- getBluetoothService() called with no BluetoothManagerCallback HOT 1
- targetSdkVersion < 4
- the package name is spelled incorrectly...
- ConnectThread NullPointerException when the BT has been manually disabled
- How to make own Reciver
- bt.setupService()
- Fixing send function HOT 1
- How do you solve the issue of broken packets/messages? HOT 1
- Question: Will this library give me serial port in PC when my android device connected to PC using BT?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from bluetoothspplibrary.