update demo object

This commit is contained in:
Jay 2017-08-22 22:35:36 +08:00
parent 85f9b96137
commit dca769861e
3 changed files with 35 additions and 8 deletions

View File

@ -2,8 +2,9 @@
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/BTDemo.iml" filepath="$PROJECT_DIR$/BTDemo.iml" />
<module fileurl="file://D:\Project\ble-android-demo\BTDemo.iml" filepath="D:\Project\ble-android-demo\BTDemo.iml" />
<module fileurl="file://$PROJECT_DIR$/app/app.iml" filepath="$PROJECT_DIR$/app/app.iml" />
<module fileurl="file://$PROJECT_DIR$/ble-android-demo.iml" filepath="$PROJECT_DIR$/ble-android-demo.iml" />
</modules>
</component>
</project>

View File

@ -9,6 +9,7 @@ import android.os.Bundle
import android.os.Handler
import android.widget.Button
import android.widget.EditText
import kotlinx.android.synthetic.main.activity_main.*
import xyz.mtfos.btdemo.objectTool.bind
/**
@ -37,6 +38,19 @@ class MainActivity : Activity() {
ble?.setOnStateChangeListener {
System.out.println("Now State is >>>> " + it)
if(it == PrinterBle.CONNECTING) {
disconnectBtn.isEnabled = true
}
if(it == PrinterBle.CONNECTED) {
btn.isEnabled = true
connectBtn.isEnabled = false
disconnectBtn.isEnabled = true
}
if(it == PrinterBle.DISCONNECTED) {
btn.isEnabled = false
connectBtn.isEnabled = true
disconnectBtn.isEnabled = false
}
}
var permissionCheck: Int = 0
@ -50,7 +64,8 @@ class MainActivity : Activity() {
btn.setOnClickListener {
val txt: String = intxt.text.toString()
ble?.setAlign(PrinterBle.ALIGN_CENTER)?.addText(txt)
ble?.sendQueue()
}
connectBtn.setOnClickListener {

View File

@ -15,6 +15,7 @@ import android.bluetooth.le.ScanResult;
import android.bluetooth.le.ScanSettings;
import android.content.Context;
import android.os.Build;
import android.os.Handler;
import android.os.ParcelUuid;
import android.util.Log;
@ -71,6 +72,8 @@ public class PrinterBle {
private stateChangeListener mStateListener;
private notifyListener mNotifyListener;
private Handler mHandler = new Handler();
public interface stateChangeListener {
void onChange(int state);
}
@ -96,7 +99,12 @@ public class PrinterBle {
mState = CONNECTED;
Log.d(TAG, "Connected");
}
mStateListener.onChange(mState);
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
mStateListener.onChange(mState);
}
}, 100);
if(mState == CONNECTED) {
mBtGatt.discoverServices();
}
@ -137,6 +145,7 @@ public class PrinterBle {
break;
}
sending = false;
runQueue();
}
@Override
@ -283,7 +292,7 @@ public class PrinterBle {
Log.d(TAG, String.format("setVal: %s / sendVal: %s", String.valueOf(setVal), String.valueOf(sendVal)));
}
private void sendQueue() {
public void sendQueue() {
if (sendingQueue == null) sendingQueue = new ArrayList<>();
if (dataQueue == null) dataQueue = new ArrayList<>();
if (dataQueue.size() == 0) return;
@ -350,9 +359,10 @@ public class PrinterBle {
addText("\n");
}
public void addText(String txt) {
if (txt == null || txt.isEmpty()) return;
public PrinterBle addText(String txt) {
if (txt == null || txt.isEmpty()) return this;
dataQueue.add(txt);
return this;
}
public void addTextln(String txt) {
@ -360,7 +370,7 @@ public class PrinterBle {
addText(txt + "\n");
}
public void setAlign(int align) {
public PrinterBle setAlign(int align) {
String strAlign = "";
switch (align) {
case ALIGN_CENTER:
@ -371,9 +381,10 @@ public class PrinterBle {
case ALIGN_RIGHT:
strAlign = "rt";
default:
return;
return this;
}
addText("__a" + strAlign + "__");
return this;
}
public void setSize(int w, int h) {