Android Beacon示例应用

时间:2014-06-20 12:20:47

标签: android android-logcat ibeacon-android

我试图在信标上构建一个应用程序。所以我从here.

获得了一个示例代码

我已经运行了该代码,当我进入信标范围时,应用程序崩溃并且logcat中的错误如下:

06-20 17:48:24.256: E/AndroidRuntime(18104): android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.

请帮帮我。

我检查了代码,错误在于这些行。

iBeaconManager.setRangeNotifier(new RangeNotifier() {
        @Override
        public void didRangeBeaconsInRegion(Collection<IBeacon> iBeacons, Region region) {
            if (iBeacons.size() > 0) {
                double distance = iBeacons.iterator().next().getAccuracy();
                DecimalFormat decimalFormat = new DecimalFormat("##.##");
                double distanceFormatted = Double.valueOf(decimalFormat.format(distance));
                TextView distanceTextView = (TextView) findViewById(R.id.am_tv_distance);
                distanceTextView.setText(String.valueOf(distanceFormatted) + " m");
            }
        }
    });

2 个答案:

答案 0 :(得分:1)

您需要在UI线程中执行UI更改:

    iBeaconManager.setRangeNotifier(new RangeNotifier() {
    @Override
    public void didRangeBeaconsInRegion(Collection<IBeacon> iBeacons, Region region) {
        if (iBeacons.size() > 0) {
            double distance = iBeacons.iterator().next().getAccuracy();
            DecimalFormat decimalFormat = new DecimalFormat("##.##");
            double distanceFormatted = Double.valueOf(decimalFormat.format(distance));
            runOnUiThread(new Runnable() {

                  @Override
                  public void run() {
                        TextView distanceTextView = (TextView) findViewById(R.id.am_tv_distance);
                        distanceTextView.setText(String.valueOf(distanceFormatted) + " m");
                  }
            });
         }
    }
});

答案 1 :(得分:0)

对于CalledFromWrongThreadException

编写一个函数并调用它:

private void toastToDisplay(final String line) {
    runOnUiThread(new Runnable() {
        public void run() {
            //Toast.makeText(YourActivity.this, "" + line,
                    // Toast.LENGTH_LONG).show();
            TextView yourTextView = (TextView) findViewById(R.id.your_tv);
            yourTextView .setText(line);
        }
    });
}  

如果需要,您可以使用所需的distanceTextView或其他视图编辑该功能。 发生的情况是服务正在应用程序的后台线程上运行,因此当您直接调用将更新UI的操作时,它会与您编写该代码时所处的线程冲突。

因此,如果当前线程不是UI线程,则使用runOnUiThread

将操作发布到UI线程的事件队列中

“我只获得距离不是范围”的含义究竟是什么? ...对于iBeacons.iterator().next().getAccuracy();,部分..您可以通过使用for for each收集信标详细信息,如:

for (IBeacon iBeacon : iBeacons) {
String line = "Major: " + iBeacon.getMajor()
                                    + "Minor: " + iBeacon.getMinor()
                                    + "Accuracy in m(s): "
                                    + iBeacon.getAccuracy()
                                    + "BluetoothAddress: "
                                    + iBeacon.getBluetoothAddress()
                                    + "Proximity: " + iBeacon.getProximity()
                                    + "ProximityUuid: "
                                    + iBeacon.getProximityUuid() + "Rssi: "
                                    + iBeacon.getRssi() + "TxPower: "
                                    + iBeacon.getTxPower(); 
toastToDisplay(line);
    }  

如果您还没有看到它,this Android iBeacon Library sample application是一个很好的参考。