Skip to content
This repository has been archived by the owner on Jan 27, 2021. It is now read-only.

OnDataReceived #30

Open
mschopra opened this issue Jun 2, 2015 · 14 comments
Open

OnDataReceived #30

mschopra opened this issue Jun 2, 2015 · 14 comments

Comments

@mschopra
Copy link

mschopra commented Jun 2, 2015

hi,

I have tried this code and everything is working fine except data receive , i am not able to receive data by my HC-05 module .

Data send by mobile is working fine , please suggest

@msuzer
Copy link

msuzer commented Oct 14, 2015

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;
}

@naevtamarkus
Copy link

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.

@msuzer
Copy link

msuzer commented Oct 15, 2015

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.

@naevtamarkus
Copy link

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.

@msuzer
Copy link

msuzer commented Oct 15, 2015

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.

@naevtamarkus
Copy link

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!

@xiaochenMate
Copy link

淡定

------------------ 原始邮件 ------------------
发件人: "Pablo Fernandez";notifications@github.com;
发送时间: 2015年10月16日(星期五) 下午2:46
收件人: "akexorcist/Android-BluetoothSPPLibrary"Android-BluetoothSPPLibrary@noreply.github.com;

主题: 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.

@msuzer
Copy link

msuzer commented Oct 16, 2015

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 :)

@naevtamarkus
Copy link

Yeah, sorry, I was using this thread as a chat :-)

@irares
Copy link

irares commented Feb 3, 2017

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?

@WenGray
Copy link

WenGray commented Apr 13, 2017

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.

@vidyajejurkar
Copy link

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.

@Twsa
Copy link

Twsa commented Jun 6, 2018

add '\r' end,and it really works!

@youssefghandour
Copy link

any one solved the problem

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants