-
Notifications
You must be signed in to change notification settings - Fork 0
/
TransferData.java
82 lines (66 loc) · 2.43 KB
/
TransferData.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package com.pcr_23.pcr_thermal_cycler;
import android.app.ActionBar;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;
import java.io.IOException;
import android.widget.EditText;
import android.content.Intent;
import java.util.HashMap;
import android.support.v7.app.ActionBarActivity;
/**
* Created by briansia1 on 1/19/16.
*/
public class TransferData extends ActionBarActivity {
protected void onCreate(Bundle savedInstanceState){
Intent intent = getIntent();
HashMap<String, Integer> input_map = (HashMap<String, Integer>) intent.getSerializableExtra("input_map");
super.onCreate(savedInstanceState);
setContentView(R.layout.data_transfer);
BTinit(input_map);
}
protected void BTinit(HashMap<String, Integer>input_map){
Bluetooth BT = new Bluetooth(getApplicationContext(), this);
try{
BT.findBT();
BT.openBT();
}
catch(IOException ex){
Toast toast = Toast.makeText(getApplicationContext(), "Bluetooth Initialization Error.", Toast.LENGTH_LONG);
toast.show();
}
sendInputData(input_map, BT);
BT.beginListenForData();
}
public void sendInputData(HashMap<String, Integer> input_map, Bluetooth BT) {
//loop through hash map and send both key and value
for (HashMap.Entry<String, Integer> entry : input_map.entrySet()) {
String key = entry.getKey();
int value = entry.getValue();
byte[] key_byte = key.getBytes();
//send the key bytes
for (int index = 0; index < key_byte.length; index++) {
try {
BT.sendData(key_byte[index]);
//sendByte(value, BT);
} catch (IOException ex) {
Toast toast = Toast.makeText(getApplicationContext(), "Data send error.", Toast.LENGTH_LONG);
toast.show();
}
}
}
}
public void sendByte(int value, Bluetooth BT){
byte[] result = new byte[4];
result[0] = (byte) (value >> 24);
result[1] = (byte) (value >> 16);
result[2] = (byte) (value >> 8);
result[3] = (byte) (value /*>> 0*/);
for (int i = 0; i<4; i++){
try{BT.sendData(result[i]);}
catch(IOException ex){}
}
}
public void receiveData(byte[] encodedByte){
}
}