我想将项目从ionic1迁移到ionic3,但是cordova-plugin-ble-central存在一些问题
ionic1
function quickScan( ) {
var q = $ble.scan([], 3);
q.then(function() {
/* Done scanning */
if (!sys.fsm.is("LISTING")) {
setNumScans(0);
return;
}
if (sys.numScans > 0)
setNumScans(sys.numScans - 1);
if (sys.numScans > 0) {
quickScan();
} else {
if (sys.fsm.is("LISTING")) {
sys.fsm.abort();
sys.listOkCb(null);
}
}
}, function(err) {
/* Scanning Error */
sys.fsm.abort();
sys.listErrCb(err);
}, function(dev) {
/* New device found */
addAndReportDevice(dev);
});
}
ionic3
quickScan( ) {
let q = this.Ble.scan([], 3)
q.toPromise().then(function() {
/* Done scanning */
if (!this.fsm.is("LISTING")) {
this.setNumScans(0);
return;
}
if (this.numScans > 0)
this.setNumScans(this.numScans - 1);
if (this.numScans > 0) {
this.quickScan();
}else {
console.log("BLE done scanning");
if (this.fsm.is("LISTING")) {
this.fsm.abort();
this.listOkCb(null);
}
}
},function(err) {
/* Scanning Error */
this.fsm.abort();
this.listErrCb(err);
/* New device found */
this.addAndReportDevice(this.dev);
});
}
错误消息:TypeError:无法读取未定义的属性“扫描”。
我不确定Scan()的用法是否已更改。
请告诉我如何解决。
答案 0 :(得分:1)
我假设您正在将native BLE plugin用于离子3。这是一个简单的示例,您可以使用它:
1)将BLE模块导入到您的app.module
import { BLE } from '@ionic-native/ble';
@NgModule({
declarations: [AppComponent],
providers: [
BLE
]
}
2)在您的页面组件中注入BLE模块
constructor( private ble: BLE ) { ... }
3)像这样扫描附近的BLE设备:
const bleServices = ["service-uuid-you-want-to-scan-for"];
this.ble.startScan(bleServices).subscribe(
device => { console.log('Discovered device:', JSON.stringify(device)); },
error => { console.log('Scan error:', error); }
);
之后,您可以使用设备ID this.ble.connect(device.id)