"Bluetooth failes to initialize" 自动执行蓝牙步骤时

"Bluetooth failes to initialize" when automating Bluetooth steps

我正在尝试创建一个 class 顺序执行蓝牙任务,除了启动过程之外无需用户干预。在此 class 中,外部事件调用重写方法 "executeCentral",它从那里调用 setup() 来启用和请求权限。如果它们成功完成,则调用 initialize() 方法并等待一秒钟,然后调用在 EDT 中执行的蓝牙 initialize()。如果它无一例外地运行,它会调用 startScanning(),它也会等待 1 秒,然后再在 EDT 中调用蓝牙 startScan()。扫描开始后等待 10 秒,然后在 EDT 中调用蓝牙 stopScan()。

我为干净的设置重新创建了项目,并在代号一设置中使用了 "downloader"。编译成功并运行,但在"Bluetooth not initialized"

上报异常

知道我做错了什么吗?我的印象是所有调用都必须在美国东部时间完成。

单一形式的 BTDemo 将每个任务作为单独的用户启动事件进行编译和执行。

public class UITaskBluetoothEx extends com.crumptech.library.mobile.ui.tasks.UITaskBluetooth {

protected Bluetooth bt = new Bluetooth();
protected Map devices = new HashMap();

public UITaskBluetoothEx() {
    super();
}

@Override
public String getReplacement() {
    return "UITaskBluetoothEx";
}

protected void showDebug(String message) {
    Display.getInstance().callSerially(new Runnable() {
        @Override
        public void run() {
            UIApplication.showDebug("UITaskBluetoothEx " + message);
            completed(result(false));
        }
    });
}

@Override
protected void executeCentral() {
    bt = new Bluetooth();
    try {
        setup();
        initialize();
    } catch (Exception e) {
        showDebug(e.getMessage());
    }
}

protected void setup() throws IOException {
    if (!bt.isEnabled()) {
        bt.enable();
    }
    if (!bt.hasPermission()) {
        bt.requestPermission();
    }
}

protected void initialize() {
    Timer timer = new Timer();
    timer.schedule(new TimerTask() {
        @Override
        public void run() {
            Display.getInstance().callSerially(new Runnable() {
                @Override
                public void run() {
                    try {
                        if (!bt.isInitialized()) {
                            bt.initialize(true, false, "ShopMyLocalStores");
                        }
                        startScanning();
                    } catch (Exception e) {
                        showDebug(e.getMessage());
                    }

                }
            });
        }
    }, 1000);
}

protected void startScanning() {
    Timer timer = new Timer();
    timer.schedule(new TimerTask() {
        @Override
        public void run() {
            Display.getInstance().callSerially(new Runnable() {
                @Override
                public void run() {
                    try {
                        if (!bt.isScanning()) {
                            bt.startScan(new ActionListener() {
                                @Override
                                public void actionPerformed(ActionEvent evt) {
                                    try {
                                        JSONObject res = (JSONObject) evt.getSource();
                                        if (res.getString("status").equals("scanResult")) {
                                            if (!devices.containsKey(res.getString("address"))) {
                                                devices.put(res.getString("address"), res);
                                            }
                                        }
                                    } catch (JSONException e) {
                                    }
                                }
                            }, null, true, Bluetooth.SCAN_MODE_LOW_POWER, Bluetooth.MATCH_MODE_STICKY, Bluetooth.MATCH_NUM_MAX_ADVERTISEMENT, Bluetooth.CALLBACK_TYPE_ALL_MATCHES);
                            stopScanning();
                        }
                    } catch (Exception e) {
                        showDebug(e.getMessage());
                    }
                }
            });
        }
    }, 1000);
}

protected void stopScanning() {
    try {
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                Display.getInstance().callSerially(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            if (bt.isScanning()) {
                                bt.stopScan();
                            }
                        } catch (Exception e) {
                            showDebug(e.getMessage());
                        }
                        showResults();
                    }
                });
            }
        }, 10000);

    } catch (Exception e) {
    }
}

protected void showResults() {
    Timer timer = new Timer();
    timer.schedule(new TimerTask() {
        @Override
        public void run() {
            Display.getInstance().callSerially(new Runnable() {
                @Override
                public void run() {
                    String text = "";
                    Iterator it = devices.entrySet().iterator();
                    while (it.hasNext()) {
                        Map.Entry pair = (Map.Entry) it.next();
                        text += (pair.getKey() + " = " + pair.getValue() + "\r\n");
                    }
                    UIApplication.showDebug(text);
                    completed(result(true));
                }
            });
        }
    }, 1000);
}

}

iOS 似乎目前不支持某些方法。如果在 iOS 上调用它们,它们将抛出 IOExceptions。这是我们移植的 Cordova 插件中的一个限制。这些方法字面意思是 return "Unsupported Operation" 在插件里面。我不确定这些是否只是插件的遗漏,或者它们是否不受支持。 iOS 目前不支持的方法列表是:

  1. 已启用()
  2. 启用()
  3. 禁用()
  4. mtu()
  5. requestConnectionPriority()
  6. hasPermission()
  7. requestPermission()
  8. isLocationEnabled()
  9. requestLocation()

我在蓝牙的 javadocs 中标记了这些 class 以帮助识别它们。我们可能必须在这里做一些事情来清理它……也许例外并不是最好的事情。

在任何情况下,您的测试应用都会失败,因为您在同一个 try/catch 块中调用了 isEnabled() 和 initialize()。 isEnabled 抛出异常,因此它永远不会初始化()并且您的测试不是 运行.

我已经将您的代码改编成我自己的测试用例,并进行了修改,看起来 运行 没问题。