Android信标库未开始检测信标

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

我不知道我的代码在哪里,我正在尝试使用https://altbeacon.github.io/android-beacon-library检测信标对不起,英语不好,我是移动应用程序开发的新手。

AndroidManifiets.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.isleem.hospital">
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.BLUETOOTH"/>
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />

    <application
        android:name=".application.BeaconApplication"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme.NoActionBar">
        <activity android:name=".activities.MainActivity"/>
        <activity android:name=".activities.BeaconActivity"/>
        <activity
            android:name=".activities.LoginActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

BeaconActivity.class

package com.isleem.hospital.activities;

import android.app.Activity;
import android.os.Bundle;
import android.os.RemoteException;
import android.util.Log;


import org.altbeacon.beacon.Beacon;
import org.altbeacon.beacon.BeaconConsumer;
import org.altbeacon.beacon.BeaconManager;
import org.altbeacon.beacon.RangeNotifier;
import org.altbeacon.beacon.Region;

import java.util.Collection;

public class BeaconActivity extends Activity implements  BeaconConsumer  {
    protected static final String TAG = "RangingActivity";
    private BeaconManager beaconManager = BeaconManager.getInstanceForApplication(this);

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d("naji","beaconactivity");
    }

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

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

    @Override
    protected void onResume() {
        super.onResume();
        beaconManager.bind(this);
    }


    @Override
    public void onBeaconServiceConnect() {

        RangeNotifier rangeNotifier = new RangeNotifier() {
            @Override
            public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
                if (beacons.size() > 0) {
                    Log.d(TAG, "didRangeBeaconsInRegion called with beacon count:  "+beacons.size());
                    Beacon firstBeacon = beacons.iterator().next();
                    Log.d("naji","The first beacon " + firstBeacon.toString() + " is about " + firstBeacon.getDistance() + " meters away.");
                } else {
                    Log.d(TAG, "no avilable beacons");
                }
            }
        };
        try {
//            Collection<Region> monitoredRegions = beaconManager.getMonitoredRegions();
//           Log.d("monitoredRegions",monitoredRegions.toString());
            beaconManager.startMonitoringBeaconsInRegion(new Region("myRangingUniqueId",null,null,null));
            beaconManager.addRangeNotifier(rangeNotifier);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }
}

BeaconApplication.class

package com.isleem.hospital.application;

import android.app.Application;
import android.app.Notification;
import android.os.RemoteException;
import android.util.Log;

import com.isleem.hospital.R;

import org.altbeacon.beacon.BeaconManager;
import org.altbeacon.beacon.Region;
import org.altbeacon.beacon.powersave.BackgroundPowerSaver;
import org.altbeacon.beacon.startup.BootstrapNotifier;
import org.altbeacon.beacon.startup.RegionBootstrap;

public class BeaconApplication extends Application implements BootstrapNotifier {
    private static final String TAG = "BeaconReferenceApp";
    private RegionBootstrap regionBootstrap;
    private BackgroundPowerSaver backgroundPowerSaver;
    private boolean haveDetectedBeaconsSinceBoot = false;
    private String cumulativeLog = "";
    BeaconManager beaconManager ;
    public void onCreate() {
        super.onCreate();
        beaconManager = org.altbeacon.beacon.BeaconManager.getInstanceForApplication(this);
        beaconManager.setDebug(false);
        beaconManager.setEnableScheduledScanJobs(false);
        beaconManager.setBackgroundBetweenScanPeriod(0);
        beaconManager.setBackgroundScanPeriod(1100);
        Notification.Builder builder = new Notification.Builder(this);
        builder.setSmallIcon(R.drawable.ic_launcher_background);
        builder.setContentTitle("Scanning for Beacons");
        beaconManager.enableForegroundServiceScanning(builder.getNotification(),0);
        beaconManager.setEnableScheduledScanJobs(false);
        beaconManager.setBackgroundBetweenScanPeriod(0);
        beaconManager.setBackgroundScanPeriod(1100);
        Log.d(TAG, "setting up background monitoring for beacons and power saving");
        Region region = new Region("myRangingUniqueId",
            null, null, null);
        regionBootstrap = new RegionBootstrap(this, region);
        backgroundPowerSaver = new BackgroundPowerSaver(this);
//        try {
//            beaconManager.startRangingBeaconsInRegion(new Region("myRangingUniqueId", null, null, null));
//        } catch (RemoteException e) {
//            e.printStackTrace();
//        }


}


    @Override
    public void didEnterRegion(Region arg0) {
        // In this example, this class sends a notification to the user whenever a Beacon
        // matching a Region (defined above) are first seen.
        Log.d(TAG, "did enter region.");
        if (!haveDetectedBeaconsSinceBoot) {
            Log.d(TAG, "auto launching MainActivity");

            // The very first time since boot that we detect an beacon, we launch the
            // MainActivity
            //Intent intent = new Intent(this, MonitoringActivity.class);
            //   intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            // Important:  make sure to add android:launchMode="singleInstance" in the manifest
            // to keep multiple copies of this activity from getting created if the user has
            // already manually launched the app.
            //   this.startActivity(intent);
            haveDetectedBeaconsSinceBoot = true;
        } else {
            // If the Monitoring Activity is visible, we log info about the beacons we have
            // seen on its display
            Log.d("beacon","I see a beacon again" );
            // If we have already seen beacons before, but the monitoring activity is not in
            // the foreground, we send a notification to the user on subsequent detections.
        }
    }

    @Override
    public void didExitRegion(Region region) {
        Log.d(TAG,"I no longer see a beacon.");
    }

    @Override
    public void didDetermineStateForRegion(int state, Region region) {
        Log.d(TAG,"Current region state is: " + (state == 1 ? "INSIDE" : "OUTSIDE ("+state+")"));
    }


}

并且我使用此代码来启动我的BeaconActivity

 Intent intent = new Intent(this, BeaconActivity.class);
    startActivity(intent);
java android mobile ibeacon altbeacon
1个回答
0
投票

要检查的几件事:

  1. 您是信标广告吗?尝试使用像BeaconScope这样的现成应用程序,并确保已检测到它。

  2. 您是否从用户动态request and obtain location permission?除非您这样做,否则无法检测到信标。

  3. 您是否为要使用的信标类型设置了BeaconParser?如果使用AltBeacon,则不需要这样做。但是if using iBeacon you do

  4. 您接到“ didDetermineStateForRegion”的电话吗?是否表明您已经在该区域内?如果是这样,则可能是检测到您的信标,但是库认为它已经在该区域中了(它会在重新启动时记住这一点),因此它再也不会调用didEnterRegion了。

  5. 蓝牙打开了吗?手机设置中是否打开了位置信息?如果您检查应用程序权限,是否确认已将位置权限授予您的应用程序?

© www.soinside.com 2019 - 2024. All rights reserved.