2017-10-30 5 views
1

私はこのプラグインhttps://github.com/don/BluetoothSerialを使用してBluetooth接続などを管理しています。私はarduino LeonardoでHC-06モジュールを使用しています。問題はモバイルアプリケーション上ですべてうまくいきます。ここ はhome.ts(接続呼び出すボタンがあるhome.htmlにIonic BluetoothシリアルSubscribeRawDataアップデートUI

connect() { 
    this.blt = this.bluetoothSerial.connect("XX:XX:XX:XX:XX:XX"); 
    this.blt.subscribe((data) => { 
     this._disable1 = false; 
     this._connected = true; 

     this.bluetoothSerial.subscribeRawData().subscribe((dt) => { 
      this.bluetoothSerial.read().then((dd) => { 
       this.onDataReceive(dd) 
      }); 
     }); 
    }, (error) => { 
     this._connected = false; 
     this.setStatus("Not Connected"); 
    }); 
} 

sendToHc(num) { 
    this.bluetoothSerial.write(""+num).then((ok) => { }); 
} 

dummyCheck() { 
    // Do nothing 
} 

onDataReceive(dd) { 
    this._debug += "\n" + JSON.stringify(dd); 

} 

)とパラメータでsendToHcを呼び出すこと別です。これまでのところすべてがOKです。問題は、subscribeRawDataがarduinoからの応答を読み込み、UIを自動的に更新しないことです。 dummyCheck()を呼び出すために3番目のボタンを挿入する必要があり、その後UIが更新されます。レスポンスを読むときに自動的にUIを更新する方法はありますか?

答えて

1

ネストされたサブスクリプションがAngularスコープ外にあり、アップデートが到着したときに変更検出が実行されないことは間違いありません。ダミーボタンを押すと、clickイベントが発生し、変更検出がトリガーされ、DOM上で更新が表示されます。あなたはChangeDetectorRefを使用して手動で変更の検出を実行することができます。

import { ChangeDetectorRef } from '@angular/core'; 
constructor(private cdr: ChangeDetectorRef) {} 

this.bluetoothSerial.subscribeRawData().subscribe((dt) => { 
    this.bluetoothSerial.read().then((dd) => { 
    this.onDataReceive(dd); 
    this.cdr.detectChanges(); // either here 
    }); 
}); 

onDataReceive(dd) { 
    this._debug += "\n" + JSON.stringify(dd); 
    this.cdr.detectChanges(); // or here 
} 

これに関するドキュメントはhere見つけることができます。そしてhereは、一般的な話題に関するとても良い記事です。

関連する問題