What could be causing the rapid breakage of the socket in Ionic 3's Bluetooth Serial after just a short period

Although the code appears to be functioning correctly, it loses connection shortly after establishing it.

This snippet contains the relevant code:

import { Component } from '@angular/core';
import { Platform, NavController, ToastController, Refresher } from 'ionic-angular';
import { BluetoothSerial } from '@ionic-native/bluetooth-serial';
import { Observable } from 'rxjs';

@Component({
  selector: 'page-opciones',
  templateUrl: 'opciones.html'
})
export class OpcionesPage {

  li_devices: Array<Object> = [];
  loading: any;
  mostrarSpiner = true;

 constructor(
 public platform: Platform, 
 public toastCtrl: ToastController, 
 public navCtrl: NavController, 
 public bluetoothSerial: BluetoothSerial) {
 platform.ready().then(() => {
  this.searchBluetooth().then((successMessage: Array<Object>) => {
    console.log("Devices " + JSON.stringify(successMessage));
    this.li_devices = successMessage;
    this.mostrarSpiner = false;
  },
  failureMessage => {
    const toast = this.toastCtrl.create({
      message: JSON.stringify(failureMessage),
      duration: 3000
    });
    toast.present();
    this.mostrarSpiner = false;
  });
});
}

search_bluetooth(){
  return new Promise((resolve, reject) => {
    this.bluetoothSerial.isEnabled().then(success =>{
      this.bluetoothSerial.discoverUnpaired().then(data => {
        if (data.length > 0){
          resolve(data);
        } else {
          reject('No devices found');
        }
      }).catch((error) => {
      console.log("Error " + JSON.stringify(error));
      reject('Bluetooth not available on this platform');
    });
  }, failure => {
    reject('Bluetooth not available');
  });
});
}

refresh_bluetooth(refresher: Refresher){
if (refresher){
  this.searchBluetooth().then((successMessage: Array<Object>) => {
    this.li_devices = [];
    console.log("Devices " + JSON.stringify(successMessage));
    this.li_devices = successMessage;
    refresher.complete();
  },
  failureMessage => {
    const toast = this.toastCtrl.create({
      message: JSON.stringify(failureMessage),
      duration: 3000
    });
    toast.present();
    refresher.complete();
  });
}
}

check_connection(selection){
this.bluetoothSerial.isConnected().then(isConnected => {
  const toast = this.toastCtrl.create({
    message: "Already connected",
    duration: 3000
  });
  toast.present();
}, notConected => {
  this.connect(selection["address"]);
});
}

connect(mac_address: string){
console.log("Connecting to " + mac_address);
this.bluetoothSerial.connect(mac_address).subscribe((data: Observable<string>) => {
  console.log("Connect " + JSON.stringify(data));
  this.bluetoothSerial.available().then(data =>{
    console.log("Available " + JSON.stringify(data));
    this.bluetoothSerial.read().then(data =>{
      console.log("Read " + JSON.stringify(data));
    });
  });
}, fail => {
  console.log("Disconnected " + JSON.stringify(fail));
});
}

message: string = "";
sendMessages(){
this.bluetoothSerial.isConnected().then(isConnected => {
  this.bluetoothSerial.write(this.message);
  const toast = this.toastCtrl.create({
    message: "Message sent",
    duration: 3000
  });
  toast.present();
}, notConected => {
  const toast = this.toastCtrl.create({
    message: "You are not connected",
    duration: 3000
  });
  toast.present();
});
}
}

Below is the output for reference:

06-16 12:42:18.170 17996-17996/io.ionic.starter I/chromium: [INFO:CONSOLE(62200)] "Devices [{"name":"JLOZOYABT","address":"98:D3:33:80:AD:E5","id":"98:D3:33:80:AD:E5","class":7936},{"address":"65:68:12:29:A9:2B","id":"65:68:12:29:A9:2B","class":7936}]", source: file:///android_asset/www/build/main.js (62200)
...
[Various log messages regarding connectivity and disconnection]
...

The issue at hand remains unclear, as no relevant information has been discovered thus far.

Any assistance or insights would be greatly appreciated.

Answer №1

After some troubleshooting, I managed to resolve the issue by addressing a problem with my cell phone's Bluetooth settings. Simply clearing the cache and restarting the device did the trick.

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

Having trouble closing the phonegap application using the Back Button on an Android device

I've encountered an issue with my code for exiting the application. It works perfectly the first time, but if I navigate to other screens and then return to the screen where I want to close the app, it doesn't work. <script type="text/javascr ...

The significance of zone.js and rxjs within the context of Angular 2

As a newcomer to Angular2, I recently learned about zone.js and rxjs. I'm curious to know if they both serve the same purpose for handling asynchronous tasks, or if each has its own specific role. Can someone explain to me the exact reasons why zone.j ...

