Giter Site home page Giter Site logo

OnDataReceived about bluetoothspplibrary HOT 14 OPEN

akexorcist avatar akexorcist commented on June 21, 2024
OnDataReceived

from bluetoothspplibrary.

Comments (14)

WenGray avatar WenGray commented on June 21, 2024 5

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.

Twsa avatar Twsa commented on June 21, 2024 2

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

from bluetoothspplibrary.

msuzer avatar msuzer commented on June 21, 2024 1

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.

naevtamarkus avatar naevtamarkus commented on June 21, 2024

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.

msuzer avatar msuzer commented on June 21, 2024

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.

naevtamarkus avatar naevtamarkus commented on June 21, 2024

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.

msuzer avatar msuzer commented on June 21, 2024

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.

naevtamarkus avatar naevtamarkus commented on June 21, 2024

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.

xiaochenMate avatar xiaochenMate commented on June 21, 2024

淡定

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

msuzer avatar msuzer commented on June 21, 2024

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.

naevtamarkus avatar naevtamarkus commented on June 21, 2024

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

from bluetoothspplibrary.

irares avatar irares commented on June 21, 2024

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.

vidyajejurkar avatar vidyajejurkar commented on June 21, 2024

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.

youssefghandour avatar youssefghandour commented on June 21, 2024

any one solved the problem

from bluetoothspplibrary.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.