Reputation: 17
I want to migrate a project from ionic1 to ionic3, but cordova-plugin-ble-central has some problems
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);
});
}
error message:TypeError: Cannot read property ‘scan’ of undefined.
I am not sure the usage of Scan() has changed.
Please tell me how to solve it.
Upvotes: 0
Views: 1150
Reputation: 722
I assume you are using native BLE plugin for ionic 3. Here is a simple example how you can use it:
1) Import the BLE module in your app.module
import { BLE } from '@ionic-native/ble';
@NgModule({
declarations: [AppComponent],
providers: [
BLE
]
}
2) In your page component inject the BLE module
constructor( private ble: BLE ) { ... }
3) Scan for nearby BLE devices like this:
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); }
);
Afterwards you can connect to your device using its device id this.ble.connect(device.id)
Upvotes: 1