|
@@ -417,15 +417,8 @@ public class MainActivity extends BaseActivity {
|
|
|
}
|
|
|
|
|
|
@JavascriptInterface
|
|
|
- public void getPortStatus() {
|
|
|
- if(!FastClickUtil.isFastClick()) {
|
|
|
- toGetPortStatus();
|
|
|
- new Handler().postDelayed(() -> {
|
|
|
- Log.i(TAG, "getPortStatus:" + JSONObject.toJSONString(portStatusParam));
|
|
|
- runOnUiThread(() -> mWebView.loadUrl("javascript:getPortStatus('" + JSONObject.toJSONString(portStatusParam) + "')"));
|
|
|
- }, 9000);
|
|
|
- // XToast.showToast("正在初始化串口");
|
|
|
- }
|
|
|
+ public void getPortStatus(int scannerNum, int lightNum, int maxRetryNum) {
|
|
|
+ toGetPortStatus(scannerNum, lightNum, maxRetryNum);
|
|
|
}
|
|
|
|
|
|
@JavascriptInterface
|
|
@@ -548,10 +541,7 @@ public class MainActivity extends BaseActivity {
|
|
|
@JavascriptInterface
|
|
|
public void connectPort(String name) {
|
|
|
Log.e(TAG, "connectPort");
|
|
|
- if(!FastClickUtil.isFastClick()) {
|
|
|
- toConnectPort(name);
|
|
|
- // XToast.showToast("正在连接串口");
|
|
|
- }
|
|
|
+ toConnectPort(name);
|
|
|
}
|
|
|
|
|
|
@JavascriptInterface
|
|
@@ -578,7 +568,7 @@ public class MainActivity extends BaseActivity {
|
|
|
Set<String> scannerList = new HashSet<>();
|
|
|
Set<String> lightList = new HashSet<>();
|
|
|
Map<String, Set<String>> portStatusParam = new HashMap<>();
|
|
|
- private void toGetPortStatus() {
|
|
|
+ private void toGetPortStatus(int scannerNum, int lightNum, int maxRetryNum) {
|
|
|
// 清除上次连接的数据
|
|
|
scannerList.clear();
|
|
|
lightList.clear();
|
|
@@ -591,8 +581,6 @@ public class MainActivity extends BaseActivity {
|
|
|
toCloseLight();
|
|
|
// 遍历串口
|
|
|
SerialPortFinder finder = new SerialPortFinder();
|
|
|
- // 保存能够连接上的串口
|
|
|
- List<String> availableSeries = new ArrayList<>();
|
|
|
// 将每次的连接对象保存在map对象中
|
|
|
HashMap<String, SerialPortManager> serialPortManagerMap = new HashMap<>();
|
|
|
// 保存usb口的串口
|
|
@@ -621,28 +609,39 @@ public class MainActivity extends BaseActivity {
|
|
|
}
|
|
|
// 对usb串口进行初始化
|
|
|
initUsbGreaterThan10(usbSeries);
|
|
|
+ // 发送命令
|
|
|
+ sendCommandWithRetry(serialPortManagerMap, usbSeries, scannerNum, lightNum, 1, maxRetryNum);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void sendCommandWithRetry(HashMap<String, SerialPortManager> serialPortManagerMap, List<String> usbSeries, int scannerNum, int lightNum, int currentRetryCount, int maxRetryNum) {
|
|
|
+ if (currentRetryCount > maxRetryNum) {
|
|
|
+ Log.i(TAG, "getPortStatus:" + JSONObject.toJSONString(portStatusParam));
|
|
|
+ runOnUiThread(() -> mWebView.loadUrl("javascript:getPortStatus('" + JSONObject.toJSONString(portStatusParam) + "')"));
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ Log.i(TAG, "--------------------getPortStatus: 第" + currentRetryCount + "次尝试----------------------");
|
|
|
// 定义常量
|
|
|
int usbPortNum = usbSeries.size();
|
|
|
- int commandNum = sendCommands.size();
|
|
|
int num = 0;
|
|
|
- int delay = 500;
|
|
|
- if (usbPortNum >0){
|
|
|
- for (int i = 0; i < usbSeries.size(); i++) {
|
|
|
+ int delay = 300;
|
|
|
+ int commandNum = sendCommands.size();
|
|
|
+ // 为每个usb口建立串口连接并保存
|
|
|
+ if (usbPortNum > 0){
|
|
|
+ for (int i = 0; i < usbPortNum; i++) {
|
|
|
String seriesName = usbSeries.get(i);
|
|
|
try {
|
|
|
+ Log.i(TAG, "findPort: " + seriesName);
|
|
|
SerialPortManager serialPortManager = new SerialPortManager();
|
|
|
serialPortManager.init(this, seriesName, UserCache.getSerialRate(), new SerialPortManager.SerialCallBack() {
|
|
|
@Override
|
|
|
public void dataCallBack(String data) {
|
|
|
- Log.i(TAG, "findSpecificSeries: " + seriesName + "收到数据" + data);
|
|
|
+ Log.i(TAG, "findAllSpecificSeries: " + seriesName + "收到数据" + data);
|
|
|
if (data.contains("04D00000FF2C")) {
|
|
|
Log.i(TAG, "查找USB SCANNER成功:" + seriesName + "-->" + data);
|
|
|
scannerList.add(seriesName);
|
|
|
- portStatusParam.put("scanners", scannerList);
|
|
|
} else if(data.contains("A00000A0")) {
|
|
|
Log.i(TAG, "查找USB Light成功:" + seriesName + "-->" + data);
|
|
|
lightList.add(seriesName);
|
|
|
- portStatusParam.put("lights", lightList);
|
|
|
}
|
|
|
};
|
|
|
|
|
@@ -650,7 +649,6 @@ public class MainActivity extends BaseActivity {
|
|
|
public void openCallBack(int code) {
|
|
|
if (code == 1) {
|
|
|
Log.i(TAG, "findAllSpecificSeries: " + "尝试打开连接" + seriesName + "成功...");
|
|
|
- availableSeries.add(seriesName);
|
|
|
} else {
|
|
|
Log.i(TAG, "findAllSpecificSeries: " + "尝试打开连接" + seriesName + "失败...");
|
|
|
Toast.makeText(MainActivity.this, "串口未打开", Toast.LENGTH_SHORT).show();
|
|
@@ -665,31 +663,54 @@ public class MainActivity extends BaseActivity {
|
|
|
// 将连接保存在map中, 便于后续操作
|
|
|
serialPortManagerMap.put(seriesName, serialPortManager);
|
|
|
// 每间隔两秒执行一次发送指令
|
|
|
- for (int j = 0; j < sendCommands.size(); j++) {
|
|
|
- String command = sendCommands.get(j);
|
|
|
- sendCommandhandler.postDelayed(() -> {
|
|
|
- try {
|
|
|
- if(command.equals("07C60408008A09FE94")) {
|
|
|
- serialPortManager.sendHexData1(command);
|
|
|
- } else{
|
|
|
- serialPortManager.sendHexData1(command);
|
|
|
- }
|
|
|
- Log.i(TAG, "向" + seriesName + "发送" + command);
|
|
|
- }catch (Exception e) {
|
|
|
- Log.i(TAG, seriesName + "发送指令出现异常");
|
|
|
- }
|
|
|
- }, (long) num * delay);
|
|
|
- num++;
|
|
|
- }
|
|
|
}catch (Exception e) {
|
|
|
- Log.i(TAG, "findSpecificSeries2: " + "尝试连接" + seriesName + "发生错误...");
|
|
|
+ Log.i(TAG, "findAllSpecificSeries: " + "尝试连接" + seriesName + "发生错误...");
|
|
|
}
|
|
|
- sendCommandhandler.postDelayed(() -> {
|
|
|
- Objects.requireNonNull(serialPortManagerMap.get(seriesName)).close();
|
|
|
- Log.i(TAG, "关闭" + seriesName + "的连接");
|
|
|
- }, (long) (i + 1) * commandNum * delay + 500);
|
|
|
}
|
|
|
}
|
|
|
+ // 向每个usb口发送指令
|
|
|
+ for (int i = 0; i < usbSeries.size(); i++) {
|
|
|
+ int finalI = i;
|
|
|
+ String seriesName = usbSeries.get(i);
|
|
|
+ SerialPortManager serialPortManager = serialPortManagerMap.get(seriesName);
|
|
|
+ try {
|
|
|
+ // 每间隔两秒执行一次发送指令
|
|
|
+ for (int j = 0; j < sendCommands.size(); j++) {
|
|
|
+ String command = sendCommands.get(j);
|
|
|
+ sendCommandhandler.postDelayed(() -> {
|
|
|
+ try {
|
|
|
+ if(command.equals("07C60408008A09FE94")) {
|
|
|
+ serialPortManager.sendHexData1(command);
|
|
|
+ } else {
|
|
|
+ serialPortManager.sendHexData1(command);
|
|
|
+ }
|
|
|
+ Log.i(TAG, "向" + seriesName + "发送" + command);
|
|
|
+ }catch (Exception e) {
|
|
|
+ Log.i(TAG, seriesName + "发送指令出现异常");
|
|
|
+ }
|
|
|
+ }, (long) num * delay);
|
|
|
+ num++;
|
|
|
+ }
|
|
|
+ }catch (Exception e) {
|
|
|
+ Log.i(TAG, "findSpecificSeries: " + "尝试连接" + seriesName + "发生错误...");
|
|
|
+ }
|
|
|
+ sendCommandhandler.postDelayed(() -> {
|
|
|
+ Objects.requireNonNull(serialPortManagerMap.get(seriesName)).close();
|
|
|
+ Log.i(TAG, "关闭" + seriesName + "的连接");
|
|
|
+ // 如果释放的是最后一个连接
|
|
|
+ if(finalI == usbSeries.size() - 1) {
|
|
|
+ if(scannerList.size() < scannerNum || lightList.size() < lightNum) {
|
|
|
+ // 递归重试
|
|
|
+ int cnt = currentRetryCount;
|
|
|
+ cnt = cnt + 1;
|
|
|
+ sendCommandWithRetry(serialPortManagerMap, usbSeries, scannerNum, lightNum, cnt, maxRetryNum);
|
|
|
+ }else{
|
|
|
+ Log.i(TAG, "getPortStatus:" + JSONObject.toJSONString(portStatusParam));
|
|
|
+ runOnUiThread(() -> mWebView.loadUrl("javascript:getPortStatus('" + JSONObject.toJSONString(portStatusParam) + "')"));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }, (long) (i + 1) * commandNum * delay + 500);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
private void initUsbGreaterThan10(List<String> usbSeries) {
|
|
@@ -1206,6 +1227,22 @@ public class MainActivity extends BaseActivity {
|
|
|
//开始蓝牙扫码器
|
|
|
private void startScan(String name) {
|
|
|
// Toast.makeText(MainActivity.this, "开始连接", Toast.LENGTH_SHORT).show();
|
|
|
+ if(mBleDevice == null) {
|
|
|
+ // 蓝牙未连接过 或 连接的蓝牙不存在
|
|
|
+ doStartScan(name);
|
|
|
+ } else if (!mBleDevice.getDevice().getName().contains(name)){
|
|
|
+ // 要连接的蓝牙名字和之前的蓝牙设备不一致
|
|
|
+ // 关闭先前的蓝牙, 再扫描新的蓝牙
|
|
|
+ toCloseScan();
|
|
|
+ doStartScan(name);
|
|
|
+ } else {
|
|
|
+ runOnUiThread(() -> {
|
|
|
+ mWebView.loadUrl("javascript:responseCode('6')");
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void doStartScan(String name) {
|
|
|
BleManager.getInstance().scan(new BleScanCallback() {
|
|
|
@Override
|
|
|
public void onScanStarted(boolean success) {
|
|
@@ -1235,13 +1272,19 @@ public class MainActivity extends BaseActivity {
|
|
|
boolean isScanSuccess = false;
|
|
|
for (int i = 0; i < scanResultList.size(); i++) {
|
|
|
BleDevice bleDevice = scanResultList.get(i);
|
|
|
- if ((name+"BLE").equals(bleDevice.getName())){
|
|
|
+ if ((name +"BLE").equals(bleDevice.getName())){
|
|
|
isScanSuccess = true;
|
|
|
}
|
|
|
}
|
|
|
// Toast.makeText(MainActivity.this, "扫描完成,是否成功="+isScanSuccess, Toast.LENGTH_SHORT).show();
|
|
|
if (isScanSuccess){
|
|
|
+ // 找到了匹配的蓝牙
|
|
|
mWebView.loadUrl("javascript:responseCode('1')");
|
|
|
+ }else{
|
|
|
+ // 没找到匹配的蓝牙
|
|
|
+ // 返回不等于6的code
|
|
|
+ mWebView.loadUrl("javascript:responseCode('2')");
|
|
|
+ mBleDevice = null;
|
|
|
}
|
|
|
}
|
|
|
});
|