如何使用 cordova-plugin-ble-central API Scan( ) 从 ionic 1 到 ionic 3
How to use cordova-plugin-ble-central's API Scan( ) from ionic1 to ionic3
我想将项目从 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);
});
}
错误message:TypeError:无法读取属性未定义的“扫描”。
我不确定 Scan() 的用法是否已更改。
请告诉我如何解决。
我假设您正在使用 native BLE plugin for ionic 3。这是一个如何使用它的简单示例:
1) 在您的 app.module
中导入 BLE 模块
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)
我想将项目从 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);
});
}
错误message:TypeError:无法读取属性未定义的“扫描”。
我不确定 Scan() 的用法是否已更改。
请告诉我如何解决。
我假设您正在使用 native BLE plugin for ionic 3。这是一个如何使用它的简单示例:
1) 在您的 app.module
中导入 BLE 模块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)