Advertisement
Guest User

noninCollector.java

a guest
Mar 21st, 2016
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.18 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.FileWriter;
  3. import java.io.InputStream;
  4. import java.io.OutputStream;
  5. import java.lang.Exception;
  6. import java.util.UUID;
  7.  
  8. import android.bluetooth.BluetoothDevice;
  9. import android.bluetooth.BluetoothSocket;
  10. import android.util.Log;
  11.  
  12. class NoninCollector implements Runnable {
  13.  
  14. NoninCollector(NoninActivity activity) {
  15. this.activity = activity;
  16. }
  17.  
  18. void startRunning(BluetoothDevice noninDevice, File externalPath) {
  19. if (thread == null) {
  20. this.noninDevice = noninDevice;
  21. this.externalPath = externalPath;
  22. thread = new Thread(this);
  23. thread.start();
  24. }
  25. }
  26.  
  27. void stopRunning() {
  28. thread = null;
  29. }
  30.  
  31. public void run() {
  32. activity.showMessage("Started");
  33. InputStream noninIn = null;
  34. OutputStream noninOut = null;
  35. FileWriter fileWriter = null;
  36.  
  37. try {
  38. // Initialize
  39.  
  40. /*
  41. Call the method “connectToSensor” (this initializes bluetoothSocket)
  42. Retrieve the input and output streams from the socket (attribute bluetoothSocket of class BlueToothSocket )
  43. Write the byte sequence representing the data format to the sensor. Look at FORMAT_2 (also flush the stream, noninOut.flush())
  44. Read one byte from the input stream, use method read in noninIn
  45. If the reply equals ACK
  46. Create a FileWriter using the provided externalPath ( fileWriter = new FileWriter(externalPath); )
  47. Write a date stamp to the first line of the file ( fileWriter.write(new java.util.Date().toString() + "\r\n") )
  48. While not interrupted (i.e. thread != null)
  49. Read a packet, 5 bytes
  50. Extract the byte representing the pleth measurement from the byte array
  51. Write the relevant data to the file (for example fileWriter.write(pleth + "\r\n") )
  52. If you so wish, display the data
  53. Close the Bluetooth socket and the file writer (make sure this always happens)
  54. */
  55. // Pleth data sample frequency = 75 Hz, sent as
  56. // 3 frames per second (containing 25 packets).
  57.  
  58.  
  59. connectToSensor();
  60. noninIn = bluetoothSocket.getInputStream();
  61. noninOut = bluetoothSocket.getOutputStream();
  62.  
  63. noninOut.write(FORMAT_2);
  64. noninOut.flush();
  65.  
  66. byte[] reply = new byte[1];
  67. noninin.read(reply);
  68. if (reply[0] == ACK) {
  69. }
  70.  
  71. fileWriter = new fileWriter(externalPath);
  72. fileWriter.write(new java.util.Date().toString() + "\r\n");
  73. byte[] packet = new byte[5];
  74. int pleth, i = 0;
  75. while(thread != null) {
  76.  
  77. }
  78. noninIn.read(packet);
  79. pleth = unsignedByteToInt(packet[2]);
  80. fileWriter.write(pleth + "\r\n");
  81. if (i++ % 75 == 0) {
  82. Log.i("NoninActivity", "pleth = " + pleth);
  83. }
  84.  
  85.  
  86.  
  87. }
  88.  
  89.  
  90. catch (Exception e) {
  91. activity.showMessage("Error: " + e.toString());
  92. } finally {
  93. // Clean up
  94. thread = null;
  95. try {
  96. if (bluetoothSocket != null)
  97. bluetoothSocket.close();
  98. } catch (Exception e) {}
  99. try {
  100. if (fileWriter != null)
  101. fileWriter.close();
  102. } catch (Exception e) {}
  103. }
  104.  
  105. finally {
  106. thread = null;
  107. try {
  108. if (bluetoothSocket != null)
  109. double.close();
  110. } catch (Exception e) {
  111. try {
  112. if(fileWriter != null) fileWriter.close();
  113. }
  114. catch(Exception e) {}
  115. }
  116. }
  117.  
  118. }
  119.  
  120.  
  121. private Thread thread = null;
  122. private NoninActivity activity;
  123. private BluetoothDevice noninDevice;
  124. private BluetoothSocket bluetoothSocket;
  125. private File externalPath;
  126.  
  127. private void connectToSensor() throws Exception {
  128. // Android version. 4.0, to avoid Android prompting for password for BT device
  129. bluetoothSocket = noninDevice.createInsecureRfcommSocketToServiceRecord(STANDARD_SPP_UUID);
  130.  
  131. Log.i("SPPTask", "createInsecureRfcommSocketToServiceRecord");
  132. Log.i("NoninCollector", "createRfcommSocketToServiceRecord");
  133. bluetoothSocket.connect();
  134. Log.i("NoninCollector", "BT-socket connected...");
  135. }
  136.  
  137. private int unsignedByteToInt(byte b) {
  138. return (int) b & 0xFF;
  139. }
  140.  
  141. // The byte sequence to set sensor data format 2.
  142. private static final byte[] FORMAT_2 = { 0x02, 0x70, 0x04, 0x02, 0x02,
  143. 0x00, (byte) 0x78, 0x03 };
  144. private static final byte ACK = 0x06; // ACK from Nonin sensor
  145.  
  146. private static final UUID STANDARD_SPP_UUID = UUID
  147. .fromString("00001101-0000-1000-8000-00805F9B34FB");
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement