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

Allow \n to be used for line endings instead of \r\n #66

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public class BluetoothSPP {

private String keyword = "";
private boolean isAndroid = BluetoothState.DEVICE_ANDROID;
private boolean mUseCarriageReturn = true;

private BluetoothConnectionListener bcl;
private int c = 0;
Expand Down Expand Up @@ -122,6 +123,7 @@ public boolean cancelDiscovery() {

public void setupService() {
mChatService = new BluetoothService(mContext, mHandler);
mChatService.setUseCarriageReturn(mUseCarriageReturn);
}

public BluetoothAdapter getBluetoothAdapter() {
Expand Down Expand Up @@ -165,6 +167,10 @@ public void setDeviceTarget(boolean isAndroid) {
startService(isAndroid);
BluetoothSPP.this.isAndroid = isAndroid;
}

public void setUseCarriageReturn(boolean useCarriageReturn) {
mUseCarriageReturn = useCarriageReturn;
}

@SuppressLint("HandlerLeak")
private final Handler mHandler = new Handler() {
Expand Down Expand Up @@ -269,10 +275,11 @@ public void enable() {
public void send(byte[] data, boolean CRLF) {
if(mChatService.getState() == BluetoothState.STATE_CONNECTED) {
if(CRLF) {
byte[] data2 = new byte[data.length + 2];
byte[] data2 = new byte[data.length + (mUseCarriageReturn ? 2 : 1)];
for(int i = 0 ; i < data.length ; i++)
data2[i] = data[i];
data2[data2.length - 2] = 0x0A;
if (mUseCarriageReturn)
data2[data2.length - 2] = 0x0A;
data2[data2.length - 1] = 0x0D;
mChatService.write(data2);
} else {
Expand All @@ -283,8 +290,12 @@ public void send(byte[] data, boolean CRLF) {

public void send(String data, boolean CRLF) {
if(mChatService.getState() == BluetoothState.STATE_CONNECTED) {
if(CRLF)
data += "\r\n";
if(CRLF) {
if (mUseCarriageReturn)
data += "\r\n";
else
data += "\n";
}
mChatService.write(data.getBytes());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public class BluetoothService {
private ConnectedThread mConnectedThread;
private int mState;
private boolean isAndroid = BluetoothState.DEVICE_ANDROID;
private boolean mUseCarriageReturn = true;

// Constructor. Prepares a new BluetoothChat session
// context : The UI Activity Context
Expand All @@ -81,6 +82,14 @@ public synchronized int getState() {
return mState;
}

/**
* Set whether received messages should end with carriage return followed by newline or just newline
* @param useCarriageReturn true to use carriage return
*/
public synchronized void setUseCarriageReturn(boolean useCarriageReturn) {
mUseCarriageReturn = useCarriageReturn;
}

// Start the chat service. Specifically start AcceptThread to begin a
// session in listening (server) mode. Called by the Activity onResume()
public synchronized void start(boolean isAndroid) {
Expand Down Expand Up @@ -352,12 +361,15 @@ public void run() {
byte[] buffer;
ArrayList<Integer> arr_byte = new ArrayList<Integer>();

byte terminator = mUseCarriageReturn ? (byte)0x0A : (byte)0x0D;

// Keep listening to the InputStream while connected
while (true) {
try {
int data = mmInStream.read();
if(data == 0x0A) {
} else if(data == 0x0D) {

if(mUseCarriageReturn && data == 0x0A) {
} else if((!mUseCarriageReturn && data == 0x0A) || data == 0x0D) {
buffer = new byte[arr_byte.size()];
for(int i = 0 ; i < arr_byte.size() ; i++) {
buffer[i] = arr_byte.get(i).byteValue();
Expand Down