Comments (3)
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 :
- Use the default behavior that is going to read until it hits a new line, then return the message in a byte array;
- 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.
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.
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)
- String Translate ... HOT 2
- Multiple Bluetooth Client Connections HOT 1
- AutoConnect Error HOT 1
- NullPointerException when starting another activity HOT 3
- Add support for PIN HOT 2
- Multiple Clients. HOT 1
- About scanning the nearby bluetooth device HOT 7
- How to set baudrate? HOT 1
- onMessage method is not calling after device sending data to application HOT 2
- Android 9 device not visible in scan HOT 1
- Cannot connect after close an activity and reopen again. HOT 1
- Can not terminate socket on activity close. HOT 2
- startDiscovery impossible HOT 7
- Connected but chat not working HOT 1
- Connected but chat not working HOT 2
- SocketReader read() returning null HOT 2
- Android Bluetooth Classic RFCOMM connection failing, getUUIDs() returns no SPP UUID HOT 2
- Audio not working in bluetooth headphone when connected to bluetooth HOT 1
- My Application restart when I connect/disconnect bluetooth USB 5.0 Bluetooth Dongle
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 bluetooth-library.