iBeacon具有多个区域的android通知

问题描述 投票:0回答:1

我刚开始使用Java和Android编码。我在我的大学写了一份科学研究申请书。该应用程序是为博物馆的本地展览。我镇上有不同的地点,每个地点都有自己的展览。

现在我为每个位置创建了一个活动,因此用户可以看到该示例的一些有用信息。现在我想将应用程序与iBeacons结合起来,我从Estimote购买了6个信标。我希望应用程序向用户发送一些通知,其中包含以下文字:“您在对象XY前面。点击以查看更多信息。”点击通知后,用户应该打开我创建的特定活动。我还希望应用程序在后台搜索信标,因此如果用户靠近某个位置,他/她会在几秒钟后自动收到通知。

我已经编写了一些代码,但我不知道如何进一步处理。该应用程序现在什么都不做。我为iBeacon做了一个额外的课程,在我的MainActivity中我有一个菜单,用户可以在其中选择一个名为“List Of Places”的新活动。然后他/她可以为每个位置选择特定活动。

所以在这里我有我的灯塔类:

package com.example.walter.him;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.app.Application;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.TaskStackBuilder;
import android.support.v4.app.NotificationCompat;
import org.altbeacon.beacon.BeaconConsumer;
import org.altbeacon.beacon.BeaconManager;
import org.altbeacon.beacon.BeaconParser;
import org.altbeacon.beacon.Identifier;
import org.altbeacon.beacon.Region;
import org.altbeacon.beacon.powersave.BackgroundPowerSaver;
import org.altbeacon.beacon.startup.RegionBootstrap;
import org.altbeacon.beacon.startup.BootstrapNotifier;
import java.util.Arrays;
import java.util.List;


public class BeaconMain extends Application implements BootstrapNotifier
{

private static final String TAG = "AndroidProximityReferenceApplication";
private RegionBootstrap regionBootstrap;
private BackgroundPowerSaver backgroundPowerSaver;
private boolean haveDetectedBeaconsSinceBoot = false;
private andreasplatz monitoringActivity = null;
private hagentor monitoringActivity2 = null;

public void onCreate(Bundle savedInstanceState)
{
    super.onCreate();

    BeaconManager beaconManager = org.altbeacon.beacon.BeaconManager.getInstanceForApplication(this);
    beaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24"));
    beaconManager.bind((BeaconConsumer) this);
    Region museumLocation1 = new Region("museumLocation1",  Identifier.parse("B9407F30-F5F8-466E-AFF9-25556B57FE6D"), Identifier.parse("56170"), Identifier.parse("42307"));
    Region museumLocation2 = new Region("museumLocation2",  Identifier.parse("B9407F30-F5F8-466E-AFF9-25556B57FE6D"), Identifier.parse("55787"), Identifier.parse("12089"));
    List regionList = Arrays.asList(new Region[]{museumLocation1, museumLocation2});
    regionBootstrap = new RegionBootstrap(this, regionList);
    backgroundPowerSaver = new BackgroundPowerSaver(this);
}


public void didEnterRegion(Region region) {

    if (region.getUniqueId().equals("museumLocation1")) {
        Intent intent = new Intent(this, andreasplatz.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        this.startActivity(intent);
    }
    if (region.getUniqueId().equals("museumLocation2")) {
        Intent intent = new Intent(this, hagentor.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        this.startActivity(intent);
    }
    haveDetectedBeaconsSinceBoot = true;
}

@Override
public void didExitRegion(Region region) {

}

@Override
public void didDetermineStateForRegion(int i, Region region) {

}


private void sendNotification() {
    NotificationCompat.Builder builder =
            new NotificationCompat.Builder(this)
                    .setContentTitle("Beacon Reference Application")
                    .setContentText("An beacon is nearby.")
                    .setSmallIcon(R.drawable.ic_launcher);

    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    stackBuilder.addNextIntent(new Intent(this, andreasplatz.class));
    PendingIntent resultPendingIntent =
            stackBuilder.getPendingIntent(
                    0,
                    PendingIntent.FLAG_UPDATE_CURRENT
            );
    builder.setContentIntent(resultPendingIntent);
    NotificationManager notificationManager =
            (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(1, builder.build());
}

public void setMonitoringActivity(andreasplatz activity) {
    this.monitoringActivity = activity;
}
}

活动“andreasplatz”将是示例性位置。活动“hagentor”中包含相同的代码。所以这是代码:

package com.example.walter.him;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import org.altbeacon.beacon.BeaconManager;

public class andreasplatz extends Activity {

    protected static final String TAG = "MonitoringActivity";
    private BeaconManager beaconManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_andreasplatz);
        verifyBluetooth();
    }



    private void verifyBluetooth() {

        try {
            if (!BeaconManager.getInstanceForApplication(this).checkAvailability()) {
                final AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setTitle("Bluetooth not enabled");
                builder.setMessage("Please enable bluetooth in settings and restart this application.");
                builder.setPositiveButton(android.R.string.ok, null);
                builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
                    @Override
                    public void onDismiss(DialogInterface dialog) {
                        finish();
                        System.exit(0);
                    }
                });
                builder.show();
            }
        }
        catch (RuntimeException e) {
            final AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("Bluetooth LE not available");
            builder.setMessage("Sorry, this device does not support Bluetooth LE.");
            builder.setPositiveButton(android.R.string.ok, null);
            builder.setOnDismissListener(new DialogInterface.OnDismissListener() {

                @Override
                public void onDismiss(DialogInterface dialog) {
                    finish();
                    System.exit(0);
                }

            });
            builder.show();

        }

    }


}

我应该添加或更改什么,来做我想要的应用程序?这是我的Manifest.xml的摘录 - 可能是我做错了:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    android:versionCode="1"
    android:versionName="1.0"
    package="com.example.walter.him" >

    <uses-sdk
        android:minSdkVersion="18"
        android:targetSdkVersion="18" />

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme"
        android:launchMode="singleInstance" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
android notifications ibeacon region ibeacon-android
1个回答
1
投票

一些提示:

  1. 由于您使用的是RegionBootstrap,因此无需调用绑定到beaconManager。删除此行: beaconManager.bind((BeaconConsumer) this);
  2. 将调试行添加到BeaconMain类'onCreatedidEnterRegion方法的顶部,如下所示: // put this at the top of onCreate Log.d(TAG, "onCreate called"); // put this at the top of didEnterRegion Log.d(TAG, "didEnterRegion called with: "+region..getUniqueId());
  3. 确保您的信标正在传输这些标识符。当您的信标打开时,请使用现成的信标检测器应用程序,如Locate,并确保您看到相同的标识符: B9407F30-F5F8-466E-AFF9-25556B57FE6D 56170 42307 B9407F30-F5F8-466E-AFF9-25556B57FE6D 55787 12089
  4. 完成上述操作后,关闭信标,从任务切换器中删除应用程序,重新启动应用程序,然后打开信标。
  5. 观察LogCat输出并查找调试行,告诉您是否调用了onCreate和didEnterRegion。
© www.soinside.com 2019 - 2024. All rights reserved.