"Unable to connect to device" with bluetoothSerial.connect()
JeremDlcn opened this issue · 3 comments
I made an ionic app with a connection between a Android phone and a HC-05 on an Arduino Uno.
When i use the method .connect() with a .subscribe() in order to connect the device or catch error, the only error that i console.log is "Unable to connect to device".
Is there any way to know more about my error because a message with "unable to connect" don't help me to resolve my problem
Thanks.
import { Component } from '@angular/core';
import { BluetoothSerial } from '@ionic-native/bluetooth-serial/ngx';
import { AlertController, ToastController } from '@ionic/angular';
@Component({
selector: 'app-home',
templateUrl: 'home.page.html',
styleUrls: ['home.page.scss'],
})
export class HomePage {
pairedList: pairedlist;
listToggle: boolean = false;
pairedDeviceID: number = 0;
dataSend: string = "";
constructor(private alertCtrl: AlertController, private bluetoothSerial: BluetoothSerial, private toastCtrl: ToastController) {
this.checkBluetoothEnabled();
}
checkBluetoothEnabled() {
this.bluetoothSerial.isEnabled().then(success => {
this.listPairedDevices();
}, error => {
this.showError("Please Enable Bluetooth")
});
}
listPairedDevices() {
this.bluetoothSerial.list().then(success => {
this.pairedList = success;
this.listToggle = true;
}, error => {
this.showError("Please Enable Bluetooth")
this.listToggle = false;
});
}
selectDevice() {
let connectedDevice = this.pairedList[this.pairedDeviceID];
if (!connectedDevice.address) {
this.showError('Select Paired Device to connect');
return;
}
let address = connectedDevice.address;
let name = connectedDevice.name;
this.connection(address);
}
connection(address: string) {
// Attempt to connect device with specified address, call app.deviceConnected if success
this.bluetoothSerial.connect(address).subscribe(success => {
this.deviceConnected();
this.showToast("Successfully Connected");
}, error => {
console.log("Connection Error" + error);
this.showError("Error:Connecting to Device");
});
}
deviceConnected() {
// Subscribe to data receiving as soon as the delimiter is read
this.bluetoothSerial.subscribe('\n').subscribe(success => {
this.handleData(success);
this.showToast("Connected Successfullly");
}, error => {
this.showError(error);
});
}
deviceDisconnected() {
// Unsubscribe from data receiving
this.bluetoothSerial.disconnect();
this.showToast("Device Disconnected");
}
handleData(data) {
this.showToast(data);
}
sendData() {
this.dataSend += '\n';
this.showToast(this.dataSend);
this.bluetoothSerial.write(this.dataSend).then(success => {
this.showToast(success);
}, error => {
this.showError(error)
});
}
async showError(error) {
const alert = await this.alertCtrl.create({
message: 'Error',
subHeader: error,
buttons: ['Dismiss']
});
await alert.present();
}
async showToast(msj: string) {
const toast = await this.toastCtrl.create({
message: msj,
duration: 1000
});
toast.present();
}
}
interface pairedlist {
"class": number,
"id": string,
"address": string,
"name": string
}```
I am getting the same information
can someone please guide ?
thanks for little help.
In order to connect a client, you have to start a server socket on the other device.
The code that manage the sever part is disabled in this library.
So, you can only build a client.
I succeed to make it working from Android to Android by uncomenting a part code and by changing UUID.
To sum up: this library is not complete, it has been build for a specific use case (connection with Arduino) and seems to not be maintained anymore.
It's a shame that ionic proposes it as a bluetooth serial solution in their documentation!
I had similar issue and battled for hours but it turns out all I had to do was connect and pair my phone and bluetooth printer manually from my phone bluetooth settings and then came back to the app to proceed and worked fine