0

我想从 Android 手机 Nexus5 扫描 iBeacon UUID = "2F234454-CF6D-4A0F-ADF2-F4911BA9FFA6"

我遵循了 AltBeacon 和SO query中的示例。但是没有看到任何扫描。我哪里错了?

这是代码

private static final String TAG = "ALTBEACON";
private BeaconManager beaconManager = BeaconManager.getInstanceForApplication(this);
private String UUID = "2F234454-CF6D-4A0F-ADF2-F4911BA9FFA6";
private static final int REQUEST_ENABLE_BT = 1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    if(beaconManager.checkAvailability()){
        beaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24"));
        beaconManager.bind(this);
    }
    else{
            Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // User chose not to enable Bluetooth.
    if (requestCode == REQUEST_ENABLE_BT && resultCode == Activity.RESULT_CANCELED) {
        return;
    }
    super.onActivityResult(requestCode, resultCode, data);
}


@Override
protected void onDestroy() {
    super.onDestroy();
    beaconManager.unbind(this);
}


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

        @Override
        public void didExitRegion(Region region) {
            Log.i(TAG, "Exit from Region");
            Toast.makeText(getApplicationContext(), "Exit from Region", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void didEnterRegion(Region region) {
            Log.i(TAG, "Entered in Region");
            Toast.makeText(getApplicationContext(), "Entered in Region", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void didDetermineStateForRegion(int state, Region region) {
            Log.i(TAG, "Not Sure... State : "+state+" ... Region : "+region.describeContents());
            Toast.makeText(getApplicationContext(), "Not Sure... State : "+state+" ... Region : "+region.describeContents(), Toast.LENGTH_SHORT).show();
        }
    });

    try {
        beaconManager.startMonitoringBeaconsInRegion(new Region(UUID, null, null, null));
    } catch (RemoteException e) {
        e.printStackTrace();
    }
}
4

1 回答 1

1

最可能的问题是您的应用没有正确的 AndroidManifest.xml 条目来声明信标扫描服务。这通常是使用从库的清单中合并清单自动完成的。如果您使用的是 Eclipse,则需要按照库的快速入门指南中的说明打开清单合并:

编辑您的 project.properties 文件并添加以下行:manifestmerger.enabled=true

判断这是否是问题的一种简单方法是将日志行添加到onBeaconServiceConnect()方法的顶部。如果它没有被调用,这意味着服务无法启动。

于 2014-11-18T12:16:31.797 回答