1
votes

I have an app with a bluetoothserial subscribeRawData listener in my app.component(probably bad placement, but I needed it to be accessable from all app). There is a page with segments, which has some custom event listeners which I use to transfer data from bluetooth listeners.

I tried to make there two segments. And it worked like this:

Page loads with default segment open. It gets data from bluetooth properly. If I try to change segment, page stops responding until I send more data by bluetooth. Then the segment changes. And if I try to change it back, it does nothing until I tap anywhere on the page.

All code works well if I don't use segments and everything is ok when I use randomizers instead of bluetoothserial listeners.

Will provide code if needed.

1

1 Answers

0
votes

If you want your data accessible from anywhere in the app, you need to create a provider, which you can then inject in all your components. And it's best if you create a subject, to which you can subscribe anywhere in your app. That subject emits values that it gets from BT.

bt-prodiver.ts:

@Injectable()
export class BTProvider {
    public dataSubject: Subject<any> = new Subject();
}

You need to inject it into app.module.ts:

@NgModule({
providers: [
    BTProvider
]
})
export class AppModule {}

In components you then subscribe to your subject (Which emits values somewhere in your code) using this.dataSubject.next(data);

component:

ionViewDidLoad() {

    this.btProvider.dataSubject
            .subscribe((data) => {
              console.log("Got BT data");
              console.log(data);
            })
  }

tl;dr; Use a service!