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"> <project version="4">
<component name="ProjectModuleManager"> <component name="ProjectModuleManager">
<modules> <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$/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> </modules>
</component> </component>
</project> </project>

View File

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

View File

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