This commit is contained in:
Jay 2017-08-31 17:11:15 +08:00
parent 85d8586aed
commit 74159a1b47
3 changed files with 228 additions and 35 deletions

View File

@ -7,29 +7,46 @@ import android.content.pm.PackageManager
import android.os.Build
import android.os.Bundle
import android.os.Handler
import android.widget.Button
import android.widget.EditText
import kotlinx.android.synthetic.main.activity_main.*
import android.widget.*
import xyz.mtfos.btdemo.objectTool.bind
/**
* Created by jay on 2017/8/9.
*/
class MainActivity : Activity() {
val mainLay: LinearLayout by bind(R.id.mainlay)
val intxt: EditText by bind(R.id.intxt)
val btn: Button by bind(R.id.bt)
val connectBtn: Button by bind(R.id.connect)
val disconnectBtn: Button by bind(R.id.disconnect)
var ble: PrinterBle? = null
val alignSpinner: Spinner by bind(R.id.align)
val sizeSpinner: Spinner by bind(R.id.size)
val alignBtn: Button by bind(R.id.btn_align)
val sizeBtn: Button by bind(R.id.btn_size)
val addHR: Button by bind(R.id.addHR)
val addNL: Button by bind(R.id.addNL)
val addTAB: Button by bind(R.id.addTAB)
val addTXT: Button by bind(R.id.addTXT)
val preview: TextView by bind(R.id.preview)
val uiHandler: Handler = Handler()
var th: Thread? = null
var thrun: Boolean = false
var isInit: Boolean = false
val alignArr: Array<String> = arrayOf("Left", "Center", "Right")
val sizeArr: Array<String> = arrayOf("1,1", "1,2", "2,1", "2,2")
@SuppressLint("WrongConstant")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
mainLay.visibility = LinearLayout.GONE
btn.isEnabled = false
connectBtn.isEnabled = true
disconnectBtn.isEnabled = false
@ -45,11 +62,17 @@ class MainActivity : Activity() {
btn.isEnabled = true
connectBtn.isEnabled = false
disconnectBtn.isEnabled = true
uiHandler.post {
mainLay.visibility = LinearLayout.VISIBLE
}
}
if (it == PrinterBle.DISCONNECTED) {
btn.isEnabled = false
connectBtn.isEnabled = true
disconnectBtn.isEnabled = false
uiHandler.post {
mainLay.visibility = LinearLayout.GONE
}
}
}
@ -57,6 +80,29 @@ class MainActivity : Activity() {
System.out.println("Get Notify :::::: " + it)
}
alignSpinner.adapter = ArrayAdapter<String>(this@MainActivity, android.R.layout.simple_spinner_item, alignArr)
alignBtn.setOnClickListener {
val pos = alignSpinner.selectedItemPosition
when (pos) {
0 -> ble?.setAlign(PrinterBle.ALIGN_LEFT)
1 -> ble?.setAlign(PrinterBle.ALIGN_CENTER)
2 -> ble?.setAlign(PrinterBle.ALIGN_RIGHT)
}
refreshPreview()
}
sizeSpinner.adapter = ArrayAdapter<String>(this@MainActivity, android.R.layout.simple_spinner_item, sizeArr)
sizeBtn.setOnClickListener {
val pos = sizeSpinner.selectedItemPosition
when (pos) {
0 -> ble?.setSize(1, 1)
1 -> ble?.setSize(1, 2)
2 -> ble?.setSize(2, 1)
3 -> ble?.setSize(2, 2)
}
refreshPreview()
}
var permissionCheck: Int = 0
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
permissionCheck += this@MainActivity.checkSelfPermission(android.Manifest.permission.ACCESS_COARSE_LOCATION)
@ -66,17 +112,28 @@ class MainActivity : Activity() {
}
}
btn.setOnClickListener {
val txt: String = intxt.text.toString()
ble?.setSize(2,1)
ble?.setAlign(PrinterBle.ALIGN_CENTER)
ble?.addTextln("MTFoS Shop")
ble?.resetSize()
ble?.setAlign(PrinterBle.ALIGN_RIGHT)
ble?.addTextln("MTFoS Shop")
addHR.setOnClickListener {
ble?.addHR()
ble?.setAlign(PrinterBle.ALIGN_LEFT)
refreshPreview()
}
addNL.setOnClickListener {
ble?.textNewLine()
refreshPreview()
}
addTAB.setOnClickListener {
ble?.textTab()
refreshPreview()
}
addTXT.setOnClickListener {
val txt: String = intxt.text.toString()
ble?.addText(txt)
refreshPreview()
}
btn.setOnClickListener {
ble?.sendQueue()
}
@ -111,4 +168,12 @@ class MainActivity : Activity() {
ble?.disconnect()
}
}
fun refreshPreview() {
val data = ble?.dataQueue ?: return
preview.text = ""
for (str: String in data) {
preview.text = preview.text.toString() + "\n" + str
}
}
}

View File

@ -426,4 +426,8 @@ public class PrinterBle {
public void resetSize() {
setSize(1, 1);
}
public ArrayList<String> getDataQueue() {
return dataQueue;
}
}

View File

@ -8,31 +8,155 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/connect"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/connect"
android:text="Connect"
android:textAllCaps="false" />
<Button
android:id="@+id/disconnect"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/disconnect"
android:text="Disconnect"
android:textAllCaps="false" />
</LinearLayout>
<EditText
<LinearLayout
android:id="@+id/mainlay"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:text="Align: " />
<android.support.v7.widget.AppCompatSpinner
android:id="@+id/align"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
<Button
android:id="@+id/btn_align"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Write"
android:textAllCaps="false" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:text="Size: " />
<android.support.v7.widget.AppCompatSpinner
android:id="@+id/size"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
<Button
android:id="@+id/btn_size"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Write"
android:textAllCaps="false" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/addHR"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="分隔線" />
<Button
android:id="@+id/addNL"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="換行" />
<Button
android:id="@+id/addTAB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TAB" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="@+id/intxt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="輸入傳送字串" />
<Button
android:id="@+id/addTXT"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="addTXT"
android:textAllCaps="false" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:id="@+id/bt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send"
android:textAllCaps="false" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/preview"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</ScrollView>
</LinearLayout>
</LinearLayout>
</LinearLayout>