11package com .example .brainflowplot ;
22
3+ import android .content .Context ;
4+ import android .content .Intent ;
5+ import android .content .SharedPreferences ;
36import android .os .Bundle ;
7+ import android .util .Log ;
8+ import android .view .View ;
9+ import android .widget .Toast ;
410
511import com .google .android .material .bottomnavigation .BottomNavigationView ;
612
915import androidx .navigation .Navigation ;
1016import androidx .navigation .ui .AppBarConfiguration ;
1117import androidx .navigation .ui .NavigationUI ;
18+ import androidx .preference .PreferenceManager ;
19+
20+ import brainflow .BoardShim ;
21+ import brainflow .BrainFlowError ;
22+ import brainflow .BrainFlowInputParams ;
1223
1324public class DataActivity extends AppCompatActivity {
1425
26+ public BoardShim boardShim = null ;
27+ public int samplingRate = 0 ;
28+ public int [] channels = null ;
29+
30+ private boolean isTryingToConnect = false ;
31+
1532 @ Override
1633 protected void onCreate (Bundle savedInstanceState ) {
1734 super .onCreate (savedInstanceState );
1835 setContentView (R .layout .activity_data );
1936 BottomNavigationView navView = findViewById (R .id .nav_view );
2037 // Passing each menu ID as a set of Ids because each
2138 // menu should be considered as top level destinations.
39+
40+ // comment out these two methods for theme wo actionbar
41+ /*
2242 AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
23- R .id .navigation_home , R .id .navigation_dashboard , R .id .navigation_notifications )
43+
44+ R.id.navigation_dataplot, R.id.navigation_psdplot, R.id.navigation_bandpowerplot)
2445 .build();
46+ */
2547 NavController navController = Navigation .findNavController (this , R .id .nav_host_fragment );
26- NavigationUI .setupActionBarWithNavController (this , navController , appBarConfiguration );
48+ // NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
2749 NavigationUI .setupWithNavController (navView , navController );
50+
51+ // read settings
52+ SharedPreferences prefs = PreferenceManager .getDefaultSharedPreferences (this );
53+ int boardId = Integer .valueOf (prefs .getString (getString (R .string .board_id_key ), "-1" ));
54+ String ipAddr = prefs .getString (getString (R .string .ip_address_key ), "" );
55+ int ipPort = 0 ;
56+ try {
57+ ipPort = Integer .valueOf (prefs .getString (getString (R .string .ip_port_key ), "0" ));
58+ } catch (Exception e ) {
59+ // do nothing
60+ }
61+ String dataType = prefs .getString (getString (R .string .data_type_key ), "" );
62+
63+ boolean connected = false ;
64+ try {
65+ BrainFlowInputParams params = new BrainFlowInputParams ();
66+ params .ip_address = ipAddr ;
67+ params .ip_port = ipPort ;
68+ boardShim = new BoardShim (boardId , params );
69+ boardShim .prepare_session ();
70+ boardShim .start_stream ();
71+ connected = true ;
72+ } catch (Exception e ) {
73+ Context context = getApplicationContext ();
74+ CharSequence text = "Error occurred, validate provided parameters and your board" ;
75+ int duration = Toast .LENGTH_LONG ;
76+ Toast toast = Toast .makeText (context , text , duration );
77+ toast .show ();
78+ Log .e (getString (R .string .log_tag ), e .getMessage ());
79+ connected = false ;
80+ }
81+ if (!connected ) {
82+ // if failed to connect back to settings page
83+ Intent myIntent = new Intent (this , SettingsActivity .class );
84+ startActivity (myIntent );
85+ }
86+ }
87+
88+ @ Override
89+ protected void onDestroy () {
90+ try {
91+ if (boardShim != null ) {
92+ boardShim .release_session ();
93+ }
94+ } catch (BrainFlowError e ) {
95+ Log .e (getString (R .string .log_tag ), e .getMessage ());
96+ }
97+ super .onDestroy ();
2898 }
2999
30100}
0 commit comments