Reputation: 1949
I'm working on an ionic app which reads heart pulse and blood pressure from a bluetooth smart wristband. I used ionic native ble plugin to access bluetooth features. In my case discovering and connecting to a device works fine. But when I try to read some service it tells "Failed to get the particular service". In my code testService()
is just a function I created to read a specified service from the device.
testService() function
testService(deviceId, service, charac) {
// Subscribe for notifications
this.ble.startNotification(deviceId, service, charac).subscribe(
data => this.onValueChange(data, service+"|"+charac),
() => this.alertMsg('Unexpected Error', 'Failed to subscribe for ' + service + " | " + charac)
);
// Read the current value
this.ble.read(deviceId, service, charac).then(
data => {
this.onValueChange(data, service+"|"+charac);
},
() => this.alertMsg('Unexpected Error', 'Failed to get ' + service + " | " + charac)
);
}
This is my whole javascript code.
import { Component, ViewChild, NgZone } from '@angular/core';
import { IonicPage, NavController, NavParams, AlertController, ToastController } from 'ionic-angular';
import { BLE } from '@ionic-native/ble';
import { Storage } from '@ionic/storage';
@IonicPage()
@Component({
selector: 'page-heart-rate',
templateUrl: 'heart-rate.html',
})
export class HeartRatePage {
private isConnected: boolean;
conDevice: any; //connected device
constructor(public navCtrl: NavController,
public navParams: NavParams,
private ble: BLE,
private storage: Storage,
private alertCtrl: AlertController,
private toastCtrl: ToastController,
private ngZone: NgZone
) {
}
ionViewDidEnter() {
this.isConnected = false;
this.storage.get('last-connected-device').then((val) => { //check if a device is connected
this.ble.isConnected(String(val.id)).then(
() => {
this.conDevice = val;
this.isConnected = true;
this.testService(this.conDevice.id, "180D", "2A37"); //reading the service (heart rate)
},
() => {
}
);
}).catch( err => {
});
}
//this method will read device for a specified service
testService(deviceId, service, charac) {
// Subscribe for notifications
this.ble.startNotification(deviceId, service, charac).subscribe(
data => this.onValueChange(data, service+"|"+charac),
() => this.alertMsg('Unexpected Error', 'Failed to subscribe for ' + service + " | " + charac)
);
// Read the current value
this.ble.read(deviceId, service, charac).then(
data => {
this.onValueChange(data, service+"|"+charac);
},
() => this.alertMsg('Unexpected Error', 'Failed to get ' + service + " | " + charac)
);
}
onValueChange(buffer:ArrayBuffer, text:string) {
var data = new Float32Array(buffer);
var dataString = String.fromCharCode.apply(null, data);
this.ngZone.run( () => {
let toast = this.toastCtrl.create({
message: 'value change: ' + text +'::'+ data[0],
duration: 2000
});
toast.present();
} );
}
//this method will popup an alert
alertMsg(title, message) {
let alert = this.alertCtrl.create({
title: title,
message: message,
buttons: [
{
text: 'Ok'
}
]
});
alert.present();
}
}
can someone help me to identify what I did wrong here?
Upvotes: 0
Views: 733
Reputation: 1949
After researching a little bit more about this question, finally I found the answer. I'm posting it here for anyone, who is interested in future.
It turns out to be, many of the Bluetooth devices don't use default service and characteristic UUIDs. In the above code, I'm using default service and characteristic UUIDs ("180D", "2A37"). The device I used to test my code even doesn't have such a service. That's why I got Failed to get the particular service
.
So the solution is to somehow find out the device's service and characteristic UUIDs and write the code for those particular values.
Upvotes: 1