diff --git a/library/src/main/java/app/akexorcist/bluetotohspp/library/BluetoothSPP.java b/library/src/main/java/app/akexorcist/bluetotohspp/library/BluetoothSPP.java index 869a98b..eb7fb12 100644 --- a/library/src/main/java/app/akexorcist/bluetotohspp/library/BluetoothSPP.java +++ b/library/src/main/java/app/akexorcist/bluetotohspp/library/BluetoothSPP.java @@ -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; @@ -122,6 +123,7 @@ public boolean cancelDiscovery() { public void setupService() { mChatService = new BluetoothService(mContext, mHandler); + mChatService.setUseCarriageReturn(mUseCarriageReturn); } public BluetoothAdapter getBluetoothAdapter() { @@ -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() { @@ -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 { @@ -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()); } } diff --git a/library/src/main/java/app/akexorcist/bluetotohspp/library/BluetoothService.java b/library/src/main/java/app/akexorcist/bluetotohspp/library/BluetoothService.java index a136ca2..82ddcbf 100644 --- a/library/src/main/java/app/akexorcist/bluetotohspp/library/BluetoothService.java +++ b/library/src/main/java/app/akexorcist/bluetotohspp/library/BluetoothService.java @@ -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 @@ -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) { @@ -352,12 +361,15 @@ public void run() { byte[] buffer; ArrayList arr_byte = new ArrayList(); + 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();