Обнаружение фоновых маяков с помощью Android Beacon Library - PullRequest
1 голос
/ 19 апреля 2019

Я использую библиотеку Android Beacon для сканирования iBeacons. Я использую модуль HM-10 BLE в качестве iBeacon. Моя проблема в том, что когда я использовал примеры кодов библиотеки Android Beacon, ничего не происходит вообще. Как упоминалось в примере кода для запуска приложения в фоновом режиме , я создал новый класс Java с именем «Backgroud» и класс MainActivity.

Я хочу, чтобы мое приложение запускало , когда маяк обнаружен, когда приложение не открыто. Или показать уведомление (тост) , когда приложение открыто.

Я также хочу знать, что мы помещаем в класс MainActivity .

Любая помощь будет оценена.

Это мой файл AndroidManifest.xml:

<application
        android:allowBackup="true"
        android:name=".Background"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:launchMode="singleInstance">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    </application>

Это мой класс MainActivity Java:

public class MainActivity extends Activity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate( savedInstanceState );
            setContentView( R.layout.activity_main );
        }
    }

Это мой фоновый Java-класс:

public class Background extends Application implements BootstrapNotifier {
    private static final String TAG = ".Background";
    private RegionBootstrap regionBootstrap;

@Override
    public void onCreate() {
        super.onCreate();
        Log.d(TAG, "App started up");
        BeaconManager 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=0215,i:4-19,i:20-21,i:22-23,p:24-24"));

        // wake up the app when any beacon is seen (you can specify specific id filers in the parameters below)
        Region region = new Region("com.example.myapp.boostrapRegion", null, null, null);
        regionBootstrap = new RegionBootstrap(this, region);
    }

    @Override
    public void didDetermineStateForRegion(int arg0, Region arg1) {
        // Don't care
    }

    @Override
    public void didEnterRegion(Region arg0) {
        Log.d(TAG, "Got a didEnterRegion call");
        // This call to disable will make it so the activity below only gets launched the first time a beacon is seen (until the next time the app is launched)
        // if you want the Activity to launch every single time beacons come into view, remove this call.
        //regionBootstrap.disable();

        Intent intent = new Intent(this, MainActivity.class);
        // IMPORTANT: in the AndroidManifest.xml definition of this activity, you must set android:launchMode="singleInstance" or you will get two instances
        // created when a user launches the activity manually and it gets launched from here.

        Toast.makeText(getApplicationContext(), "A Beacon is detected", Toast.LENGTH_LONG).show();
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        this.startActivity(intent);
    }

    @Override
    public void didExitRegion(Region arg0) {
        // Don't care
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...