fork download
  1. package mashiro.fan;
  2.  
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.io.OutputStream;
  6. import java.lang.reflect.Method;
  7. import java.util.UUID;
  8.  
  9.  
  10.  
  11. import android.app.Activity;
  12. import android.bluetooth.BluetoothAdapter;
  13. import android.bluetooth.BluetoothDevice;
  14. import android.bluetooth.BluetoothSocket;
  15. import android.content.Intent;
  16. import android.os.Build;
  17. import android.os.Bundle;
  18. import android.os.Handler;
  19. import android.util.Log;
  20. import android.view.View;
  21. import android.view.View.OnClickListener;
  22. import android.widget.Button;
  23. import android.widget.TextView;
  24. import android.widget.Toast;
  25.  
  26. public class MainActivity extends Activity {
  27. private static final String TAG = "fan";
  28.  
  29. Button connect, button, button2, button3, button4, button5, button6, button7, button8, button9;
  30.  
  31. TextView textView;
  32.  
  33. TextView textView2;
  34. TextView textView3;
  35.  
  36.  
  37.  
  38. Handler h;
  39.  
  40. final int RECIEVE_MESSAGE = 1; // Status for Handler
  41. private BluetoothAdapter btAdapter = null;
  42. private BluetoothSocket btSocket = null;
  43. private StringBuilder sb = new StringBuilder();
  44.  
  45. private ConnectedThread mConnectedThread;
  46.  
  47. // SPP UUID service
  48. private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
  49.  
  50. // MAC-address of Bluetooth module (you must edit this line)
  51. private static String address = "98:D3:32:30:47:11";
  52.  
  53. /** Called when the activity is first created. */
  54. @Override
  55. public void onCreate(Bundle savedInstanceState) {
  56. super.onCreate(savedInstanceState);
  57.  
  58. setContentView(R.layout.activity_main);
  59.  
  60. connect = (Button) findViewById(R.id.connect);
  61. button = (Button) findViewById(R.id.button);
  62. button2 = (Button) findViewById(R.id.button2);
  63. button3 = (Button) findViewById(R.id.button3);
  64. button4 = (Button) findViewById(R.id.button4);
  65. button5 = (Button) findViewById(R.id.button5);
  66. button6 = (Button) findViewById(R.id.button6);
  67. button7 = (Button) findViewById(R.id.button7);
  68. button8 = (Button) findViewById(R.id.button8);
  69. button9 = (Button) findViewById(R.id.button9); // for display the received data from the Arduino
  70.  
  71.  
  72. h = new Handler() {
  73. public void handleMessage(android.os.Message msg) {
  74. switch (msg.what) {
  75. case RECIEVE_MESSAGE: // if receive massage
  76. byte[] readBuf = (byte[]) msg.obj;
  77. String strIncom = new String(readBuf, 0, msg.arg1); // create string from bytes array
  78. sb.append(strIncom); // append string
  79. int endOfLineIndex = sb.indexOf("\r\n"); // determine the end-of-line
  80. if (endOfLineIndex > 0) { // if end-of-line,
  81. // extract string
  82. sb.delete(0, sb.length()); // and clear
  83. TextView text = (TextView) findViewById(R.id.textView);
  84. text.setText("現在溫度: " + sb.toString()); // update TextView
  85.  
  86. button.setEnabled(true);
  87. button2.setEnabled(true);
  88. button3.setEnabled(true);
  89. button4.setEnabled(true);
  90. button5.setEnabled(true);
  91. button6.setEnabled(true);
  92. button7.setEnabled(true);
  93. button8.setEnabled(true);
  94. }
  95.  
  96.  
  97. Log.d(TAG, "...String:"+ sb.toString() + "Byte:" + msg.arg1 + "...");
  98. break;
  99. }
  100. };
  101. };
  102.  
  103. btAdapter = BluetoothAdapter.getDefaultAdapter(); // get Bluetooth adapter
  104. checkBTState();
  105.  
  106.  
  107.  
  108.  
  109. }
  110.  
  111. private BluetoothSocket createBluetoothSocket(BluetoothDevice device) throws IOException {
  112. if(Build.VERSION.SDK_INT >= 10){
  113. try {
  114. final Method m = device.getClass().getMethod("createInsecureRfcommSocketToServiceRecord", new Class[] { UUID.class });
  115. return (BluetoothSocket) m.invoke(device, MY_UUID);
  116. } catch (Exception e) {
  117. Log.e(TAG, "Could not create Insecure RFComm Connection",e);
  118. }
  119. }
  120. return device.createRfcommSocketToServiceRecord(MY_UUID);
  121. }
  122.  
  123. @Override
  124. public void onResume() {
  125. super.onResume();
  126.  
  127. Log.d(TAG, "...onResume - try connect...");
  128.  
  129. // Set up a pointer to the remote node using it's address.
  130. BluetoothDevice device = btAdapter.getRemoteDevice(address);
  131.  
  132. // Two things are needed to make a connection:
  133. // A MAC address, which we got above.
  134. // A Service ID or UUID. In this case we are using the
  135. // UUID for SPP.
  136.  
  137. try {
  138. btSocket = createBluetoothSocket(device);
  139. } catch (IOException e) {
  140. errorExit("Fatal Error", "In onResume() and socket create failed: " + e.getMessage() + ".");
  141. }
  142.  
  143. // Discovery is resource intensive. Make sure it isn't going on
  144. // when you attempt to connect and pass your message.
  145. btAdapter.cancelDiscovery();
  146.  
  147. // Establish the connection. This will block until it connects.
  148. Log.d(TAG, "...Connecting...");
  149. try {
  150. btSocket.connect();
  151. Log.d(TAG, "....Connection ok...");
  152. } catch (IOException e) {
  153. try {
  154. btSocket.close();
  155. } catch (IOException e2) {
  156. errorExit("Fatal Error", "In onResume() and unable to close socket during connection failure" + e2.getMessage() + ".");
  157. }
  158. }
  159.  
  160. // Create a data stream so we can talk to server.
  161. Log.d(TAG, "...Create Socket...");
  162.  
  163. mConnectedThread = new ConnectedThread(btSocket);
  164. mConnectedThread.start();
  165. }
  166.  
  167. @Override
  168. public void onPause() {
  169. super.onPause();
  170.  
  171. Log.d(TAG, "...In onPause()...");
  172.  
  173. try {
  174. btSocket.close();
  175. } catch (IOException e2) {
  176. errorExit("Fatal Error", "In onPause() and failed to close socket." + e2.getMessage() + ".");
  177. }
  178. }
  179.  
  180. private void checkBTState() {
  181. // Check for Bluetooth support and then check to make sure it is turned on
  182. // Emulator doesn't support Bluetooth and will return null
  183. if(btAdapter==null) {
  184. errorExit("Fatal Error", "Bluetooth not support");
  185. } else {
  186. if (btAdapter.isEnabled()) {
  187. Log.d(TAG, "...Bluetooth ON...");
  188. } else {
  189. //Prompt user to turn on Bluetooth
  190. Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
  191. startActivityForResult(enableBtIntent, 1);
  192. }
  193. }
  194. }
  195.  
  196. private void errorExit(String title, String message){
  197. Toast.makeText(getBaseContext(), title + " - " + message, Toast.LENGTH_LONG).show();
  198. finish();
  199. }
  200.  
  201. private class ConnectedThread extends Thread {
  202. private final InputStream mmInStream;
  203. private final OutputStream mmOutStream;
  204.  
  205. public ConnectedThread(BluetoothSocket socket) {
  206. InputStream tmpIn = null;
  207. OutputStream tmpOut = null;
  208.  
  209. // Get the input and output streams, using temp objects because
  210. // member streams are final
  211. try {
  212. tmpIn = socket.getInputStream();
  213. tmpOut = socket.getOutputStream();
  214. } catch (IOException e) { }
  215.  
  216. mmInStream = tmpIn;
  217. mmOutStream = tmpOut;
  218. }
  219.  
  220. public void run() {
  221. byte[] buffer = new byte[256]; // buffer store for the stream
  222. int bytes; // bytes returned from read()
  223.  
  224. // Keep listening to the InputStream until an exception occurs
  225. while (true) {
  226. try {
  227. // Read from the InputStream
  228. bytes = mmInStream.read(buffer); // Get number of bytes and message in "buffer"
  229. h.obtainMessage(RECIEVE_MESSAGE, bytes, -1, buffer).sendToTarget(); // Send to message queue Handler
  230. } catch (IOException e) {
  231. break;
  232. }
  233. }
  234. }
  235.  
  236. /* Call this from the main activity to send data to the remote device */
  237. public void write(String message) {
  238. Log.d(TAG, "...Data to send: " + message + "...");
  239. byte[] msgBuffer = message.getBytes();
  240. try {
  241. mmOutStream.write(msgBuffer);
  242. } catch (IOException e) {
  243. Log.d(TAG, "...Error data send: " + e.getMessage() + "...");
  244. }
  245. }
  246. }
  247. }
  248. }
  249. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:248: error: class, interface, or enum expected
    }
    ^
1 error
stdout
Standard output is empty