2017-08-30 17 views
0
私は(低エネルギーブルートゥース)

「コールバック」機能を実行し、一度

私は(観察可能返す)BLEの通知をサブスクライブした後は、私はBLEにいくつかのメッセージを送りたいコルドバのBLEで働いている

デバイスは、これを実行するための最良の方法は、基本的に私はサブスクリプションのコードがサブスクリプションに戻って応答するように、サブスクリプションが行われた後に機能を実行する必要があります。

ble.startNotification(deviceId, uuid1, uuid2).subscribe(bufferData=> { 
    //do something with bufferData 
}) 

は今、この後、私はコールバックのような何かを実行したい、

.then(()=> { 
    //send message to device (only once), after the message is sent, the device will respond back and the `do something with bufferData` code will be run 
}) 

私は簡単にsetTimeoutを行うと、数秒後にデバイスにメッセージを送信し、もちろんそれが動作可能性があり、私はサブスクリプションが起こったことを確信した後、きちんとやりたいと思う(Observableのサブスクリプション)

答えて

1

create operatorを使用して既存の方法をラップし、新しいサブスクリプションごとに実行されるカスタムコードを追加することができます。

例を参照してください:

// emulate cordova with "dummy" bluetooth interface 
 
const BLE = { 
 
startNotification:() => Rx.Observable.interval(1000) 
 
} 
 

 
const wrappedBLE = (...params) => 
 
    Rx.Observable.create(observer => { 
 
    // constructor fn will be executed on every new subscribtion 
 

 
    const disposable = BLE.startNotification(...params).subscribe(observer); 
 

 
    // place code to send notification here, instead of console log 
 
    console.log('New subscriber for BLE with params: ', params); 
 

 
    return disposable; 
 
    }); 
 

 

 
wrappedBLE("param1", "param2", "param3") 
 
    .subscribe(e => console.log("received notification: ", e));
<script src="https://unpkg.com/[email protected]/bundles/Rx.min.js"></script>

関連する問題