Android iBeacon 示例代码无法构建

Android iBeacon Sample code cannot build

您好,我正在使用 here 中的示例代码,但在构建应用程序时出现错误。我已经下载了最新的 iBeacon 库 android-beacon-library-2.7.tar.gz 并按照网站上的说明添加到项目中。

这是MainActivity.class

package com.example.ibeacontest;

import android.util.Log;

import org.altbeacon.beacon.BeaconConsumer;
import org.altbeacon.beacon.BeaconManager;
import org.altbeacon.beacon.MonitorNotifier;
import org.altbeacon.beacon.startup.BootstrapNotifier;

import android.app.Activity;
import android.graphics.Region;
import android.os.Bundle;
import android.os.RemoteException;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends Activity  implements BeaconConsumer {
    protected static final String TAG = "MonitoringActivity";
    private BeaconManager beaconManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
                beaconManager = BeaconManager.getInstanceForApplication(this);
        // To detect proprietary beacons, you must add a line like below corresponding to your beacon
        // type.  Do a web search for "setBeaconLayout" to get the proper expression.
        // beaconManager.getBeaconParsers().add(new BeaconParser().
        //        setBeaconLayout("m:2-3=beac,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25"));
        beaconManager.bind(this);
    }
    @Override 
    protected void onDestroy() {
        super.onDestroy();
        beaconManager.unbind(this);
    }


    public void onBeaconServiceConnect() {
        beaconManager.setMonitorNotifier(new MonitorNotifier() {

        public void didEnterRegion(Region region) {
            Log.i(TAG, "I just saw an beacon for the first time!");        
        }

        public void didExitRegion(Region region) {
            Log.i(TAG, "I no longer see an beacon");
        }

        public void didDetermineStateForRegion(int state, Region region) {
            Log.i(TAG, "I have just switched from seeing/not seeing beacons: "+state);        
            }
        });

        try {
            beaconManager.startMonitoringBeaconsInRegion(new Region("myMonitoringUniqueId", null, null, null));
        } catch (RemoteException e) {    }
    }

}

错误:

Description Resource    Path    Location    Type
The constructor Region(String, null, null, null) is undefined   MainActivity.java   /iBeaconTest/src/com/example/ibeacontest    line 56 Java Problem
The method startMonitoringBeaconsInRegion(org.altbeacon.beacon.Region) in the type BeaconManager is not applicable for the arguments (android.graphics.Region)  MainActivity.java   /iBeaconTest/src/com/example/ibeacontest    line 56 Java Problem
The type new MonitorNotifier(){} must implement the inherited abstract method MonitorNotifier.didDetermineStateForRegion(int, Region)   MainActivity.java   /iBeaconTest/src/com/example/ibeacontest    line 40 Java Problem
The type new MonitorNotifier(){} must implement the inherited abstract method MonitorNotifier.didEnterRegion(Region)    MainActivity.java   /iBeaconTest/src/com/example/ibeacontest    line 40 Java Problem
The type new MonitorNotifier(){} must implement the inherited abstract method MonitorNotifier.didExitRegion(Region) MainActivity.java   /iBeaconTest/src/com/example/ibeacontest    line 40 Java Problem

可能是什么问题?

其中一个 import 语句引用了错误的包。只需更改:

import android.graphics.Region;import org.altbeacon.beacon.Region;

在我进行更改之前,我遇到了与问题中所示相同的错误。之后,代码编译成功。