diff --git a/.idea/modules.xml b/.idea/modules.xml index 0ddf669..cf4f13d 100644 --- a/.idea/modules.xml +++ b/.idea/modules.xml @@ -2,6 +2,7 @@ + diff --git a/app/build.gradle b/app/build.gradle index 4af6b23..3b1e2a8 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -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' } diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index e6016bb..4385ca4 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -6,6 +6,7 @@ + >>> " + 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() diff --git a/app/src/main/java/xyz/mtfos/btdemo/PrinterBle.java b/app/src/main/java/xyz/mtfos/btdemo/PrinterBle.java index b74826c..93f5cba 100644 --- a/app/src/main/java/xyz/mtfos/btdemo/PrinterBle.java +++ b/app/src/main/java/xyz/mtfos/btdemo/PrinterBle.java @@ -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 getDataQueue() { return dataQueue; } diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 4e1b4e2..e7ba341 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -4,6 +4,78 @@ android:layout_height="match_parent" android:orientation="vertical"> + + + + + + + + + + + + + + + + + + + + + + + + + + +