Version that doesn't live long

This commit is contained in:
Ruben van de Ven 2018-10-22 20:46:03 +02:00
parent 732763a328
commit 9af67a8d65
4 changed files with 107 additions and 27 deletions

View file

@ -28,4 +28,7 @@ dependencies {
implementation files("libs/antpluginlib_3-6-0.jar")
implementation files("libs/fit_16.00.jar")
implementation 'com.koushikdutta.async:androidasync:2.+'
}

View file

@ -3,6 +3,8 @@
package="com.rubenvandeven.heartbeatstreamer">
<uses-feature android:name="android.hardware.bluetooth_le" android:required="true" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<application
android:allowBackup="true"

View file

@ -16,6 +16,8 @@ import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
@ -37,15 +39,20 @@ import com.dsi.ant.plugins.antplus.pccbase.AntPluginPcc.IPluginAccessResultRecei
import com.dsi.ant.plugins.antplus.pccbase.AntPlusLegacyCommonPcc.ICumulativeOperatingTimeReceiver;
import com.dsi.ant.plugins.antplus.pccbase.AntPlusLegacyCommonPcc.IManufacturerAndSerialReceiver;
import com.dsi.ant.plugins.antplus.pccbase.AntPlusLegacyCommonPcc.IVersionAndModelReceiver;
import com.koushikdutta.async.callback.CompletedCallback;
import com.koushikdutta.async.future.Future;
import com.koushikdutta.async.http.AsyncHttpClient;
import com.koushikdutta.async.http.WebSocket;
import com.rubenvandeven.heartbeatstreamer.R;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.EnumSet;
/**
* Base class to connects to Heart Rate Plugin and display all the event data.
*/
public abstract class Activity_HeartRateDisplayBase extends Activity
public abstract class Activity_HeartRateDisplayBase extends AppCompatActivity
{
protected abstract void requestAccessToPcc();
@ -78,6 +85,9 @@ public abstract class Activity_HeartRateDisplayBase extends Activity
TextView tv_dataStatus;
TextView tv_rrFlag;
TextView tv_ws;
String lastMsg = "";
@Override
protected void onCreate(Bundle savedInstanceState)
@ -85,6 +95,9 @@ public abstract class Activity_HeartRateDisplayBase extends Activity
super.onCreate(savedInstanceState);
handleReset();
Log.i("HeartRate", "Created WS");
}
/**
@ -131,6 +144,7 @@ public abstract class Activity_HeartRateDisplayBase extends Activity
tv_dataStatus = (TextView)findViewById(R.id.textView_DataStatus);
tv_rrFlag = (TextView)findViewById(R.id.textView_rRFlag);
tv_ws = (TextView)findViewById(R.id.textView_ws);
//Reset the text display
tv_status.setText(status);
@ -165,6 +179,26 @@ public abstract class Activity_HeartRateDisplayBase extends Activity
*/
public void subscribeToHrEvents()
{
Future<WebSocket> fws = AsyncHttpClient.getDefaultInstance().websocket("ws://heartbeat.rubenvandeven.com:8888/ws", "my-protocol", new AsyncHttpClient.WebSocketConnectCallback() {
@Override
public void onCompleted(Exception ex, final WebSocket webSocket) {
if (ex != null) {
ex.printStackTrace();
// TODO: retry
return;
}
webSocket.send("Connect!");
webSocket.setClosedCallback(new CompletedCallback() {
@Override
public void onCompleted(Exception ex) {
//unsubscribe before triggering resubscription
hrPcc.subscribeHeartRateDataEvent(null);
subscribeToHrEvents();
}
});
hrPcc.subscribeHeartRateDataEvent(new IHeartRateDataReceiver()
{
@Override
@ -176,12 +210,24 @@ public abstract class Activity_HeartRateDisplayBase extends Activity
final String textHeartRate = String.valueOf(computedHeartRate)
+ ((DataState.ZERO_DETECTED.equals(dataState)) ? "*" : "");
// Mark heart beat count and heart beat event time with asterisk if initial value
final String textHeartBeatCount = String.valueOf(heartBeatCount)
+ ((DataState.INITIAL_VALUE.equals(dataState)) ? "*" : "");
final String textHeartBeatEventTime = String.valueOf(heartBeatEventTime)
+ ((DataState.INITIAL_VALUE.equals(dataState)) ? "*" : "");
final String msg = String.format("{\"rate\":\"%s\", \"count\":\"%s\", \"time\":\"%s\"}", textHeartRate, textHeartBeatCount, textHeartBeatEventTime);
if(msg == lastMsg) {
Log.i("HeartRate", "Skip duplicate");
return;
}
lastMsg = msg;
webSocket.send(msg);
Log.i("HeartRate", "Sent: " + msg);
runOnUiThread(new Runnable()
{
@Override
@ -194,11 +240,17 @@ public abstract class Activity_HeartRateDisplayBase extends Activity
tv_heartBeatEventTime.setText(textHeartBeatEventTime);
tv_dataStatus.setText(dataState.toString());
tv_ws.setText(lastMsg);
}
});
}
});
}
});
hrPcc.subscribePage4AddtDataEvent(new IPage4AddtDataReceiver()
{
@Override

View file

@ -394,6 +394,29 @@
</RelativeLayout>
<RelativeLayout
android:id="@+id/viewGroup_ws"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#EBEBEB"
android:paddingBottom="3dp"
android:paddingTop="3dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="Last msg:" />
<TextView
android:id="@+id/textView_ws"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="---" />
</RelativeLayout>
</LinearLayout>