question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

can't make background mode work

See original GitHub issue

My app is supposed to send a message to a specific BLE device when that device is in range. Ideally this should work even when the app is closed/in background mode. I used the simplest restoreStateFunction I could think of: it just makes a fetch request to an endpoint on my server. However, this is never called once the app goes to background. I created a sample repo to quickly replicate the problem, and here’s the relevant code. Tested on android/ios, debug/release and nothing works. Any idea about what I’m doing wrong?

Also, I only want the app to be turned on only if a specific device is in range, and I see this is configured only in startDeviceScan. Will the background mode take this into account, or the app will be turned on anytime a new ble device is in range?

import React from 'react';
import {BleManager} from "react-native-ble-plx";
import {PermissionsAndroid, Platform} from 'react-native';

export class BleComponent extends React.PureComponent {
    constructor() {
        super();
        if (Platform.OS === 'android') {
            PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.ACCESS_COARSE_LOCATION)
                .then(granted => {
                    if (granted !== 'granted') {
                        console.log('BLUETOOTH denied')
                    }
                    else {
                        this.initializeManager();
                    }
                })
        } else {
            this.initializeManager();
        }

    }

    initializeManager() {
        this.manager = new BleManager({
            restoreStateIdentifier: 'testBleBackgroundMode',
            restoreStateFunction: bleRestoredState => {
                fetch('http://restoreStateFunctionWasCalled.com')
            }
        });
        const subscription = this.manager.onStateChange((state) => {
            if (state === 'PoweredOn') {
                this.scanAndConnect();
            }
        }, true);
    }

    scanAndConnect() {
        console.log(('scan and connect'));
        this.manager.startDeviceScan(["0000fefb-0000-1000-8000-00805f9b34fb"], null, (error, device) => {
            if (error) {
                console.log(("BLE error"));
                return
            }
            console.log(device);
        });
    }

    render() {
        return null;
    }
}

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:3
  • Comments:17 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
priyanka-Sadhcommented, Jun 4, 2020

@7772 It’s great to hear form you that it’s worked. Even to put correct serviceUUID it doesn’t work. Can you please make a proper documentation on background mode os scan and can share, I think it will be help ful for lot of people. That will be grateful though 😃 Or someone can say it never work 😦

2reactions
7772commented, Nov 5, 2019

I have trouble on scanning device on background, but solved it.

  1. make sure turn on background mode on Xcode.
  2. make sure that properly pass service uuids parameter in startDeviceScan

I noticed what I should do after reading on this

The problem is the difference in scanning in foreground and background. 
When you are scanning for devices in the foreground you can scan for anything. 
In the background you must specify the actual service UUID you are scanning for. 
Ok, this isn't actually a problem as you know the UUID you are looking for.
Read more comments on GitHub >

github_iconTop Results From Across the Web

Configuring background execution modes - Apple Developer
Select one or more background execution modes using the corresponding checkboxes. For watchOS apps, choose the appropriate session type from the pop-up menu....
Read more >
can't make background mode work · Issue #127 - GitHub
On iOS background mode is tricky. Just adding special permission in XCode project allows you to do background work.
Read more >
Background Mode doesn't be added in Xcode - Stack Overflow
I want to add Background Mode to use Location Updates but I can't. ... the place of Background Mode but I don't know...
Read more >
Background Modes Tutorial: Getting Started - Kodeco
Most background modes won't work unless you enable specific capabilities that indicate that the app wants to run code in the background. The ......
Read more >
Background mode doesn't work - Ionic Forum
I have a problem with my app. The background mode does not work. It tells me that the plugin is not installed or...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found