Removing punctuation from time duration using Moment.js duration format can be achieved through a simple process

Currently, I am utilizing the moment duration format library to calculate the total duration of time. It is working as expected, but a slight issue arises when the time duration exceeds 4 digits - it automatically adds a comma in the hours section (similar ...

What are some strategies for streamlining the structure of this Firebase database to reduce the number of classes

As a newcomer to Firebase, I've successfully set up my database and it's working well. However, there are some challenges that need addressing. Below is my database in JSON format, please take a look. { "Cohesion" : { "Situation ...

Receiving an error in Typescript when passing an object dynamically to a React component

Encountering a typescript error while attempting to pass dynamic values to a React component: Error message: Property 'title' does not exist on type 'string'.ts(2339) import { useTranslation } from "react-i18next"; import ...

The term "containerName" in SymbolInformation is utilized to represent the hierarchy of

In my quest to make the code outline feature work for a custom language, I have made progress in generating symbols and displaying functions in the outline view. However, my next challenge is to display variables under the respective function in the outlin ...

What is the specific directory for the AIDL file placement?

I have a quick question about AIDL files. I am currently working on a project where I am using another project as a library. This library contains a service that I want to bind to. My question is: if the service is in a different project, do I need to inc ...

Arranging an array of arrays based on the mm/dd/yyyy date field

Currently, I am facing an issue while attempting to sort data obtained from an API by date in the client-side view. Here is an example of the data being received: address: "1212 Test Ave" address2: "" checkNumber : "" city: "La Grange" country: "" email: ...

Efficient configuration for pnpm monorepo with TypeScript, vite, and rollup

Struggling to set up a monorepo using pnpm workspaces with typescript, vite for frontends, and rollup for backend microservices. Here's the current project structure: package.json <== all dependencies reside here tsconfig ...

The argument with type 'void' cannot be assigned to a parameter with type 'Action'

Just getting started with Typescript and using VSCode. Encountering the following Error: *[ts] Argument of type 'void' is not assignable to parameter of type 'Action'. (parameter) action: void Here is the code snippet causing the err ...

The expanded interfaces of Typescript's indexable types (TS2322)

Currently, I am in the process of learning typescript by reimagining a flowtype prototype that I previously worked on. However, I have hit a roadblock with a particular issue. error TS2322: Type '(state: State, action: NumberAppendAction) => State ...

TypeScript error: Cannot find property 'propertyName' in the 'Function' type

I encountered an issue with the TypeScript compiler when running the following code snippet. Interestingly, the generated JavaScript on https://www.typescriptlang.org/play/ produces the desired output without any errors. The specific error message I recei ...

Reordering Items in a ListView using Android and Java

I am currently in the process of learning how to develop apps for Android. I am working on a small app that allows users to add and delete items from a ListView. Here is what it looks like right now: I want the items in the ListView to always stay at the ...

Using TypeORM: Implementing a @JoinTable with three columns

Seeking assistance with TypeORM and the @JoinTable and @RelationId Decorators. Any help answering my question, providing a hint, or ideally solving my issue would be greatly appreciated. I am utilizing NestJS with TypeORM to create a private API for shari ...

How can I limit generic types to only be a specific subtype of another generic type?

Looking to create a data type that represents changes in an object. For instance: const test: SomeType = { a: 1, b: "foo" }; const changing1: Changing<SomeType> = { obj: test, was: { a: test.a }, now: { a: 3 ...

Guide to creating a Map with typescript

I've noticed that many people are converting data to arrays using methods that don't seem possible for me. I'm working with React and TypeScript and I have a simple map that I want to render as a list of buttons. Here is my current progres ...

Encountering an issue with Next.js, Typescript, and mongoose when attempting to use `let cached = global.mongoose

I attempted to create a cached mongoose connection for my Next.js + Typescript application, but the code I used was: let cached = global.mongoose; if (!cached) { cached = global.mongoose = { conn: null, promise: null }; } The use of global.mongoose res ...

How can I retrieve the height of a dynamically generated div in Angular and pass it to a sibling component?

My setup consists of a single parent component and 2 child components structured as follows: Parent-component.html <child-component-1 [id]="id"></child-component-1> <child-component-2></child-component-2> The child-compo ...

Remap Objects Function with Correct Return Data Type

After receiving data from another source via a post request in a large object, I need to extract specific fields and organize them into more precise objects with some fields remapped before inserting them into a database. Currently, I have a working solut ...

Guide to invoking the API prior to shutting down the browser window in Angular4

Currently, I am working on an Angular 4 application that consists of 5 components. My goal is to trigger an API call when the user closes the browser window from any one of these components. However, I have encountered an issue where the API does not get ...