Android Play Vibration from Foreground Service

Void95

What I am doing:

I am making one app using Android Beacon library. I am using RegionBootstrap for implementation of foreground ranging and transmission of Altbeacons.

What I want:

I want to make the phone vibrate for suppose 500 ms when I can find a beacon within certain meters of range even when my app is running as foreground service.

Logic:

Logic is simple, I am performing scanning at a specific interval. Suppose I found beacons A, B, C with distance 1m, 1.5m, 0.6m. Now my threshold distance is 0.8m so I will make the phone vibrate as long as the above condition satisfies, that means user has to go out to make the distance larger so as to stop vibration.

Code I used:

I have found one code snippet to make the vibration

import android.os.Vibrator;
...
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
// Vibrate for 500 milliseconds
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    v.vibrate(VibrationEffect.createOneShot(500, VibrationEffect.DEFAULT_AMPLITUDE));
} else {
    //deprecated in API 26 
    v.vibrate(500);
}

Issue I am facing:

This works perfectly when my app is open. Once I close the app and the foreground service keeps scanning and transmitting, the vibration doesn't occur.

I am new to android development, kindly help me to resolve this issue.

davidgyoung

Try sending a local notification with vibration on the beacon nearby event. This may overcome background restrictions on vibration on your phone model and give you a easy way to confirm the event is firing. If you do not want the visual notification to stick around, you can dismiss it after a few second delay. You can even resend the notification after a second or so to continue the vibration effect until the condition clears.

A friendly word of caution: take care that these Bluetooth distance estimates are accurate enough for your use case. Other apps like OnePointFive have made the mistake of sending too many false notifications, leading to very unhappy users. You will surely want to be cautious not to repeat their mistake. See the Misusing Bluetooth Estimates section of my blog post here.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to stop a foreground service from the notification in android?

Android - what is a foreground service? (vs regular service)

Android Background Wifi Scanning Foreground Service

Android, does foreground service keep app alive?

Android: How can I get the current foreground activity (from a service)?

Foreground service android wear

Android customize foreground service notification

Android Best-Way to communicate with a Foreground Service

Make a notification from a foreground service cancelable once the service is not in foreground anymore

Android foreground service notification not showing

Observe LiveData from foreground service

Clarification on Android foreground service

Android Foreground Service For Location

Remove notification icon of killed foreground service (Android)

Foreground Notification in service not working in Android 8.1

Can Android stop a service while app is in foreground?

Is Android thread started from foreground service persistent?

Android Q - Start foreground location service from the background

foreground service never stop xamarin android

Delphi android - Send data from local foreground service to application

How to play music on Android while activities are on foreground

How to properly use Firebase from a foreground service in Android

Android: Get foreground app info from service every time a particular app enters foreground

Android InstantApp: Foreground service

Sending a SMS from a foreground service

Foreground service not working in android oreo

Android Foreground Service() issue

Start camera from foreground service if START_ACTIVITIES_FROM_BACKGROUND granted for the app (Android 11+)

Android Foreground Service Notification Delayed