The proper way to implement Bluetooth background service

Hi guys. We have a bluetooth android project where we have to do a bluetooth scan between certain intervals, (I am using BluethoothLE) and send information to the backend server if the user is near a BLE beacon. This is all working fine, but now i am running into a wall with keeping the service running when the user presses the power button. What is the correct way to implement this kind of functionality? I tried to look into backgroundmode and foreground service but it really didn’t give me a clear picture.