次のコードを使用して、見つかったすべてのデバイスに対してアレイにデバイスを追加するBluetoothデバイスをスキャンします。Typescriptの配列に "push()"を入力します
devices: Observable<Array<string>>;
bluetoothAdd() {
this.isScanning = true;
var plusIcon = this.page.getViewById("add");
plusIcon.style.opacity = 0;
var self = this;
bluetooth.hasCoarseLocationPermission().then(
function (granted) {
if (!granted) {
bluetooth.requestCoarseLocationPermission();
} else {
bluetooth.startScanning({
serviceUUIDs: ["133d"],
seconds: 4,
onDiscovered: function (peripheral) {
console.log("Periperhal found with UUID: " + peripheral.UUID);
this.devices.push(peripheral); // <- Problem Line
}
}).then(function() {
console.log("scanning complete");
self.isScanning = false;
plusIcon.style.opacity = 1;
}, function (err) {
console.log("error while scanning: " + err);
});
this.isScanning = false;
}
});
}
しかし、このコードは、次のエラーがスローされます。
JavaScript error: file:///app/Pages/Home/home.component.js:99:37: JS ERROR TypeError: undefined is not an object (evaluating 'this.devices.push')
私は活字体で働いていますが、私はプッシュ機能は、JSの事を知っています。私がTypescriptでこれをどうやって行うのか分かりません - どうしたのですか?
これはTypeScriptとは関係ありません。 「これはあなたが期待するものではありません。 http://stackoverflow.com/questions/3127429/how-does-the-this-keyword-workを参照してください。あなたは 'self'を使うことができます。 – elclanrs