1

When i try track a start walking or a stoping walking in a Samsung device, the Awareness doesn't recognise and doesn't update the current state.

Activity:

public class MainActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {

private static final String TAG = "awareness_poc";

private GoogleApiClient googleApiClient;
private PendingIntent requestPendingIntent;

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

    Intent intent = new Intent(FenceReceiver.FENCE_RECEIVER_ACTION);
    requestPendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, intent, 0);
}

@Override
protected void onStart() {
    super.onStart();
    googleApiClient = new GoogleApiClient.Builder(this, this, this)
            .addApi(Awareness.API)
            .build();
    googleApiClient.connect();
}

public void removeFences(View view) {
    FenceUpdateRequest request = new FenceUpdateRequest.Builder()
            .removeFence(WALKING_START_KEY)
            .removeFence(WALKING_STOP_KEY)
            .build();

    Awareness.FenceApi.updateFences(googleApiClient, request)
            .setResultCallback(new ResultCallbacks<Status>() {
                @Override
                public void onSuccess(@NonNull Status status) {
                    Log.i(TAG, "Fences successfully removed.");
                }

                @Override
                public void onFailure(@NonNull Status status) {
                    Log.i(TAG, "Fences could NOT be removed.");
                }
            });
}

@Override
public void onConnected(@Nullable Bundle bundle) {
    AwarenessFence walkingStartFence = DetectedActivityFence.starting(DetectedActivityFence.WALKING);
    AwarenessFence walkingStopFence = DetectedActivityFence.stopping(DetectedActivityFence.WALKING);

    FenceUpdateRequest request = new FenceUpdateRequest.Builder()
            .addFence(WALKING_START_KEY, walkingStartFence, requestPendingIntent)
            .addFence(WALKING_STOP_KEY, walkingStopFence, requestPendingIntent)
            .build();

    Awareness.FenceApi.updateFences(googleApiClient, request)
            .setResultCallback(new ResultCallback<Status>() {
                @Override
                public void onResult(@NonNull Status status) {
                    if (status.isSuccess()) {
                        Log.i(TAG, "Fence was successfully registered.");
                    } else {
                        Log.e(TAG, "Fence could not be registered: " + status);
                    }
                }
            });
}

@Override
public void onConnectionSuspended(int i) {

}

@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {

}

}

BroadcastReceiver

ublic class FenceReceiver extends BroadcastReceiver {

public static final String FENCE_RECEIVER_ACTION =
        "com.iwsbrazil.lab.awarenesspoc.FENCE_RECEIVER_ACTION";
public static final String WALKING_START_KEY = "walking_start";
public static final String WALKING_STOP_KEY = "walking_stop";

@Override
public void onReceive(Context context, Intent intent) {
    FenceState fenceState = FenceState.extract(intent);

    if (fenceState.getCurrentState() == FenceState.TRUE) {
        if (TextUtils.equals(fenceState.getFenceKey(), WALKING_START_KEY)) {
            sendNotification(context, "You Started Walking", 1);
        } else if (TextUtils.equals(fenceState.getFenceKey(), WALKING_STOP_KEY)) {
            sendNotification(context, "You Stopped Walking", 2);
        }
    }
}

private void sendNotification(Context context, String text, int id) {

    NotificationCompat.Builder builder =
            new NotificationCompat.Builder(context)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setContentTitle("Awareness")
                    .setContentText(text)
                    .setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE | Notification.DEFAULT_LIGHTS);

    NotificationManager manager =
            (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    manager.notify(id, builder.build());

}

}

shkschneider
  • 17,833
  • 13
  • 59
  • 112
  • Did you get an answer? I tried a similar code on mi. That too also did not show movement when the device is moving. The still part is working fine – user4057066 Aug 11 '16 at 07:54
  • The problem it's Google Fit only can read in real time the data what was added for yourself, if another app add data, you will can read in the next day – Diego Cunha Aug 12 '16 at 18:52

0 Answers0