Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import java.io.FileWriter;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.lang.Exception;
- import java.util.UUID;
- import android.bluetooth.BluetoothDevice;
- import android.bluetooth.BluetoothSocket;
- import android.util.Log;
- class NoninCollector implements Runnable {
- NoninCollector(NoninActivity activity) {
- this.activity = activity;
- }
- void startRunning(BluetoothDevice noninDevice, File externalPath) {
- if (thread == null) {
- this.noninDevice = noninDevice;
- this.externalPath = externalPath;
- thread = new Thread(this);
- thread.start();
- }
- }
- void stopRunning() {
- thread = null;
- }
- public void run() {
- activity.showMessage("Started");
- InputStream noninIn = null;
- OutputStream noninOut = null;
- FileWriter fileWriter = null;
- try {
- // Initialize
- /*
- Call the method “connectToSensor” (this initializes bluetoothSocket)
- Retrieve the input and output streams from the socket (attribute bluetoothSocket of class BlueToothSocket )
- Write the byte sequence representing the data format to the sensor. Look at FORMAT_2 (also flush the stream, noninOut.flush())
- Read one byte from the input stream, use method read in noninIn
- If the reply equals ACK
- Create a FileWriter using the provided externalPath ( fileWriter = new FileWriter(externalPath); )
- Write a date stamp to the first line of the file ( fileWriter.write(new java.util.Date().toString() + "\r\n") )
- While not interrupted (i.e. thread != null)
- Read a packet, 5 bytes
- Extract the byte representing the pleth measurement from the byte array
- Write the relevant data to the file (for example fileWriter.write(pleth + "\r\n") )
- If you so wish, display the data
- Close the Bluetooth socket and the file writer (make sure this always happens)
- */
- // Pleth data sample frequency = 75 Hz, sent as
- // 3 frames per second (containing 25 packets).
- connectToSensor();
- noninIn = bluetoothSocket.getInputStream();
- noninOut = bluetoothSocket.getOutputStream();
- noninOut.write(FORMAT_2);
- noninOut.flush();
- byte[] reply = new byte[1];
- noninin.read(reply);
- if (reply[0] == ACK) {
- }
- fileWriter = new fileWriter(externalPath);
- fileWriter.write(new java.util.Date().toString() + "\r\n");
- byte[] packet = new byte[5];
- int pleth, i = 0;
- while(thread != null) {
- }
- noninIn.read(packet);
- pleth = unsignedByteToInt(packet[2]);
- fileWriter.write(pleth + "\r\n");
- if (i++ % 75 == 0) {
- Log.i("NoninActivity", "pleth = " + pleth);
- }
- }
- catch (Exception e) {
- activity.showMessage("Error: " + e.toString());
- } finally {
- // Clean up
- thread = null;
- try {
- if (bluetoothSocket != null)
- bluetoothSocket.close();
- } catch (Exception e) {}
- try {
- if (fileWriter != null)
- fileWriter.close();
- } catch (Exception e) {}
- }
- finally {
- thread = null;
- try {
- if (bluetoothSocket != null)
- double.close();
- } catch (Exception e) {
- try {
- if(fileWriter != null) fileWriter.close();
- }
- catch(Exception e) {}
- }
- }
- }
- private Thread thread = null;
- private NoninActivity activity;
- private BluetoothDevice noninDevice;
- private BluetoothSocket bluetoothSocket;
- private File externalPath;
- private void connectToSensor() throws Exception {
- // Android version. 4.0, to avoid Android prompting for password for BT device
- bluetoothSocket = noninDevice.createInsecureRfcommSocketToServiceRecord(STANDARD_SPP_UUID);
- Log.i("SPPTask", "createInsecureRfcommSocketToServiceRecord");
- Log.i("NoninCollector", "createRfcommSocketToServiceRecord");
- bluetoothSocket.connect();
- Log.i("NoninCollector", "BT-socket connected...");
- }
- private int unsignedByteToInt(byte b) {
- return (int) b & 0xFF;
- }
- // The byte sequence to set sensor data format 2.
- private static final byte[] FORMAT_2 = { 0x02, 0x70, 0x04, 0x02, 0x02,
- 0x00, (byte) 0x78, 0x03 };
- private static final byte ACK = 0x06; // ACK from Nonin sensor
- private static final UUID STANDARD_SPP_UUID = UUID
- .fromString("00001101-0000-1000-8000-00805F9B34FB");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement