commit 4271722e6f5dfabaca1cd2ffdadb4e74b839586e Author: Petr KracĂ­k Date: Sat Mar 7 20:12:27 2020 +0100 Initial commit version 0.0.1 alpha diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..603b140 --- /dev/null +++ b/.gitignore @@ -0,0 +1,14 @@ +*.iml +.gradle +/local.properties +/.idea/caches +/.idea/libraries +/.idea/modules.xml +/.idea/workspace.xml +/.idea/navEditor.xml +/.idea/assetWizardSettings.xml +.DS_Store +/build +/captures +.externalNativeBuild +.cxx diff --git a/.idea/.name b/.idea/.name new file mode 100644 index 0000000..d62d26e --- /dev/null +++ b/.idea/.name @@ -0,0 +1 @@ +Goojprt Settings \ No newline at end of file diff --git a/.idea/codeStyles/Project.xml b/.idea/codeStyles/Project.xml new file mode 100644 index 0000000..681f41a --- /dev/null +++ b/.idea/codeStyles/Project.xml @@ -0,0 +1,116 @@ + + + + + + + +
+ + + + xmlns:android + + ^$ + + + +
+
+ + + + xmlns:.* + + ^$ + + + BY_NAME + +
+
+ + + + .*:id + + http://schemas.android.com/apk/res/android + + + +
+
+ + + + .*:name + + http://schemas.android.com/apk/res/android + + + +
+
+ + + + name + + ^$ + + + +
+
+ + + + style + + ^$ + + + +
+
+ + + + .* + + ^$ + + + BY_NAME + +
+
+ + + + .* + + http://schemas.android.com/apk/res/android + + + ANDROID_ATTRIBUTE_ORDER + +
+
+ + + + .* + + .* + + + BY_NAME + +
+
+
+
+
+
\ No newline at end of file diff --git a/.idea/gradle.xml b/.idea/gradle.xml new file mode 100644 index 0000000..d291b3d --- /dev/null +++ b/.idea/gradle.xml @@ -0,0 +1,16 @@ + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..37a7509 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,9 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/runConfigurations.xml b/.idea/runConfigurations.xml new file mode 100644 index 0000000..7f68460 --- /dev/null +++ b/.idea/runConfigurations.xml @@ -0,0 +1,12 @@ + + + + + + \ No newline at end of file diff --git a/app/.gitignore b/app/.gitignore new file mode 100644 index 0000000..796b96d --- /dev/null +++ b/app/.gitignore @@ -0,0 +1 @@ +/build diff --git a/app/build.gradle b/app/build.gradle new file mode 100644 index 0000000..02155e6 --- /dev/null +++ b/app/build.gradle @@ -0,0 +1,28 @@ +apply plugin: 'com.android.application' + +android { + compileSdkVersion 28 + defaultConfig { + applicationId "net.petrkr.goojprtsettings" + minSdkVersion 15 + targetSdkVersion 28 + versionCode 1 + versionName "1.0" + testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" + } + buildTypes { + release { + minifyEnabled false + proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' + } + } +} + +dependencies { + implementation fileTree(dir: 'libs', include: ['*.jar']) + implementation 'androidx.appcompat:appcompat:1.0.2' + implementation 'androidx.constraintlayout:constraintlayout:1.1.3' + testImplementation 'junit:junit:4.12' + androidTestImplementation 'androidx.test.ext:junit:1.1.0' + androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1' +} diff --git a/app/proguard-rules.pro b/app/proguard-rules.pro new file mode 100644 index 0000000..f1b4245 --- /dev/null +++ b/app/proguard-rules.pro @@ -0,0 +1,21 @@ +# Add project specific ProGuard rules here. +# You can control the set of applied configuration files using the +# proguardFiles setting in build.gradle. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} + +# Uncomment this to preserve the line number information for +# debugging stack traces. +#-keepattributes SourceFile,LineNumberTable + +# If you keep the line number information, uncomment this to +# hide the original source file name. +#-renamesourcefileattribute SourceFile diff --git a/app/src/androidTest/java/net/petrkr/goojprtsettings/ExampleInstrumentedTest.java b/app/src/androidTest/java/net/petrkr/goojprtsettings/ExampleInstrumentedTest.java new file mode 100644 index 0000000..86b35e6 --- /dev/null +++ b/app/src/androidTest/java/net/petrkr/goojprtsettings/ExampleInstrumentedTest.java @@ -0,0 +1,27 @@ +package net.petrkr.goojprtsettings; + +import android.content.Context; + +import androidx.test.platform.app.InstrumentationRegistry; +import androidx.test.ext.junit.runners.AndroidJUnit4; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import static org.junit.Assert.*; + +/** + * Instrumented test, which will execute on an Android device. + * + * @see Testing documentation + */ +@RunWith(AndroidJUnit4.class) +public class ExampleInstrumentedTest { + @Test + public void useAppContext() { + // Context of the app under test. + Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext(); + + assertEquals("net.petrkr.goojprtsettings", appContext.getPackageName()); + } +} diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml new file mode 100644 index 0000000..530bd31 --- /dev/null +++ b/app/src/main/AndroidManifest.xml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/java/net/petrkr/goojprtsettings/MainActivity.java b/app/src/main/java/net/petrkr/goojprtsettings/MainActivity.java new file mode 100644 index 0000000..c11a853 --- /dev/null +++ b/app/src/main/java/net/petrkr/goojprtsettings/MainActivity.java @@ -0,0 +1,369 @@ +package net.petrkr.goojprtsettings; + +import androidx.appcompat.app.AppCompatActivity; + +import android.app.AlertDialog; +import android.content.DialogInterface; +import android.os.Bundle; +import android.os.Looper; +import android.util.Log; +import android.view.View; +import android.widget.Button; +import android.widget.EditText; +import android.widget.TextView; + +// Import neccessor namespace +import android.bluetooth.BluetoothAdapter; +import android.bluetooth.BluetoothDevice; +import android.bluetooth.BluetoothSocket; + +import java.io.InputStream; +import java.io.OutputStream; +import java.lang.Byte; +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; + +import android.os.Handler; + +public class MainActivity extends AppCompatActivity { + + private static final String TAG = "MainActivity"; + private static final byte[] setCharsetCode = { 0x1B, 0x23, 0x23, 0x53, 0x4C, 0x41, 0x4E }; + + BluetoothAdapter bluetoothAdapter; + BluetoothSocket bluetoothSocket; + BluetoothDevice bluetoothDevice; + + OutputStream outputStream; + InputStream inputStream; + Thread thread; + + byte[] readBuffer; + int readBufferPosition; + volatile boolean stopWorker; + + TextView lblStatus; + TextView lblPrinterName; + EditText textBox; + + Button btnConnect; + Button btnDisconnect; + Button btnPrint; + Button btnPrintCharTable; + Button btnSetCharset; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main); + + // Create object of controls + Button btnFind = findViewById(R.id.btnFind); + + btnConnect = findViewById(R.id.btnConnect); + btnDisconnect = findViewById(R.id.btnDisconnect); + btnPrint = findViewById(R.id.btnPrint); + btnPrintCharTable = findViewById(R.id.btnPrintChars); + btnSetCharset = findViewById(R.id.btnSetCharset); + + textBox = findViewById(R.id.txtText); + + lblStatus = findViewById(R.id.lblStatus); + lblPrinterName = findViewById(R.id.lblPrinterName); + + btnFind.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + try { + FindBluetoothDevice(); + } catch (Exception ex) { + ex.printStackTrace(); + } + } + }); + + btnConnect.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + try { + openBluetoothPrinter(); + } catch (Exception ex) { + ex.printStackTrace(); + } + } + }); + btnDisconnect.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + try { + disconnectBT(); + } catch (Exception ex) { + ex.printStackTrace(); + } + } + }); + btnPrint.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + try { + printData(); + } catch (Exception ex) { + ex.printStackTrace(); + } + } + }); + btnPrintCharTable.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + try { + printCharsetTable(); + } catch (Exception ex) { + ex.printStackTrace(); + } + } + }); + btnSetCharset.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + try { + setCharset(); + } catch (Exception ex) { + ex.printStackTrace(); + } + } + }); + + } + + void setButtonsDisconnect() { + btnConnect.setEnabled(true); + btnDisconnect.setEnabled(false); + btnPrint.setEnabled(false); + btnPrintCharTable.setEnabled(false); + btnSetCharset.setEnabled(false); + } + + void setButtonsConnect() { + btnConnect.setEnabled(false); + btnDisconnect.setEnabled(true); + btnPrint.setEnabled(true); + btnPrintCharTable.setEnabled(true); + btnSetCharset.setEnabled(true); + } + + void FindBluetoothDevice() { + try { + Log.d(TAG, "Find bluetooth device"); + bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); + if (bluetoothAdapter == null) { + Log.w(TAG, "Bluetooth adapter not found"); + lblStatus.setText("No Bluetooth Adapter found"); + return; + } + + if (!bluetoothAdapter.isEnabled()) { + Log.w(TAG, "Bluetooth adapter not enabled."); + lblStatus.setText("Bluetooth is disabled"); + return; + } + + final List pairedDevices = + new ArrayList<>(bluetoothAdapter.getBondedDevices()); + + Log.i(TAG, "Got " + pairedDevices.size() + " paired devices"); + + if (pairedDevices.size() == 0) { + Log.d(TAG, "No paired bluetooth devices"); + return; + } + + final List strings = new ArrayList<>(); + + for (BluetoothDevice pairedDev : pairedDevices) { + strings.add(pairedDev.getName()); + } + + final CharSequence[] items = strings.toArray(new String[strings.size()]); + + AlertDialog.Builder builder = new AlertDialog.Builder(this); + builder.setTitle("Select printer") + .setItems(items, new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int which) { + Log.i(TAG, "Selected " + which); + bluetoothDevice = pairedDevices.get(which); + Log.i(TAG, "Selected " + + bluetoothDevice.getName() + " (" + + bluetoothDevice.getAddress() + ")" + ); + + lblPrinterName.setText(bluetoothDevice.getName()); + btnConnect.setEnabled(true); + } + }); + + builder.show(); + + } catch (Exception ex) { + ex.printStackTrace(); + } + + } + + void openBluetoothPrinter() { + lblStatus.setText("Connecting..."); + + // Open Bluetooth Printer + thread = new Thread(new Runnable() { + @Override + public void run() { + Handler refresh = new Handler(Looper.getMainLooper()); + try { + //Standard uuid from string // + UUID uuidSting = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb"); + bluetoothSocket = bluetoothDevice.createRfcommSocketToServiceRecord(uuidSting); + bluetoothSocket.connect(); + outputStream = bluetoothSocket.getOutputStream(); + inputStream = bluetoothSocket.getInputStream(); + + refresh.post(new Runnable() { + @Override + public void run() { + lblStatus.setText("Connected to bluetooth"); + setButtonsConnect(); + beginListenData(); + } + }); + + } catch (Exception ex) { + refresh.post(new Runnable() { + @Override + public void run() { + lblStatus.setText("Error while connecting"); + } + }); + } + } + }); + + thread.start(); + } + + void beginListenData() { + try { + + final Handler handler = new Handler(); + final byte delimiter = 10; + stopWorker = false; + readBufferPosition = 0; + readBuffer = new byte[1024]; + + thread = new Thread(new Runnable() { + @Override + public void run() { + + while (!Thread.currentThread().isInterrupted() && !stopWorker) { + try { + int byteAvailable = inputStream.available(); + if (byteAvailable > 0) { + byte[] packetByte = new byte[byteAvailable]; + inputStream.read(packetByte); + + for (int i = 0; i < byteAvailable; i++) { + byte b = packetByte[i]; + if (b == delimiter) { + byte[] encodedByte = new byte[readBufferPosition]; + System.arraycopy( + readBuffer, 0, + encodedByte, 0, + encodedByte.length + ); + final String data = new String(encodedByte, "US-ASCII"); + readBufferPosition = 0; + handler.post(new Runnable() { + @Override + public void run() { + lblStatus.setText(data); + } + }); + } else { + readBuffer[readBufferPosition++] = b; + } + } + } + } catch (Exception ex) { + stopWorker = true; + } + } + + } + }); + + thread.start(); + } catch (Exception ex) { + ex.printStackTrace(); + } + } + + // Disconnect Printer // + void disconnectBT() { + try { + stopWorker = true; + outputStream.close(); + inputStream.close(); + bluetoothSocket.close(); + lblStatus.setText("Printer Disconnected."); + setButtonsDisconnect(); + } catch (Exception ex) { + ex.printStackTrace(); + } + } + + // Printing Text to Bluetooth Printer // + void printData() { + try { + String msg = textBox.getText().toString(); + msg += "\n"; + outputStream.write(msg.getBytes()); + lblStatus.setText("Printing Text..."); + } catch (Exception ex) { + ex.printStackTrace(); + setButtonsDisconnect(); + lblStatus.setText("Error communication..."); + } + } + + void printCharsetTable() { + try { + lblStatus.setText("Printing table..."); + + byte[] array = new byte[237]; + int index=0; + for (int i = 20; i <= 255; i++) { + array[index++] = (byte)i; + } + array[236] = (byte)13; + + outputStream.write(array); + } catch (Exception ex) { + ex.printStackTrace(); + setButtonsDisconnect(); + lblStatus.setText("Error communication..."); + } + } + + void setCharset() { + try { + lblStatus.setText("Setting codepage"); + + outputStream.write(setCharsetCode); + outputStream.write((byte)0x12); + outputStream.flush(); + } catch (Exception ex) { + ex.printStackTrace(); + setButtonsDisconnect(); + lblStatus.setText("Error communication..."); + } + } + +} diff --git a/app/src/main/res/drawable-v24/ic_launcher_foreground.xml b/app/src/main/res/drawable-v24/ic_launcher_foreground.xml new file mode 100644 index 0000000..1f6bb29 --- /dev/null +++ b/app/src/main/res/drawable-v24/ic_launcher_foreground.xml @@ -0,0 +1,34 @@ + + + + + + + + + + + diff --git a/app/src/main/res/drawable/ic_launcher_background.xml b/app/src/main/res/drawable/ic_launcher_background.xml new file mode 100644 index 0000000..0d025f9 --- /dev/null +++ b/app/src/main/res/drawable/ic_launcher_background.xml @@ -0,0 +1,170 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml new file mode 100644 index 0000000..69ed481 --- /dev/null +++ b/app/src/main/res/layout/activity_main.xml @@ -0,0 +1,74 @@ + + + + + + + + + + + +