add QrScanner , modify PrinterBle class

This commit is contained in:
Jay 2017-09-08 18:09:18 +08:00
parent 2197e6ab98
commit 8a081b144a
6 changed files with 264 additions and 116 deletions

View File

@ -2,6 +2,7 @@
<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://D:\Project\ble-android-demo\app\app.iml" filepath="D:\Project\ble-android-demo\app\app.iml" />

View File

@ -32,4 +32,5 @@ dependencies {
androidTestImplementation 'com.android.support.test:runner:1.0.0'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.0'
implementation 'org.apache.commons:commons-lang3:3.6'
implementation 'com.journeyapps:zxing-android-embedded:3.5.0'
}

View File

@ -6,6 +6,7 @@
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.CAMERA"/>
<application
android:allowBackup="true"

View File

@ -3,11 +3,16 @@ package xyz.mtfos.btdemo
import android.Manifest
import android.annotation.SuppressLint
import android.app.Activity
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Build
import android.os.Bundle
import android.os.Handler
import android.text.Editable
import android.widget.*
import com.google.zxing.integration.android.IntentIntegrator
import com.google.zxing.integration.android.IntentResult
import org.json.JSONObject
import xyz.mtfos.btdemo.objectTool.bind
/**
@ -26,6 +31,11 @@ class MainActivity : Activity() {
val alignBtn: Button by bind(R.id.btn_align)
val sizeBtn: Button by bind(R.id.btn_size)
val inmac: EditText by bind(R.id.inmac)
val inservice: EditText by bind(R.id.inservice)
val incharacteristic: EditText by bind(R.id.incharacteristic)
val scanSetting: Button by bind(R.id.scansetting)
val addHR: Button by bind(R.id.addHR)
val addNL: Button by bind(R.id.addNL)
val addTAB: Button by bind(R.id.addTAB)
@ -51,7 +61,7 @@ class MainActivity : Activity() {
connectBtn.isEnabled = true
disconnectBtn.isEnabled = false
ble = PrinterBle(this@MainActivity, "B8:27:EB:21:C2:1A", "dd535b71-8f05-4e30-beb0-6fa7ea3dfc3e", "00000001-8f05-4e30-beb0-6fa7ea3dfc3e")
ble = PrinterBle(this@MainActivity)
ble?.setOnStateChangeListener {
System.out.println("Now State is >>>> " + it)
@ -76,6 +86,10 @@ class MainActivity : Activity() {
}
}
scanSetting.setOnClickListener {
IntentIntegrator(this@MainActivity).initiateScan()
}
ble?.setOnNotifyListener {
System.out.println("Get Notify :::::: " + it)
}
@ -107,8 +121,9 @@ class MainActivity : Activity() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
permissionCheck += this@MainActivity.checkSelfPermission(android.Manifest.permission.ACCESS_COARSE_LOCATION)
permissionCheck += this@MainActivity.checkSelfPermission(android.Manifest.permission.ACCESS_FINE_LOCATION)
permissionCheck += this@MainActivity.checkSelfPermission(android.Manifest.permission.CAMERA)
if (permissionCheck != 0) {
requestPermissions(arrayOf(Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION), 111)
requestPermissions(arrayOf(Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.CAMERA), 111)
}
}
@ -138,6 +153,9 @@ class MainActivity : Activity() {
}
connectBtn.setOnClickListener {
ble?.macAddr = inmac.text.toString()
ble?.serviceUUID = inservice.text.toString()
ble?.characteristicUUID = incharacteristic.text.toString()
scanConnect()
}
disconnectBtn.setOnClickListener {
@ -149,6 +167,32 @@ class MainActivity : Activity() {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
val qrRes: IntentResult? = IntentIntegrator.parseActivityResult(requestCode, resultCode, data)
if (qrRes != null) {
val content: String? = qrRes?.contents
if (content != null) {
System.out.println("GET QRCODE :: $content")
try {
val json: JSONObject = JSONObject(content)
if(json.has("mac") && json["mac"] is String){
inmac.text = Editable.Factory.getInstance().newEditable(json.getString("mac"))
}
if(json.has("service") && json["service"] is String) {
inservice.text = Editable.Factory.getInstance().newEditable(json.getString("service"))
}
if(json.has("characteristic") && json["characteristic"] is String) {
incharacteristic.text = Editable.Factory.getInstance().newEditable(json.getString("characteristic"))
}
} catch (e: Exception) {
e.printStackTrace()
Toast.makeText(this@MainActivity, "Parser Error", Toast.LENGTH_SHORT).show()
}
}
}
}
override fun onStop() {
super.onStop()
disconnect()

View File

@ -89,6 +89,10 @@ public class PrinterBle {
void onNotify(String msg);
}
public PrinterBle(Context ctx) {
this(ctx, "", "", "");
}
public PrinterBle(Context ctx, String macAddr, String serviceUUID, String characteristicUUID) {
this.ctx = ctx;
this.macAddr = macAddr;
@ -127,9 +131,9 @@ public class PrinterBle {
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
super.onServicesDiscovered(gatt, status);
if (status == BluetoothGatt.GATT_SUCCESS) {
if (status == BluetoothGatt.GATT_SUCCESS && !serviceUUID.isEmpty()) {
BluetoothGattService btService = mBtGatt.getService(UUID.fromString(serviceUUID));
if (btService != null) {
if (btService != null && !characteristicUUID.isEmpty()) {
printerCharacteristic = btService.getCharacteristic(UUID.fromString(characteristicUUID));
// set notify enable
mBtGatt.setCharacteristicNotification(printerCharacteristic, true);
@ -242,7 +246,7 @@ public class PrinterBle {
};
public void startScan() {
if (mAdapter == null) return;
if (mAdapter == null || macAddr.isEmpty() || serviceUUID.isEmpty() || characteristicUUID.isEmpty()) return;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && mLeScanner == null) return;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
@ -439,6 +443,30 @@ public class PrinterBle {
return this;
}
public String getMacAddr() {
return macAddr;
}
public void setMacAddr(String macAddr) {
this.macAddr = macAddr;
}
public String getServiceUUID() {
return serviceUUID;
}
public void setServiceUUID(String serviceUUID) {
this.serviceUUID = serviceUUID;
}
public String getCharacteristicUUID() {
return characteristicUUID;
}
public void setCharacteristicUUID(String characteristicUUID) {
this.characteristicUUID = characteristicUUID;
}
public ArrayList<String> getDataQueue() {
return dataQueue;
}

View File

@ -4,6 +4,78 @@
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
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="wrap_content"
android:text="BTMac:" />
<EditText
android:id="@+id/inmac"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="input bluetooth mac" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BTService:" />
<EditText
android:id="@+id/inservice"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="input service uuid" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BTCharacteristic:" />
<EditText
android:id="@+id/incharacteristic"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="input characteristic uuid" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:orientation="horizontal">
<Button
android:id="@+id/scansetting"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="scanSetting"
android:textAllCaps="false" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
@ -24,6 +96,10 @@
android:textAllCaps="false" />
</LinearLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/mainlay"
android:layout_width="match_parent"
@ -145,18 +221,15 @@
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" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
</LinearLayout>
</LinearLayout>