GithubHelp home page GithubHelp logo

Comments (3)

omaraflak avatar omaraflak commented on July 17, 2024

Hey @alanoatwork, thanks for your suggestion. Sending is not a problem anymore, you can now send messages as String or byte[] using bluetooth.send(). In order to receive messages you have 2 options :

  1. Use the default behavior that is going to read until it hits a new line, then return the message in a byte array;
  2. Create your own reader class, it should extend from SocketReader;

The first option is actually a particular case of the second one : https://github.com/OmarAflak/Bluetooth-Library/blob/issue_44/bluetooth/src/main/java/me/aflak/bluetooth/reader/LineReader.java

from bluetooth-library.

omaraflak avatar omaraflak commented on July 17, 2024

Unfortunately I can't test the code as I don't have any hardware right now, therefore I can't release the new version. If you want to test it yourself however, here is the new branch : https://github.com/OmarAflak/Bluetooth-Library/tree/issue_44

from bluetooth-library.

omaraflak avatar omaraflak commented on July 17, 2024

Hey Alan, this issue is fixed in the new version.

implementation 'me.aflak.libraries:bluetooth:1.3.8'

To have more control over the socket reader, you should create a class that extends SocketReader and override byte[] read() throw IOException. In your case this should work, just set the right delimiter :

public class DelimiterReader extends SocketReader {
    private PushbackInputStream reader;
    private byte delimiter;

    public DelimiterReader(InputStream inputStream) {
        super(inputStream);
        reader = new PushbackInputStream(inputStream);
        delimiter = 0;
    }

    @Override
    public byte[] read() throws IOException {
        List<Byte> byteList = new ArrayList<>();
        byte[] tmp = new byte[1];

        while(true) {
            int n = reader.read();
            reader.unread(n);

            int count = reader.read(tmp);
            if(count > 0) {
                if(tmp[0] == delimiter){
                    byte[] returnBytes = new byte[byteList.size()];
                    for(int i=0 ; i<byteList.size() ; i++){
                        returnBytes[i] = byteList.get(i);
                    }
                    return returnBytes;
                } else {
                    byteList.add(tmp[0]);
                }
            }
        }
    }
}

Then,

bluetooth.setReader(DelimiterReader.class)

from bluetooth-library.

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.