Getting the Value of a Variable from a Provider or Page in Ionic 2

I'm currently facing an issue with my Ionic 2 project and I'm hoping someone here can provide some assistance. The problem lies within a provider that I have created which contains a function for BLE. I have successfully included this provider in one of my pages and can call the function without any issues. However, I am now trying to access a public variable within the provider. This variable represents the connection status (true or false) and I need to retrieve it from my page to determine if I am connected to BLE or not.

Below is the code snippet:

BleProvider.ts

@Injectable()
export class BleProvider {

public isConnected:boolean;

constructor(public http: Http) {
 console.log('Hello BleProvider Provider');
}

connectToDevice(device){
 this.isConnected = true;
}
}

My page Login.ts

import { BleProvider } from '../../providers/ble/ble'
@IonicPage()
@Component({
selector: 'page-login',
templateUrl: 'login.html',
})
@NgModule({
providers: [BleProvider],
})

export class LoginPage {

constructor(public ble: BleProvider){
 ble.connectToDevice();
}

function testIsConnected(){
console.log("isConnected value: " + this.ble.isConnected);
}
}

Upon testing, the console log returns "isConnected value: undefined". Even when I set the isConnected variable in the provider constructor (e.g. this.isConnected = false), calling the connectToDevice function still returns false instead of true. Can anyone shed light on what might be causing this and how to resolve it?

Answer №1

Expanding on my previous statement, I believe that this solution would be suitable for your needs.

constructor(public ble: BleProvider){
}

ngOnInit(){
    this.ble.connectToDevice()
}

I trust that this information proves to be beneficial to you.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the mechanism by which AngularJS manages all of my module dependencies with jest?

Updated: Since this is an older project, I didn't use any module loader. Instead, I imported all dependencies in my index.html using the script tag. The structure of my AngularJS looks like this: app.js angular.module('app', ['Loca ...

Transform the user input fields into an array format and store it in the Local Storage for

I am working on converting multiple input fields into an array so that I can store them in Local Storage. Using a jQuery script, each input field is scanned and assigned a name based on its ID. HTML: <input class="field" type="checkbox" id="field-01" ...

Modifications performed on the Xhtml generated from xml and XSLT should be mirrored back into the original XML document

After transforming an XML file with xsl and loading it into a browser as html, I am making the html editable using the content editable attribute of html5. How can I transform their edits back to the original xml document, even if they add new nodes to e ...

How do I properly type when extending Button and encountering an error about a missing component property?

Currently in the process of transitioning from MUI v3 to v4. My challenge lies with some Button components that are wrapped and have additional styling and properties compared to the standard Material UI Button component. Ever since upgrading to v4, I&apos ...

Are there any disadvantages to utilizing bindActionCreators within the constructor of a React component when using Redux?

I have come across numerous instances where the bindActionCreators function is used within the mapDispatchToProps function as shown below: ... const mapDispatchToProps = (dispatch, props) => ({ actions: bindActionCreators({ doSomething: som ...

Is there a way to extract the href URL along with a countdown timer?

Is it possible to create a button with a 10-second timer without using the onclick function? I want the button to start a countdown when clicked and after the countdown is complete, redirect to the URL specified in the href attribute. You can test this fun ...

Encountering an issue in REACTJS where the error message "TypeError: navigate.push is not a function"

I'm currently working on setting up the homepage for my react.js website. Everything looks good with my layout and the code compiles successfully. However, when I try to click on a button, I encounter an error on the live website application: TypeErr ...

What is the method for transferring the chosen value from a select element into a textarea?

I am currently working on a feature to allow users to select a color of a shirt from a dropdown menu and have it added to a textarea when they click a button. If anyone has advice on how to achieve the same functionality with a group of radio buttons, it ...

What is the best way to monitor changes in the status and values within a FormArray

Having an issue with observing statusChanges/valueChanges in a reactive form using form-array. When the form values change, I want to subscribe and perform actions accordingly. form = new FormGroup({ users: new FormArray([ new FormGroup({ ...

Issue arose when attempting to remove an item from an array within React

I have a handleAd function that adds new components to an array, and I also have a handleDelete function that is supposed to remove the selected element from the array. I am generating unique keys for each element to prevent deletion issues. Initially, th ...

Optimizing the loading of flash objects to improve website

I have successfully implemented the jQuery lazy-loading plugin to defer loading of images below the fold on a large web page. This method works well for images, but now I am looking to apply the same technique to a large Flash object that is also positione ...

Struggling to implement validation in the Ajax form, still encountering issues with getting it to

Having previously posted here without success, the answers provided did not solve my issue. I am encountering a problem with AJAX in a form where error or success messages are not displaying. If anyone can identify what I might have missed, I would great ...

Verify if items in a list contain a source from a variable and then execute code - jQuery / JavaScript

My goal is to compare the variable SRC to the image sources in a list from my HTML. If it matches, I want to execute a specific function. Unfortunately, it appears that this process is not functioning correctly. For a live example, please visit the follow ...

Generate a request to load JSON data

On my webpage, I have several external JSON files that need to be loaded. I'm looking for a way to specify the order in which they should be loaded. Using JavaScript for this task, here is an example: const func1 = () => { $.getJSON(json1, re ...

Changing the direction of input text from right to left: A step-by-step guide

Is there a way to reverse text in HTML? For example, if I input "apple" can it be automatically changed to "elppa"? I have attempted some solutions but they only affect the text direction! https://i.sstatic.net/ce2Fv.jpg ...

What are the advantages of utilizing NGRX over constructor-injected services?

Have you ever wondered about the benefits of using NGRX or NGXS for an Angular application instead of constructor injected services to manage component IO? Is it simply to prevent mutation of component properties references without replacing the entire pr ...

What is the method to adjust the colors of the Y-Axis for values that are either above or below zero in Chart.js 2.6?

Is there a way to dynamically change the colors of bars in a chart created using chart.js 2.6 based on whether the values are above or below zero? The data for the graph series is currently generated randomly but will eventually come from a database query. ...

Experiencing difficulties with jquery compatibility in Internet Explorer

I've encountered a puzzling issue with jQuery on Internet Explorer. It works intermittently, unlike the consistent performance on Chrome and Firefox. I'm baffled by the root of this problem. Instead of linking to an external jQuery source file, ...

How can you modify Jquery show_hide so that the page automatically scrolls to the bottom of the concealed field when the button is clicked?

On my website, there is a button that reveals a map when clicked. The button is visible when the page loads, but once clicked, the map slides open and goes out of sight since the page loads at the top as usual. My goal is to have the page automatically scr ...

Using Jquery to preserve the selected value in a radio button

Having two radio buttons, <div class="class1"> <span><input name="chk1" type="radio" checked="checked" id="check1"/>Option 1</span> <span><input name="chk2" type="radio" id="check2"/>Option 2</span> </div> ...