地理围栏以检测其他设备

时间:2017-12-05 10:02:45

标签: android monitoring android-geofence

我们正在尝试构建地理围栏以便我们可以检测围栏中的其他设备(如果我们需要在要检测的设备上安装某些应用程序,那也没关系)。我们正在关注this tutorial以及the respective github repository

这是用于检测的代码,它工作正常......因为它检测到相同的设备,它创建围栏进入和退出它。我们想要的是检测其他设备。

protected void onHandleIntent(Intent intent) {
    GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent);
    // Handling errors
    if ( geofencingEvent.hasError() ) {
        String errorMsg = getErrorString(geofencingEvent.getErrorCode() );
        Log.e( TAG, errorMsg );
        return;
    }

    int geoFenceTransition = geofencingEvent.getGeofenceTransition();
    // Check if the transition type is of interest
    if ( geoFenceTransition == Geofence.GEOFENCE_TRANSITION_ENTER ||
            geoFenceTransition == Geofence.GEOFENCE_TRANSITION_EXIT ) {
        // Get the geofence that were triggered
        List<Geofence> triggeringGeofences = geofencingEvent.getTriggeringGeofences();

        String geofenceTransitionDetails = getGeofenceTransitionDetails(geoFenceTransition, triggeringGeofences );

        // Send notification details as a String
        sendNotification( geofenceTransitionDetails );
    }
}


private String getGeofenceTransitionDetails(int geoFenceTransition, List<Geofence> triggeringGeofences) {
    // get the ID of each geofence triggered
    ArrayList<String> triggeringGeofencesList = new ArrayList<>();
    for ( Geofence geofence : triggeringGeofences ) {
        triggeringGeofencesList.add( geofence.getRequestId() );
    }

    String status = null;
    if ( geoFenceTransition == Geofence.GEOFENCE_TRANSITION_ENTER )
        status = "Entering ";
    else if ( geoFenceTransition == Geofence.GEOFENCE_TRANSITION_EXIT )
        status = "Exiting ";
    return status + TextUtils.join( ", ", triggeringGeofencesList);
}

private void sendNotification( String msg ) {
    Log.i(TAG, "sendNotification: " + msg );

    // Intent to start the main Activity
    Intent notificationIntent = MainActivity.makeNotificationIntent(
            getApplicationContext(), msg
    );

    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    stackBuilder.addParentStack(MainActivity.class);
    stackBuilder.addNextIntent(notificationIntent);
    PendingIntent notificationPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);


    // Creating and sending Notification
    NotificationManager notificatioMng =
            (NotificationManager) getSystemService( Context.NOTIFICATION_SERVICE );
    notificatioMng.notify(
            GEOFENCE_NOTIFICATION_ID,
            createNotification(msg, notificationPendingIntent));

}

// Create notification
private Notification createNotification(String msg, PendingIntent notificationPendingIntent) {
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
    notificationBuilder
            .setSmallIcon(R.drawable.ic_action_location)
            .setColor(Color.RED)
            .setContentTitle(msg)
            .setContentText("Geofence Notification!")
            .setContentIntent(notificationPendingIntent)
            .setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE | Notification.DEFAULT_SOUND)
            .setAutoCancel(true);
    return notificationBuilder.build();
}

我们希望更改,以便一个设备创建地理围栏,当其他设备进入围栏时,它们可以由创建并监视围栅的设备检测到。如果我们需要在要检测的设备上安装应用程序,则没有问题。

到目前为止,我们所取得的成就是创建地理围栏的设备会自动进入或退出围栏...

0 个答案:

没有答案