我正在开发一个离子应用程序,该应用程序可以从蓝牙智能腕带读取心律和血压。我使用了离子型本机ble插件来访问蓝牙功能。就我而言,发现并连接到设备工作正常。但是,当我尝试阅读某些服务时,它会提示“无法获得特定服务”。在我的代码testService()
中,我只是创建了一个功能以从设备读取指定的服务。
testService()函数
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)
);
}
这是我的整个JavaScript代码。
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();
}
}
有人可以帮助我确定我在这里做错了什么吗?
答案 0 :(得分:0)
在对这个问题进行了更多研究之后,终于找到了答案。我将其发布给任何对未来感兴趣的人。
事实证明,许多蓝牙设备不使用默认服务和特征性UUID。在上面的代码中,我正在使用默认服务和特征性UUID(“ 180D”,“ 2A37”)。我用来测试代码的设备甚至没有这样的服务。这就是为什么我得到Failed to get the particular service
。
因此,解决方案是以某种方式找出设备的服务和特征性UUID,并为这些特定值编写代码。