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,18 +62,47 @@ 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
}
}
}
ble?.setOnNotifyListener{
ble?.setOnNotifyListener {
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

@ -100,10 +100,10 @@ public class PrinterBle {
super.onConnectionStateChange(gatt, status, newState);
Log.d(TAG, "Connect State Change to " + status + " new State : " + newState);
if (status == BluetoothGatt.GATT_SUCCESS) {
if(mState != DISCONNECTED) {
if (mState != DISCONNECTED) {
mState = CONNECTED;
Log.d(TAG, "Connected");
if(!isConnected) {
if (!isConnected) {
isConnected = true;
mBtGatt.discoverServices();
}
@ -251,7 +251,7 @@ public class PrinterBle {
}
mState = SCANNING;
if(this.mStateListener != null) {
if (this.mStateListener != null) {
this.mStateListener.onChange(mState);
}
}
@ -268,7 +268,7 @@ public class PrinterBle {
if (mState == SCANNING) {
mState = DISCONNECTED;
if(this.mStateListener != null) {
if (this.mStateListener != null) {
this.mStateListener.onChange(mState);
}
}
@ -292,7 +292,7 @@ public class PrinterBle {
if (device == null) return;
mBtGatt = device.connectGatt(ctx, false, mBtGattCB);
mState = CONNECTING;
if(this.mStateListener != null){
if (this.mStateListener != null) {
this.mStateListener.onChange(mState);
}
}
@ -415,7 +415,7 @@ public class PrinterBle {
return addText(String.format("__&s%d,%d__", w, h));
}
public PrinterBle addHR(){
public PrinterBle addHR() {
int align = this.lastAlign;
setAlign(ALIGN_CENTER);
addTextln(StringUtils.repeat('-', 20));
@ -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:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/connect"
android:text="Connect"
android:textAllCaps="false"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Connect"
android:textAllCaps="false" />
<Button
android:id="@+id/disconnect"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Disconnect"
android:textAllCaps="false"/>
android:textAllCaps="false" />
</LinearLayout>
<EditText
<LinearLayout
android:id="@+id/mainlay"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/intxt"
android:hint="輸入傳送字串"/>
android:layout_height="match_parent"
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
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>