BluetoothSPPLibrary icon indicating copy to clipboard operation
BluetoothSPPLibrary copied to clipboard

OnDataReceived

Open mschopra opened this issue 9 years ago • 14 comments

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

mschopra avatar Jun 02 '15 07:06 mschopra

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

msuzer avatar Oct 14 '15 19:10 msuzer

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.

naevtamarkus avatar Oct 15 '15 07:10 naevtamarkus

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.

msuzer avatar Oct 15 '15 09:10 msuzer

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.

naevtamarkus avatar Oct 15 '15 11:10 naevtamarkus

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.

msuzer avatar Oct 15 '15 18:10 msuzer

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!

naevtamarkus avatar Oct 16 '15 06:10 naevtamarkus

淡定

------------------ 原始邮件 ------------------ 发件人: "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.

xiaochenMate avatar Oct 16 '15 06:10 xiaochenMate

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

msuzer avatar Oct 16 '15 08:10 msuzer

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

naevtamarkus avatar Oct 16 '15 08:10 naevtamarkus

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?

irares avatar Feb 03 '17 21:02 irares

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.

WenGray avatar Apr 13 '17 08:04 WenGray

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

I have same code but still it is not working.

vidyajejurkar avatar May 15 '17 12:05 vidyajejurkar

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

Twsa avatar Jun 06 '18 01:06 Twsa

any one solved the problem

youssefghandour avatar May 17 '20 11:05 youssefghandour