Advertisement
Guest User

Untitled

a guest
May 5th, 2015
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.15 KB | None | 0 0
  1. package mt.ant;
  2.  
  3. import android.app.Activity;
  4. import android.bluetooth.BluetoothAdapter;
  5. import android.content.Intent;
  6. import android.content.SharedPreferences;
  7. import android.os.Bundle;
  8. import android.support.v7.app.ActionBarActivity;
  9. import android.util.Log;
  10. import android.view.View;
  11. import android.widget.Button;
  12. import android.widget.TextView;
  13. import android.widget.Toast;
  14.  
  15. import mt.ant.AntPlus.AsyncHR;
  16. import mt.ant.BLE.BleHr;
  17.  
  18.  
  19. public class AddDevice extends ActionBarActivity {
  20.  
  21. String bleId;
  22. String antId;
  23. boolean antScanHr = false;
  24.  
  25. private TextView titleAnt;
  26. private TextView titleBle;
  27. private TextView titleGrs;
  28. private TextView idViewAnt;
  29. private TextView idViewBle;
  30. private TextView idViewGrs;
  31.  
  32. Button bleBtn;
  33. Button antBtn;
  34. Button grsBtn;
  35.  
  36. AsyncActivity asyncActivity;
  37. AsyncHR antHr;
  38. BleHr bleHr;
  39. public SharedPreferences sp;
  40.  
  41. @Override
  42. protected void onCreate(Bundle savedInstanceState) {
  43. super.onCreate(savedInstanceState);
  44. setContentView(R.layout.activity_add_device);
  45. idViewAnt = (TextView) findViewById(R.id.deviceidAnt);
  46. titleAnt = (TextView) findViewById(R.id.titleAnt);
  47. idViewBle = (TextView) findViewById(R.id.deviceidBle);
  48. titleBle = (TextView) findViewById(R.id.titleBle);
  49. titleGrs = (TextView) findViewById(R.id.titleGrs);
  50. idViewGrs = (TextView) findViewById(R.id.deviceidGrs);
  51. bleBtn = (Button) findViewById(R.id.btnBle);
  52. antBtn = (Button) findViewById(R.id.btnAnt);
  53. grsBtn = (Button) findViewById(R.id.btnGrs);
  54. bleBtn.setOnClickListener( new View.OnClickListener() {
  55. @Override
  56. public void onClick(View v) {
  57. activateBluetooth();
  58. bleHr.scanLeDevice(true, true);
  59. }
  60. });
  61. asyncActivity = new AsyncActivity();
  62. asyncActivity.doBindChannelService(this);
  63.  
  64.  
  65. antBtn.setOnClickListener(new View.OnClickListener() {
  66. @Override
  67. public void onClick(View v) {
  68. antHr.requestAccessToPcc();
  69. //asyncActivity.addNewChannel(false);
  70. }
  71. });
  72.  
  73. grsBtn.setOnClickListener(new View.OnClickListener() {
  74. @Override
  75. public void onClick(View v) {
  76. String id = ""+asyncActivity.newChannelInfo.broadcastData[0]+
  77. asyncActivity.newChannelInfo.broadcastData[1];
  78. Log.v("GrsID", id);
  79. setViewGrs(id);
  80. }
  81. });
  82.  
  83. antHr = new AsyncHR(this, 0);
  84. bleHr = new BleHr(this, 0);
  85.  
  86. antScanHr = true;
  87. sp = getSharedPreferences("values", Activity.MODE_PRIVATE);
  88. bleId = getDeviceID("bleHrID");
  89. antId = getDeviceID("antHrID");
  90.  
  91.  
  92. if(bleId != null){
  93. setViewBle(bleId);
  94. }
  95. if(antId != null){
  96. setViewAnt(antId);
  97. }
  98. }
  99.  
  100. /**
  101. * Ask the user to enabling bluetooth
  102. */
  103. private void activateBluetooth(){
  104. BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
  105. if (!mBluetoothAdapter.isEnabled()){
  106. Intent intentBtEnabled = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
  107. int REQUEST_ENABLE_BT = 1;
  108. startActivityForResult(intentBtEnabled, REQUEST_ENABLE_BT);
  109. }
  110. }
  111.  
  112. public void setViewGrs(final String id){
  113. lagreDeviceId("grsID", id);
  114. runOnUiThread(new Runnable() {
  115. @Override
  116. public void run() {
  117. titleGrs.setText("Globeracer sensor ID");
  118. idViewGrs.setText(id);
  119. Toast.makeText(getBaseContext(), "Lagret enhet: " + id, Toast.LENGTH_SHORT).show();
  120. }
  121. });
  122. }
  123.  
  124. public void setViewAnt(final String id){
  125. lagreDeviceId("antHrID", id+"");
  126. runOnUiThread(new Runnable() {
  127. @Override
  128. public void run() {
  129. titleAnt.setText("Heartrate monitor ANT+");
  130. idViewAnt.setText(id);
  131. Toast.makeText(getBaseContext(), "Lagret enhet: " + id, Toast.LENGTH_SHORT).show();
  132. }
  133. });
  134. }
  135.  
  136. public void setViewBle(final String id){
  137. lagreDeviceId("bleHrID", id);
  138. runOnUiThread(new Runnable() {
  139. @Override
  140. public void run() {
  141. titleBle.setText("Heartrate monitor BLE: ");
  142. idViewBle.setText(id);
  143. Toast.makeText(getBaseContext(), "Lagret enhet: " + id, Toast.LENGTH_SHORT).show();
  144. }
  145. });
  146. }
  147.  
  148. public void lagreDeviceId(String key, String id){
  149. Log.v("Status", "Lagrer deviceID");
  150. SharedPreferences.Editor editor = sp.edit();
  151. editor.putString(key, id);
  152. editor.commit();
  153. }
  154.  
  155. public String getDeviceID(String key){
  156. String id = sp.getString(key, null);
  157. return id;
  158. }
  159.  
  160. @Override
  161. public void onDestroy(){
  162. super.onDestroy();
  163.  
  164. antHr.close();
  165. asyncActivity.doUnbindChannelService();
  166.  
  167. }
  168.  
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement