diff --git a/app/build.gradle b/app/build.gradle index 0be2013..bdf287e 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -6,8 +6,8 @@ android { applicationId "com.csl.cs108ademoapp" minSdkVersion 19 targetSdkVersion 30 - versionCode 32 - versionName "2.2.0" + versionCode 34 + versionName "2.3.0" } } diff --git a/app/libs/cs108ademo-20220117-v2.3.0-cs108library4a-debug.zip b/app/libs/cs108ademo-20220117-v2.3.0-cs108library4a-debug.zip new file mode 100644 index 0000000..4145cc2 Binary files /dev/null and b/app/libs/cs108ademo-20220117-v2.3.0-cs108library4a-debug.zip differ diff --git a/app/release/cs108ademo-20210604-v2.0.0-app-release.zip b/app/release/cs108ademo-20210604-v2.0.0-app-release.zip deleted file mode 100644 index 04a74ae..0000000 Binary files a/app/release/cs108ademo-20210604-v2.0.0-app-release.zip and /dev/null differ diff --git a/app/release/cs108ademo-20220117-v2.3.0-app-debug.zip b/app/release/cs108ademo-20220117-v2.3.0-app-debug.zip new file mode 100644 index 0000000..600edce Binary files /dev/null and b/app/release/cs108ademo-20220117-v2.3.0-app-debug.zip differ diff --git a/app/release/cs108ademo-20220117-v2.3.0-app-release.zip b/app/release/cs108ademo-20220117-v2.3.0-app-release.zip new file mode 100644 index 0000000..382f36a Binary files /dev/null and b/app/release/cs108ademo-20220117-v2.3.0-app-release.zip differ diff --git a/app/release/output-metadata.json b/app/release/output-metadata.json deleted file mode 100644 index f4aba6a..0000000 --- a/app/release/output-metadata.json +++ /dev/null @@ -1,18 +0,0 @@ -{ - "version": 2, - "artifactType": { - "type": "APK", - "kind": "Directory" - }, - "applicationId": "com.csl.cs108ademoapp", - "variantName": "processReleaseResources", - "elements": [ - { - "type": "SINGLE", - "filters": [], - "versionCode": 29, - "versionName": "2.1.1", - "outputFile": "app-release.apk" - } - ] -} \ No newline at end of file diff --git a/app/src/main/java/com/csl/cs108ademoapp/fragments/ConnectionFragment.java b/app/src/main/java/com/csl/cs108ademoapp/fragments/ConnectionFragment.java index f3779de..bc99563 100644 --- a/app/src/main/java/com/csl/cs108ademoapp/fragments/ConnectionFragment.java +++ b/app/src/main/java/com/csl/cs108ademoapp/fragments/ConnectionFragment.java @@ -73,7 +73,7 @@ public void onItemClick(AdapterView parent, View view, int position, long id) if (mCs108Library4a.isBleConnected() && readerDevice.isConnected() && (readerDevice.getSelected() || false)) { mCs108Library4a.disconnect(false); - readerDevice.setConnected(false); + readersList.clear(); } else if (mCs108Library4a.isBleConnected() == false && readerDevice.getSelected() == false) { boolean validStart = false; if (deviceConnectTask == null) { @@ -95,15 +95,17 @@ public void onItemClick(AdapterView parent, View view, int position, long id) } } - if (readerDevice.getSelected()) readerDevice.setSelected(false); - else readerDevice.setSelected(true); - readersList.set(position, readerDevice); - for (int i = 0; i < readersList.size(); i++) { - if (i != position) { - ReaderDevice readerDevice1 = readersList.get(i); - if (readerDevice1.getSelected()) { - readerDevice1.setSelected(false); - readersList.set(i, readerDevice1); + if (readersList.size() > position) { + if (readerDevice.getSelected()) readerDevice.setSelected(false); + else readerDevice.setSelected(true); + readersList.set(position, readerDevice); + for (int i = 0; i < readersList.size(); i++) { + if (i != position) { + ReaderDevice readerDevice1 = readersList.get(i); + if (readerDevice1.getSelected()) { + readerDevice1.setSelected(false); + readersList.set(i, readerDevice1); + } } } } @@ -157,7 +159,7 @@ public void run() { } if (operating == false) { deviceScanTask = new DeviceScanTask(); - deviceScanTask.execute(); mCs108Library4a.appendToLog("Started DeviceScanTask"); + deviceScanTask.execute(); } mHandler.postDelayed(checkRunnable, 5000); } @@ -291,7 +293,7 @@ protected void onPreExecute() { MainActivity.mCs108Library4a.appendToLog("start of Connection with mrfidToWriteSize = " + mCs108Library4a.mrfidToWriteSize()); mCs108Library4a.connect(connectingDevice); - waitTime = 20; + waitTime = 30; setting = -1; progressDialog = new CustomProgressDialog(getActivity(), prgressMsg); progressDialog.show(); @@ -333,7 +335,7 @@ protected void onCancelled(Integer result) { Toast.makeText(getActivity().getApplicationContext(), getResources().getString(R.string.error_bluetooth_connection_failed), Toast.LENGTH_SHORT).show(); } super.onCancelled(); - mCs108Library4a.disconnect(false); mCs108Library4a.appendToLog("done"); + mCs108Library4a.disconnect(false); bConnecting = false; } diff --git a/app/src/main/java/com/csl/cs108ademoapp/fragments/HomeFragment.java b/app/src/main/java/com/csl/cs108ademoapp/fragments/HomeFragment.java index f511bb4..764d3b2 100644 --- a/app/src/main/java/com/csl/cs108ademoapp/fragments/HomeFragment.java +++ b/app/src/main/java/com/csl/cs108ademoapp/fragments/HomeFragment.java @@ -42,7 +42,7 @@ public void onActivityCreated(Bundle savedInstanceState) { actionBar.setTitle(R.string.title_activity_home); } MainActivity.mDid = null; - if (MainActivity.sharedObjects.versioinWarningShown == false) + if (true || MainActivity.sharedObjects.versioinWarningShown == false) mHandler.post(runnableConfiguring); } diff --git a/app/src/main/java/com/csl/cs108ademoapp/fragments/InventoryRfidiMultiFragment.java b/app/src/main/java/com/csl/cs108ademoapp/fragments/InventoryRfidiMultiFragment.java index 749c6dc..45278f7 100644 --- a/app/src/main/java/com/csl/cs108ademoapp/fragments/InventoryRfidiMultiFragment.java +++ b/app/src/main/java/com/csl/cs108ademoapp/fragments/InventoryRfidiMultiFragment.java @@ -265,6 +265,7 @@ public void onPause() { @Override public void onDestroy() { + mHandler.removeCallbacks(runnableCheckReady); MainActivity.mCs108Library4a.setNotificationListener(null); if (inventoryRfidTask != null) { if (DEBUG) MainActivity.mCs108Library4a.appendToLog("InventoryRfidiMultiFragment().onDestory(): VALID inventoryRfidTask"); @@ -354,6 +355,7 @@ void startStopHandler(boolean buttonTrigger) { return; } else if (MainActivity.mCs108Library4a.mrfidToWriteSize() != 0) { Toast.makeText(MainActivity.mContext, R.string.toast_not_ready, Toast.LENGTH_SHORT).show(); + mHandler.post(runnableCheckReady); return; } if (bAdd2End) rfidListView.setTranscriptMode(AbsListView.TRANSCRIPT_MODE_ALWAYS_SCROLL); @@ -568,4 +570,20 @@ void startInventoryTask() { } inventoryRfidTask.execute(); } + + private final Runnable runnableCheckReady = new Runnable() { + @Override + public void run() { + if (MainActivity.mCs108Library4a.mrfidToWriteSize() != 0) { + button.setEnabled(false); + button.setText("Please wait"); + MainActivity.mCs108Library4a.setNotificationListener(null); + mHandler.postDelayed(runnableCheckReady, 500); + } else { + button.setText("Start"); + button.setEnabled(true); + setNotificationListener(); + } + } + }; } diff --git a/build.gradle b/build.gradle index db91b1b..4e54e4c 100644 --- a/build.gradle +++ b/build.gradle @@ -6,7 +6,7 @@ buildscript { google() } dependencies { - classpath 'com.android.tools.build:gradle:4.1.0' + classpath 'com.android.tools.build:gradle:7.0.3' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files diff --git a/cs108library4a/build.gradle b/cs108library4a/build.gradle index 41776bb..ec928b1 100644 --- a/cs108library4a/build.gradle +++ b/cs108library4a/build.gradle @@ -5,9 +5,9 @@ android { defaultConfig { minSdkVersion 19 targetSdkVersion 30 - versionCode 32 - versionName "2.2.0" - buildConfigField 'String', 'VERSION_NAME', "\"2.2.0\"" + versionCode 34 + versionName "2.3.0" + buildConfigField 'String', 'VERSION_NAME', "\"2.3.0\"" testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner' } diff --git a/cs108library4a/libs/cs108ademo-20210604_v2.0.0-cs108library4a-debug.zip b/cs108library4a/libs/cs108ademo-20210604_v2.0.0-cs108library4a-debug.zip deleted file mode 100644 index 0aba745..0000000 Binary files a/cs108library4a/libs/cs108ademo-20210604_v2.0.0-cs108library4a-debug.zip and /dev/null differ diff --git a/cs108library4a/src/main/java/com/csl/cs108library4a/BleConnector.java b/cs108library4a/src/main/java/com/csl/cs108library4a/BleConnector.java index 7dd5731..b92e36b 100644 --- a/cs108library4a/src/main/java/com/csl/cs108library4a/BleConnector.java +++ b/cs108library4a/src/main/java/com/csl/cs108library4a/BleConnector.java @@ -34,8 +34,6 @@ import java.util.List; import java.util.UUID; -import static android.bluetooth.BluetoothDevice.BOND_NONE; -import static android.bluetooth.BluetoothDevice.PHY_LE_2M; import static android.content.ContentValues.TAG; import static android.content.Context.LAYOUT_INFLATER_SERVICE; import static android.content.Context.LOCATION_SERVICE; @@ -51,12 +49,13 @@ ReaderDevice getmBluetoothDevice() { return mBluetoothDevice; } - private BluetoothAdapter mBluetoothAdapter; - private BluetoothGatt mBluetoothGatt; + BluetoothManager mBluetoothManager; + BluetoothAdapter mBluetoothAdapter; + BluetoothGatt mBluetoothGatt; private BluetoothLeScanner mleScanner; - private int mBluetoothProfile; - boolean isBleConnected() { return mBluetoothProfile == BluetoothProfile.STATE_CONNECTED && mReaderStreamOutCharacteristic != null; } + int mBluetoothConnectionState; + boolean isBleConnected() { return mBluetoothConnectionState == BluetoothProfile.STATE_CONNECTED && mReaderStreamOutCharacteristic != null; } private boolean mScanning = false; boolean isBleScanning() { return mScanning; } @@ -71,7 +70,7 @@ ReaderDevice getmBluetoothDevice() { private boolean characteristicListRead = false; boolean isCharacteristicListRead() { return characteristicListRead; } - private BluetoothGattCharacteristic mReaderStreamOutCharacteristic; + BluetoothGattCharacteristic mReaderStreamOutCharacteristic; private BluetoothGattCharacteristic mReaderStreamInCharacteristic; private long mStreamWriteCount, mStreamWriteCountOld; private boolean _readCharacteristic_in_progress; @@ -118,25 +117,32 @@ long getStreamInAddTime() { @Override public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { super.onConnectionStateChange(gatt, status, newState); + appendToLog("abcc: newState = " + newState); if (gatt != mBluetoothGatt) { - if (DEBUG) appendToLog("INVALID mBluetoothGatt"); -// } else if (status != BluetoothGatt.GATT_SUCCESS) { -// if (DEBUG) appendToLog("NOT GATT_SUCCESS, newState=" + newState); + appendToLog("abcc mismatched mBluetoothGatt = " + (gatt != mBluetoothGatt) + ", status = " + status); } else { - mBluetoothProfile = newState; + mBluetoothConnectionState = newState; switch (newState) { case BluetoothProfile.STATE_DISCONNECTED: - forcedDisconnect(false); - if (DEBUG) appendToLog("state=Disconnected"); + if (DEBUG) appendToLog("abcc state=Disconnected with status = " + status); break; case BluetoothProfile.STATE_CONNECTED: - if (disconnectRunning) break; + if (DEBUG) appendToLog("abcc state=Connected with status = " + status); + if (disconnectRunning) { + appendToLog("abcc disconnectRunning !!!"); + break; + } mStreamWriteCount = mStreamWriteCountOld = 0; _readCharacteristic_in_progress = _writeCharacteristic_in_progress = false; + if (bDiscoverStarted) { + appendToLog("abc discovery has been started before"); + break; + } appendToLog("Start discoverServices"); if (mBluetoothGatt.discoverServices()) { - if (DEBUG) appendToLog("state=Connected. discoverServices starts"); + bDiscoverStarted = true; + if (DEBUG) appendToLog("state=Connected. discoverServices starts with status = " + status); } else { if (DEBUG) appendToLog("state=Connected. discoverServices FAIL"); } @@ -151,6 +157,7 @@ public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState } } + boolean bDiscoverStarted = false; @Override public void onServicesDiscovered(BluetoothGatt gatt, int status) { super.onServicesDiscovered(gatt, status); @@ -330,16 +337,15 @@ public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristi onCharacteristicWriteFailue++; if (DEBUG) appendToLog("status=" + status); } else { + onCharacteristicWriteFailue = 0; if (DEBUG_BTDATA) appendToLog("characteristic=" + characteristic.getUuid().toString().substring(4, 8) + ", sent " + (mStreamWriteCount - mStreamWriteCountOld) + " bytes"); _writeCharacteristic_in_progress = false; } } - private int writeBleCounter = 0; private int writeBleFailure = 0; private int onCharacteristicWriteFailue = 0; boolean writeBleStreamOut(byte[] value) { - //appendToLog("Start"); if (mBluetoothGatt == null) { if (DEBUG) appendToLog("ERROR with NULL mBluetoothGatt"); } else if (mReaderStreamOutCharacteristic == null) { @@ -348,30 +354,23 @@ boolean writeBleStreamOut(byte[] value) { if (true) appendToLog("isBleBusy() = " + isBleBusy() + ", characteristicListRead = " + characteristicListRead); } else { mReaderStreamOutCharacteristic.setValue(value); - if (((writeBleCounter / 100) * 100) == writeBleCounter) { - if (true) appendToLog("writeBleCounter = " + writeBleCounter + ", writeBleFailure = " + writeBleFailure + ", onCharacteristicWriteFailue = " + onCharacteristicWriteFailue); - if (writeBleCounter == 1000) { - writeBleCounter = 0; - writeBleFailure = 0; - onCharacteristicWriteFailue = 0; - } - } - writeBleCounter++; boolean bValue = mBluetoothGatt.writeCharacteristic(mReaderStreamOutCharacteristic); - if (bValue == false) { - writeBleFailure++; - if (true) appendToLog("writeCharacteristic(): ERROR for " + byteArrayToString(value) + ", writeBleFailure = " + writeBleFailure); - if (writeBleFailure > 5) { - appendToLogView("writeBleFailure is too much. mReaderStreamOutCharacteristic is set NULL to assume disconnection !!!"); - mReaderStreamOutCharacteristic = null; - } - } else { + if (bValue == false) writeBleFailure++; + else { + writeBleFailure = 0; if (DEBUG_BTDATA0) appendToLog(byteArrayToString(value)); _writeCharacteristic_in_progress = true; mStreamWriteCountOld = mStreamWriteCount; mStreamWriteCount += value.length; return true; } + if (writeBleFailure != 0 || onCharacteristicWriteFailue != 0) { + appendToLogView("failure in writeCharacteristic(" + byteArrayToString(value) + "), writeBleFailure = " + writeBleFailure + ", onCharacteristicWriteFailue = " + onCharacteristicWriteFailue); + if (writeBleFailure > 5 || onCharacteristicWriteFailue > 5) { + appendToLogView("writeBleFailure is too much. start disconnect !!!"); + disconnect(); //mReaderStreamOutCharacteristic = null; + } + } } return false; } @@ -387,7 +386,7 @@ public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteris } } else if (!characteristic.equals(mReaderStreamInCharacteristic)) { if (DEBUG) utility.appendToLogRunnable("onCharacteristicChanged(): characteristic is not ReaderSteamIn"); - } else if (mBluetoothProfile == BluetoothProfile.STATE_DISCONNECTED) { + } else if (mBluetoothConnectionState == BluetoothProfile.STATE_DISCONNECTED) { streamInBufferHead = 0; streamInBufferTail = 0; streamInBufferSize = 0; @@ -483,7 +482,7 @@ public void onReliableWriteCompleted(BluetoothGatt gatt, int status) { PackageManager mPackageManager = mContext.getPackageManager(); if (mPackageManager.hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) { - BluetoothManager mBluetoothManager = (BluetoothManager) mContext.getSystemService(Context.BLUETOOTH_SERVICE); + mBluetoothManager = (BluetoothManager) mContext.getSystemService(Context.BLUETOOTH_SERVICE); mBluetoothAdapter = mBluetoothManager.getAdapter(); if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { boolean isBle5 = mBluetoothAdapter.isLeCodedPhySupported(); @@ -598,7 +597,7 @@ public void onClick(View v) { } } if (enable == false) { - if (DEBUG) appendToLog("scanLeDevice(" + enable + ") with mScanCallBack is " + (mScanCallBack != null ? "VALID" : "INVALID")); + if (DEBUG) appendToLog("abcc scanLeDevice(" + enable + ") with mScanCallBack is " + (mScanCallBack != null ? "VALID" : "INVALID")); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { if (mScanCallBack != null) mleScanner.stopScan(mScanCallBack); } else { @@ -681,6 +680,7 @@ public void onReceive(Context context, Intent intent) { }; */ boolean connectBle(ReaderDevice readerDevice) { + appendToLog("abcc: start connecting " + readerDevice.getName()); if (readerDevice == null) { if (DEBUG) appendToLog("with NULL readerDevice"); } else { @@ -691,30 +691,12 @@ boolean connectBle(ReaderDevice readerDevice) { if (DEBUG) appendToLog("connectBle(" + address + ") with DISABLED mBluetoothAdapter"); } else { utility.debugFileSetup(); - mHandler.removeCallbacks(mDisconnectRunnable); utility.setReferenceTimeMs(); if (DEBUG) appendToLog("connectBle(" + address + "): connectGatt starts"); -/* - IntentFilter intentFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND); - intentFilter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED); - mContext.registerReceiver(myReceiver, intentFilter); -*/ - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { //android 8 = api level 26 - BluetoothDevice mBluetoothDevice = mBluetoothAdapter.getRemoteDevice(address); - appendToLog("mBluetoothDevice.getBondState() = " + mBluetoothDevice.getBondState()); - boolean bOkBond = true; - if (false && mBluetoothDevice.getBondState() == BOND_NONE) { - bOkBond = mBluetoothDevice.createBond(); - if (bOkBond) appendToLog("sucess to creatBond"); - else appendToLog("failed to createBond"); - } - appendToLog("writeBleStreamOut: android 8 or above sets PHY_LE_2M"); - if (bOkBond) mBluetoothGatt = mBluetoothDevice.connectGatt(mContext, false, this, BluetoothDevice.TRANSPORT_LE, PHY_LE_2M); - else return false; - } else { - mBluetoothGatt = mBluetoothAdapter.getRemoteDevice(address).connectGatt(mContext, false, this); - } - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + mBluetoothConnectionState = -1; + mBluetoothGatt = mBluetoothAdapter.getRemoteDevice(address).connectGatt(mContext, false, this); + if (mBluetoothGatt != null) mBluetoothGattActive = true; + if (false && Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { if (true) { mBluetoothGatt.requestConnectionPriority(BluetoothGatt.CONNECTION_PRIORITY_HIGH); appendToLog("Stream Set to HIGH"); @@ -733,49 +715,65 @@ boolean connectBle(ReaderDevice readerDevice) { } void disconnect() { - if (mBluetoothAdapter == null) { - if (DEBUG) appendToLog("with NULL mBluetoothAdapter"); - } else if (!mBluetoothAdapter.isEnabled()) { - if (DEBUG) appendToLog("DISABLED mBluetoothAdapter"); - } else if (mBluetoothGatt == null) { + appendToLog("abcc: start disconnect "); + if (mBluetoothGatt == null) { if (DEBUG) appendToLog("NULL mBluetoothGatt"); } else { utility.debugFileClose(); mReaderStreamOutCharacteristic = null; - mBluetoothGatt.disconnect(); mHandler.removeCallbacks(mDisconnectRunnable); - mHandler.postDelayed(mDisconnectRunnable, 200); disconnectRunning = true; - if (DEBUG) appendToLog("done"); + mHandler.post(mDisconnectRunnable); disconnectRunning = true; + if (DEBUG) appendToLog("abcc done and start mDisconnectRunnable"); } } - void forcedDisconnect(boolean bForce) { - if (bForce) forcedDisconnect1(); + boolean mBluetoothGattActive = false; + boolean forcedDisconnect1() { mHandler.removeCallbacks(mReadRssiRunnable); mHandler.removeCallbacks(mReadCharacteristicRunnable); - } - void forcedDisconnect1() { - if (mBluetoothGatt != null) { appendToLog("mDisconnectRunnable(): Close the GATT"); mBluetoothGatt.close(); } - mBluetoothProfile = BluetoothProfile.STATE_DISCONNECTED; + if (mBluetoothGatt != null) { + if (mBluetoothGattActive) { + appendToLog("abcc mDisconnectRunnable(): close mBluetoothGatt"); + mBluetoothGatt.close(); + mBluetoothGattActive = false; + } else { + appendToLog("abcc mDisconnectRunnable(): Null mBluetoothGatt"); + mBluetoothGatt = null; + return true; + } + } + return false; + //mBluetoothConnectionState = -1; } private boolean disconnectRunning = false; + BluetoothDevice bluetoothDeviceConnectOld; private final Runnable mDisconnectRunnable = new Runnable() { @Override public void run() { - if (mBluetoothProfile != BluetoothProfile.STATE_DISCONNECTED) { - forcedDisconnect(true); - if (DEBUG) appendToLog("mDisconnectRunnable(): disconnect"); - mHandler.postDelayed(mDisconnectRunnable, 500); - } else { - forcedDisconnect1(); - if (DEBUG) appendToLog("mDisconnectRunnable(): END"); + boolean done = false; + int bGattConnection = -1; + if (bluetoothDeviceConnectOld != null) bGattConnection = mBluetoothManager.getConnectionState(bluetoothDeviceConnectOld, BluetoothProfile.GATT); + if (DEBUG) appendToLog("abcc DisconnectRunnable(): disconnect with mBluetoothConnectionState = " + mBluetoothConnectionState + ", gattConnection = " + bGattConnection); + if (mBluetoothConnectionState < 0) { + appendToLog("abcc DisconnectRunnable(): start mBluetoothGatt.disconnect"); + mBluetoothGatt.disconnect(); + mBluetoothConnectionState = BluetoothProfile.STATE_DISCONNECTED; + } else if (mBluetoothConnectionState != BluetoothProfile.STATE_DISCONNECTED) { + appendToLog("abcc 2 DisconnectRunnable(): start mBluetoothGatt.disconnect"); + mBluetoothGatt.disconnect(); //forcedDisconnect(true); + mBluetoothConnectionState = BluetoothProfile.STATE_DISCONNECTED; + } else if (forcedDisconnect1()) { + if (DEBUG) appendToLog("abcc mDisconnectRunnable(): END"); disconnectRunning = false; + if (false) mBluetoothAdapter.disable(); + done = true; } + if (done == false) mHandler.postDelayed(mDisconnectRunnable, 100); } }; boolean isBleBusy() { - return mBluetoothProfile != BluetoothProfile.STATE_CONNECTED || _readCharacteristic_in_progress /*|| _writeCharacteristic_in_progress*/; + return mBluetoothConnectionState != BluetoothProfile.STATE_CONNECTED || _readCharacteristic_in_progress /*|| _writeCharacteristic_in_progress*/; } private BluetoothGattCharacteristic getCharacteristic(UUID service, UUID characteristic) { diff --git a/cs108library4a/src/main/java/com/csl/cs108library4a/BluetoothConnector.java b/cs108library4a/src/main/java/com/csl/cs108library4a/BluetoothConnector.java index 830bef1..91cb5d5 100644 --- a/cs108library4a/src/main/java/com/csl/cs108library4a/BluetoothConnector.java +++ b/cs108library4a/src/main/java/com/csl/cs108library4a/BluetoothConnector.java @@ -25,7 +25,7 @@ public class BluetoothConnector { private int icsModel = -1; int getCsModel() { - appendToLog("icsModel = " + icsModel); + if (false) appendToLog("icsModel = " + icsModel); return icsModel; } diff --git a/cs108library4a/src/main/java/com/csl/cs108library4a/Cs108Connector.java b/cs108library4a/src/main/java/com/csl/cs108library4a/Cs108Connector.java index 52ea773..ecf98e8 100644 --- a/cs108library4a/src/main/java/com/csl/cs108library4a/Cs108Connector.java +++ b/cs108library4a/src/main/java/com/csl/cs108library4a/Cs108Connector.java @@ -64,7 +64,7 @@ boolean connectBle(ReaderDevice readerDevice) { @Override void disconnect() { super.disconnect(); - appendToLog("done"); + appendToLog("abcc done"); mRfidDevice.mRfidToWrite.clear(); mRfidDevice.mRfidReaderChip.mRx000ToWrite.clear(); } diff --git a/cs108library4a/src/main/java/com/csl/cs108library4a/Cs108Library4A.java b/cs108library4a/src/main/java/com/csl/cs108library4a/Cs108Library4A.java index 5c9b0bb..901a73b 100644 --- a/cs108library4a/src/main/java/com/csl/cs108library4a/Cs108Library4A.java +++ b/cs108library4a/src/main/java/com/csl/cs108library4a/Cs108Library4A.java @@ -2,6 +2,7 @@ import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; +import android.bluetooth.BluetoothProfile; import android.bluetooth.le.ScanCallback; import android.bluetooth.le.ScanResult; import android.content.Context; @@ -162,10 +163,13 @@ public void onLeScan(final BluetoothDevice device, final int rssi, final byte[] @Keep public boolean isBleScanning() { return super.isBleScanning(); } ArrayList mScanResultList = new ArrayList<>(); @Keep public boolean scanLeDevice(final boolean enable) { - if (enable && isBleConnected()) return true; + if (enable) mHandler.removeCallbacks(connectRunnable); - appendToLog("scanLeDevice(" + enable + ")"); - return super.scanLeDevice(enable, this.mLeScanCallback, this.mScanCallback); + appendToLog("abcc scanLeDevice(" + enable + ")"); + if (bluetoothDeviceConnectOld != null) appendToLog("abcc abcc 1 bluetoothDeviceConnectOld connection state = " + mBluetoothManager.getConnectionState(bluetoothDeviceConnectOld, BluetoothProfile.GATT)); + boolean bValue = super.scanLeDevice(enable, this.mLeScanCallback, this.mScanCallback); + appendToLog("abcc isScanning = " + isBleScanning()); + return bValue; } int check9800_serviceUUID2p1 = 0; boolean check9800(Cs108Library4A.Cs108ScanData scanResultA) { @@ -209,13 +213,6 @@ boolean check9800(Cs108Library4A.Cs108ScanData scanResultA) { return found98; } - @Override - @Keep public boolean scanLeDevice(final boolean enable, BluetoothAdapter.LeScanCallback mLeScanCallback, ScanCallback mScanCallBack) { -// usbDeviceScannedList.clear(); - appendToLog("scanLeDevice(" + enable + "mLeScanCallback = " + (mLeScanCallback != null ? "VALID" : "NULL") + "mScanCallBack = " + (mScanCallBack != null ? "VALID" : "NULL")); - return super.scanLeDevice(enable, mLeScanCallback, mScanCallBack); -// mUsbConnector.scanUsbDevice(enable); - } @Keep public String getBluetoothDeviceName() { if (getmBluetoothDevice() == null) return null; return getmBluetoothDevice().getName(); } @Keep public String getBluetoothDeviceAddress() { if (getmBluetoothDevice() == null) return null; return getmBluetoothDevice().getAddress(); } @@ -273,29 +270,66 @@ boolean check9800(Cs108Library4A.Cs108ScanData scanResultA) { return(bleConnection); } + boolean bNeedReconnect = false; int iConnectStateTimer = 0; + final Runnable connectRunnable = new Runnable() { + @Override + public void run() { + appendToLog("abcc connectRunnable: mBluetoothConnectionState = " + mBluetoothConnectionState + ", bNeedReconnect = " + bNeedReconnect); + if (isBleScanning()) { + appendToLog("abcc connectRunnable: still scanning. Stop scanning first"); + scanLeDevice(false); + } else if (bNeedReconnect) { + if (readerDeviceConnect == null) return; + else if (mBluetoothGatt == null) { + appendToLog("abcc connectRunnable: connect1 again"); + connect1(null); + bNeedReconnect = false; + } + } else if (mBluetoothConnectionState == BluetoothProfile.STATE_DISCONNECTED) { //mReaderStreamOutCharacteristic valid around 1500ms + iConnectStateTimer = 0; + appendToLog("abcc connectRunnable: disconnect as disconnected connectionState is received"); + bNeedReconnect = true; + if (mBluetoothGatt != null) disconnect(); + } else if (mReaderStreamOutCharacteristic == null) { + appendToLog("abcc connectRunnable: disconnect as not yet discovery, iConnectStateTimer = " + iConnectStateTimer); + if (++iConnectStateTimer > 10) { } + } else return; + mHandler.postDelayed(connectRunnable, 500); + } + }; ReaderDevice readerDeviceConnect; @Keep public boolean connect(ReaderDevice readerDevice) { if (isBleConnected()) return true; + if (mBluetoothGatt != null) disconnect(); if (readerDevice != null) readerDeviceConnect = readerDevice; if (false) { mHandler.removeCallbacks(runnableToggleConnection); mHandler.post(runnableToggleConnection); return true; - } else return connect1(readerDevice); + } else { + mHandler.removeCallbacks(connectRunnable); + mHandler.postDelayed(connectRunnable, 500); + return connect1(readerDevice); + } } boolean connect1(ReaderDevice readerDevice) { appendToLog("Connect with NULLreaderDevice = " + (readerDevice == null) + ", NULLreaderDeviceConnect = " + (readerDeviceConnect == null)); + if (isBleScanning()) { + appendToLog("abcc still scanning. Stop scanning first"); + scanLeDevice(false); + } if (readerDevice == null && readerDeviceConnect != null) readerDevice = readerDeviceConnect; boolean result = false; if (readerDevice != null) { + bNeedReconnect = false; iConnectStateTimer = 0; bDiscoverStarted = false; setServiceUUIDType(readerDevice.getServiceUUID2p1()); result = super.connectBle(readerDevice); - if (DEBUG) appendToLog("Result = " + result); + if (true) appendToLog("Result = " + result); } return result; } @Keep public void disconnect(boolean tempDisconnect) { - appendToLog("tempDisconnect: getBarcodeOnStatus = " + (getBarcodeOnStatus() ? "on" : "off")); + appendToLog("abcc tempDisconnect: getBarcodeOnStatus = " + (getBarcodeOnStatus() ? "on" : "off")); if (DEBUG) appendToLog("tempDisconnect = " + tempDisconnect); mHandler.removeCallbacks(checkVersionRunnable); mHandler.removeCallbacks(runnableToggleConnection); @@ -309,14 +343,18 @@ boolean connect1(ReaderDevice readerDevice) { } else appendToLog("tempDisconnect: getBarcodeOnStatus is false"); mHandler.postDelayed(disconnectRunnable, 100); appendToLog("done with tempDisconnect = " + tempDisconnect); - if (tempDisconnect == false) readerDeviceConnect = null; + if (tempDisconnect == false) { + mHandler.removeCallbacks(connectRunnable); + bluetoothDeviceConnectOld = mBluetoothAdapter.getRemoteDevice(readerDeviceConnect.getAddress()); + readerDeviceConnect = null; + } } void disconnect() { super.disconnect(); } final Runnable disconnectRunnable = new Runnable() { @Override public void run() { - appendToLog("disconnectRunnable with mBarcodeToWrite.size = " + mBarcodeDevice.mBarcodeToWrite.size()); + appendToLog("abcc disconnectRunnable with mBarcodeToWrite.size = " + mBarcodeDevice.mBarcodeToWrite.size()); if (mBarcodeDevice.mBarcodeToWrite.size() != 0) mHandler.postDelayed(disconnectRunnable, 100); else disconnect(); } @@ -721,7 +759,6 @@ public boolean setAntennaDwell(long antennaDwell) { @Keep public long getPwrlevel() { long lValue = 0; lValue = mRfidDevice.mRfidReaderChip.mRx000Setting.getAntennaPower(-1); - appendToLog("AntennaPower = " + lValue); return lValue; } long pwrlevelSetting; @@ -2946,7 +2983,7 @@ public void run() { if (isBleConnected() == false) { if (connect1(readerDeviceConnect) == false) return; } else return; - } else { disconnect(true); appendToLog("done"); } + } else { disconnect(); appendToLog("done"); } mHandler.postDelayed(runnableToggleConnection, 500); } };