首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Cordova-Android上网络打印机的扫描

Cordova-Android上网络打印机的扫描
EN

Stack Overflow用户
提问于 2018-12-12 07:12:08
回答 1查看 366关注 0票数 0

我试图建立一个插件在科多瓦,可以找到蓝牙,USB和网络打印机和打印文本,图像,QR代码,条形码.我有一个问题,在网络打印机扫描,需要一些帮助。下面的代码可以搜索连接到wifi的网络打印机。它适用于android 7和6,但是在android 5的情况下,它无法返回callback.This可能是由于android 5上的线程限制或其他原因。

代码语言:javascript
复制
 scanWifi(ips, new OnIPScanningCallback() {
            @Override
            public void onScanningComplete(List<String> results) {
                Log.d("TAG", "onScanningComplete: " + results.size()+" : "+results.toString());
                Printers printerList =null;
                for(String printerIps:results) {
                    String mac = getHardwareAddress(printerIps);
                    printerList = new Printers(printerIps, mac);
                    printers.put(printerIps);
                    list.add(printerList);
                }
                Log.d(TAGS,"The List of all wifi Printers : "+list);
            }
        });
        isStart = false;
    }

private static void scanWifi(final List<String> ips, final 
OnIPScanningCallback callback) {
    final Vector<String> results = new Vector<String>();
    final int totalSize = ips.size();
    final int splitSize = 10;
    final int[] index = {0};
    final long start = System.currentTimeMillis();
    for (int i = 0; i < totalSize; i += splitSize) {
        final List<String> child = new ArrayList<String>(ips.subList(i, Math.min(totalSize, i + splitSize)));

        new Thread() {
            @Override
            public void run() {
                for (String ip : child) {
                    Log.d("TAG", " scanning : " + index[0] + ", ip: " + ip);
                    boolean isPrinter = connect(ip);
                    if (isPrinter) {
                        results.add(ip);
                    }
                    if (index[0] == ips.size() - 1) {
                        long end = System.currentTimeMillis();
                        Log.d("TAG", "scanning time: " + (end - start) / 1000);
                        callback.onScanningComplete(results);
                    } else {
                        index[0]++;
                    }
                }

            }
        }.start();
    }
}
  public interface OnIPScanningCallback {
    void onScanningComplete(List<String> results);
  }

我也尝试过异步任务,它在所有版本的android上都能工作,但是问题是这个过程需要170到193秒,这在上面的代码中是很长的,它可以在20秒内完成同样的任务。

代码语言:javascript
复制
 scanWifi(ips, new PrintingPlugin.OnIPScanningCallback() {
            @Override
            public void onScanningComplete(List<String> results) {
                Log.d(TAGS, "onScanningComplete: " + results.size() + " : " + results.toString());
                Printers printerList;
                for (String printerIps : results) {
                    String mac = getHardwareAddress(printerIps);
                    printerList = new Printers(printerIps, mac);
                    printers.put(printerIps);
                    list.add(printerList);
                }
                Log.d(TAGS, "The List of all wifi Printers : " + list);

            }
        });
        isStart = false;
    } catch (Exception e) {
        Log.e(TAGS, "Error while scanning for wifi"+e.getMessage());
    }
    return printers;
}

private  Integer index = 0;

void resetIndex() {
    index = 0;
}

private  void scanWifi(final List<String> ips, final PrintingPlugin.OnIPScanningCallback callback) {
    Log.d(TAGS, " scanWifi" );
    final Vector<String> results = new Vector<String>();
    final int totalSize = ips.size();
    final int splitSize = 10;
    resetIndex();
    final long start = System.currentTimeMillis();
    for (int i = 0; i < totalSize; i += splitSize) {
        final List<String> child = new ArrayList<String>(ips.subList(i, Math.min(totalSize, i + splitSize)));
        executeTask(new AsyncTask() {
            @Override
            protected Object doInBackground(Object[] objects) {
                synchronized (index) {
                    for (String ip : child) {
                        Log.d(TAGS, " scanning : " + index + ", ip: " + ip);
                        boolean isPrinter = connect(ip);
                        if (isPrinter) {
                            results.add(ip);
                        }
                        if (index == ips.size() - 1) {
                            long end = System.currentTimeMillis();
                            Log.d(TAGS, "scanning time: " + (end - start) / 1000);
                            callback.onScanningComplete(results);
                        } else {
                            index++;
                        }
                    }
                }

                return null;
            }

        });
    }

   public void executeTask(AsyncTask asyncTask) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        asyncTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
    } else {
        asyncTask.execute();
    }
}

这就是我在android 5上运行时得到的信息。

代码语言:javascript
复制
D/ConnectivityManager.CallbackHandler: CM callback handler got msg 524290

任何帮助使这个东西以任何方式工作(第一个在android 5上工作的代码或第二个工作速度更快和效率更高的代码)将是非常感谢的。我已经看过与此相关的问题,但我不想使用打印服务。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-01-23 04:01:34

由于索引值是未发送回调的原因,因此,为了解决这个问题,只要您发现Network并将其存储在var中(在我的例子中是JSONArray),就会给出打印机的列表及其有效性。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53737790

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档