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. Handler h;
  38.  
  39. final int RECIEVE_MESSAGE = 1; // Status for Handler
  40. private BluetoothAdapter btAdapter = null;
  41. private BluetoothSocket btSocket = null;
  42. private StringBuilder sb = new StringBuilder();
  43.  
  44. private ConnectedThread mConnectedThread;
  45.  
  46. // SPP UUID service
  47. private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
  48.  
  49. // MAC-address of Bluetooth module (you must edit this line)
  50. private static String address = "98:D3:32:30:47:11";
  51.  
  52. /** Called when the activity is first created. */
  53. @Override
  54. public void onCreate(Bundle savedInstanceState) {
  55. super.onCreate(savedInstanceState);
  56.  
  57. setContentView(R.layout.activity_main);
  58.  
  59. connect = (Button) findViewById(R.id.connect);
  60. button = (Button) findViewById(R.id.button);
  61. button2 = (Button) findViewById(R.id.button2);
  62. button3 = (Button) findViewById(R.id.button3);
  63. button4 = (Button) findViewById(R.id.button4);
  64. button5 = (Button) findViewById(R.id.button5);
  65. button6 = (Button) findViewById(R.id.button6);
  66. button7 = (Button) findViewById(R.id.button7);
  67. button8 = (Button) findViewById(R.id.button8);
  68. button9 = (Button) findViewById(R.id.button9); // for display the received data from the Arduino
  69.  
  70. h = new Handler() {
  71. public void handleMessage(android.os.Message msg) {
  72. switch (msg.what) {
  73. case RECIEVE_MESSAGE: // if receive massage
  74. byte[] readBuf = (byte[]) msg.obj;
  75. String strIncom = new String(readBuf, 0, msg.arg1); // create string from bytes array
  76. sb.append(strIncom); // append string
  77. int endOfLineIndex = sb.indexOf("\r\n"); // determine the end-of-line
  78. if (endOfLineIndex > 0) { // if end-of-line,
  79. String sbprint = sb.substring(0, endOfLineIndex); // extract string
  80. sb.delete(0, sb.length()); // and clear
  81. textView.setText("現在時間: " + sbprint); // update TextView
  82. button.setEnabled(true);
  83. button2.setEnabled(true);
  84. button3.setEnabled(true);
  85. button4.setEnabled(true);
  86. button5.setEnabled(true);
  87. button6.setEnabled(true);
  88. button7.setEnabled(true);
  89. button8.setEnabled(true);
  90. }
  91. //Log.d(TAG, "...String:"+ sb.toString() + "Byte:" + msg.arg1 + "...");
  92. break;
  93. }
  94. };
  95. };
  96.  
  97. btAdapter = BluetoothAdapter.getDefaultAdapter(); // get Bluetooth adapter
  98. checkBTState();
  99.  
  100. button2.setOnClickListener(new OnClickListener() {
  101. public void onClick(View v) {
  102. mConnectedThread.write("g");
  103. TextView text = (TextView) findViewById(R.id.textView3);
  104. text.setText("現在模式:自動模式");
  105. button.setEnabled(true);
  106. button2.setEnabled(false);
  107. button3.setEnabled(false);
  108. button4.setEnabled(false);
  109. button5.setEnabled(false);
  110. button6.setEnabled(false);
  111. button7.setEnabled(false);
  112. button8.setEnabled(false);
  113. }
  114. });
  115. button3.setOnClickListener(new OnClickListener() {
  116. public void onClick(View v) {
  117. mConnectedThread.write("c");
  118.  
  119. TextView text = (TextView) findViewById(R.id.textView2);
  120. text.setText("現在風速:強");
  121. button.setEnabled(false);
  122. button2.setEnabled(true);
  123. button3.setEnabled(false);
  124. button4.setEnabled(true);
  125. button5.setEnabled(true);
  126. button6.setEnabled(true);
  127. button7.setEnabled(true);
  128. button8.setEnabled(true);
  129. }
  130. });
  131. button4.setOnClickListener(new OnClickListener() {
  132. public void onClick(View v) {
  133. mConnectedThread.write("b");
  134.  
  135. TextView text = (TextView) findViewById(R.id.textView2);
  136. text.setText("現在風速:中");
  137. button.setEnabled(false);
  138. button2.setEnabled(true);
  139. button3.setEnabled(true);
  140. button4.setEnabled(false);
  141. button5.setEnabled(true);
  142. button6.setEnabled(true);
  143. button7.setEnabled(true);
  144. button8.setEnabled(true);
  145. }
  146. });
  147. button5.setOnClickListener(new OnClickListener() {
  148. public void onClick(View v) {
  149. mConnectedThread.write("a");
  150.  
  151. TextView text = (TextView) findViewById(R.id.textView2);
  152. text.setText("現在風速:弱");
  153. button.setEnabled(false);
  154. button2.setEnabled(true);
  155. button3.setEnabled(true);
  156. button4.setEnabled(true);
  157. button5.setEnabled(false);
  158. button6.setEnabled(true);
  159. button7.setEnabled(true);
  160. button8.setEnabled(true);
  161. }
  162. });
  163. button6.setOnClickListener(new OnClickListener() {
  164. public void onClick(View v) {
  165. mConnectedThread.write("d");
  166. button.setEnabled(false);
  167. button2.setEnabled(true);
  168. button3.setEnabled(true);
  169. button4.setEnabled(true);
  170. button5.setEnabled(true);
  171. button6.setEnabled(false);
  172. button7.setEnabled(true);
  173. button8.setEnabled(true);
  174. }
  175. });
  176. button7.setOnClickListener(new OnClickListener() {
  177. public void onClick(View v) {
  178. mConnectedThread.write("e");
  179. button.setEnabled(false);
  180. button2.setEnabled(true);
  181. button3.setEnabled(true);
  182. button4.setEnabled(true);
  183. button5.setEnabled(true);
  184. button6.setEnabled(true);
  185. button7.setEnabled(false);
  186. button8.setEnabled(true);
  187. }
  188. });
  189. button8.setOnClickListener(new OnClickListener() {
  190. public void onClick(View v) {
  191. mConnectedThread.write("f");
  192. button.setEnabled(false);
  193. button2.setEnabled(true);
  194. button3.setEnabled(true);
  195. button4.setEnabled(true);
  196. button5.setEnabled(true);
  197. button6.setEnabled(true);
  198. button7.setEnabled(true);
  199. button8.setEnabled(false);
  200. }
  201. });
  202. button.setOnClickListener(new View.OnClickListener() {
  203. @Override
  204. public void onClick(View v) {
  205. TextView text = (TextView) findViewById(R.id.textView3);
  206. text.setText("現在模式:手動模式");
  207. button.setEnabled(false);
  208. button2.setEnabled(true);
  209. button3.setEnabled(true);
  210. button4.setEnabled(true);
  211. button5.setEnabled(true);
  212. button6.setEnabled(true);
  213. button7.setEnabled(true);
  214. button8.setEnabled(true);
  215.  
  216. }
  217. });
  218.  
  219. }
  220.  
  221. private BluetoothSocket createBluetoothSocket(BluetoothDevice device) throws IOException {
  222. if(Build.VERSION.SDK_INT >= 10){
  223. try {
  224. final Method m = device.getClass().getMethod("createInsecureRfcommSocketToServiceRecord", new Class[] { UUID.class });
  225. return (BluetoothSocket) m.invoke(device, MY_UUID);
  226. } catch (Exception e) {
  227. Log.e(TAG, "Could not create Insecure RFComm Connection",e);
  228. }
  229. }
  230. return device.createRfcommSocketToServiceRecord(MY_UUID);
  231. }
  232.  
  233. @Override
  234. public void onResume() {
  235. super.onResume();
  236.  
  237. Log.d(TAG, "...onResume - try connect...");
  238.  
  239. // Set up a pointer to the remote node using it's address.
  240. BluetoothDevice device = btAdapter.getRemoteDevice(address);
  241.  
  242. // Two things are needed to make a connection:
  243. // A MAC address, which we got above.
  244. // A Service ID or UUID. In this case we are using the
  245. // UUID for SPP.
  246.  
  247. try {
  248. btSocket = createBluetoothSocket(device);
  249. } catch (IOException e) {
  250. errorExit("Fatal Error", "In onResume() and socket create failed: " + e.getMessage() + ".");
  251. }
  252.  
  253. // Discovery is resource intensive. Make sure it isn't going on
  254. // when you attempt to connect and pass your message.
  255. btAdapter.cancelDiscovery();
  256.  
  257. // Establish the connection. This will block until it connects.
  258. Log.d(TAG, "...Connecting...");
  259. try {
  260. btSocket.connect();
  261. Log.d(TAG, "....Connection ok...");
  262. } catch (IOException e) {
  263. try {
  264. btSocket.close();
  265. } catch (IOException e2) {
  266. errorExit("Fatal Error", "In onResume() and unable to close socket during connection failure" + e2.getMessage() + ".");
  267. }
  268. }
  269.  
  270. // Create a data stream so we can talk to server.
  271. Log.d(TAG, "...Create Socket...");
  272.  
  273. mConnectedThread = new ConnectedThread(btSocket);
  274. mConnectedThread.start();
  275. }
  276.  
  277. @Override
  278. public void onPause() {
  279. super.onPause();
  280.  
  281. Log.d(TAG, "...In onPause()...");
  282.  
  283. try {
  284. btSocket.close();
  285. } catch (IOException e2) {
  286. errorExit("Fatal Error", "In onPause() and failed to close socket." + e2.getMessage() + ".");
  287. }
  288. }
  289.  
  290. private void checkBTState() {
  291. // Check for Bluetooth support and then check to make sure it is turned on
  292. // Emulator doesn't support Bluetooth and will return null
  293. if(btAdapter==null) {
  294. errorExit("Fatal Error", "Bluetooth not support");
  295. } else {
  296. if (btAdapter.isEnabled()) {
  297. Log.d(TAG, "...Bluetooth ON...");
  298. } else {
  299. //Prompt user to turn on Bluetooth
  300. Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
  301. startActivityForResult(enableBtIntent, 1);
  302. }
  303. }
  304. }
  305.  
  306. private void errorExit(String title, String message){
  307. Toast.makeText(getBaseContext(), title + " - " + message, Toast.LENGTH_LONG).show();
  308. finish();
  309. }
  310.  
  311. private class ConnectedThread extends Thread {
  312. private final InputStream mmInStream;
  313. private final OutputStream mmOutStream;
  314.  
  315. public ConnectedThread(BluetoothSocket socket) {
  316. InputStream tmpIn = null;
  317. OutputStream tmpOut = null;
  318.  
  319. // Get the input and output streams, using temp objects because
  320. // member streams are final
  321. try {
  322. tmpIn = socket.getInputStream();
  323. tmpOut = socket.getOutputStream();
  324. } catch (IOException e) { }
  325.  
  326. mmInStream = tmpIn;
  327. mmOutStream = tmpOut;
  328. }
  329.  
  330. public void run() {
  331. byte[] buffer = new byte[256]; // buffer store for the stream
  332. int bytes; // bytes returned from read()
  333.  
  334. // Keep listening to the InputStream until an exception occurs
  335. while (true) {
  336. try {
  337. // Read from the InputStream
  338. bytes = mmInStream.read(buffer); // Get number of bytes and message in "buffer"
  339. h.obtainMessage(RECIEVE_MESSAGE, bytes, -1, buffer).sendToTarget(); // Send to message queue Handler
  340. } catch (IOException e) {
  341. break;
  342. }
  343. }
  344. }
  345.  
  346. /* Call this from the main activity to send data to the remote device */
  347. public void write(String message) {
  348. Log.d(TAG, "...Data to send: " + message + "...");
  349. byte[] msgBuffer = message.getBytes();
  350. try {
  351. mmOutStream.write(msgBuffer);
  352. } catch (IOException e) {
  353. Log.d(TAG, "...Error data send: " + e.getMessage() + "...");
  354. }
  355. }
  356. }
  357. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:26: error: class MainActivity is public, should be declared in a file named MainActivity.java
public class MainActivity extends Activity {
       ^
Main.java:11: error: package android.app does not exist
import android.app.Activity;
                  ^
Main.java:12: error: package android.bluetooth does not exist
import android.bluetooth.BluetoothAdapter;
                        ^
Main.java:13: error: package android.bluetooth does not exist
import android.bluetooth.BluetoothDevice;
                        ^
Main.java:14: error: package android.bluetooth does not exist
import android.bluetooth.BluetoothSocket;
                        ^
Main.java:15: error: package android.content does not exist
import android.content.Intent;
                      ^
Main.java:16: error: package android.os does not exist
import android.os.Build;
                 ^
Main.java:17: error: package android.os does not exist
import android.os.Bundle;
                 ^
Main.java:18: error: package android.os does not exist
import android.os.Handler;
                 ^
Main.java:19: error: package android.util does not exist
import android.util.Log;
                   ^
Main.java:20: error: package android.view does not exist
import android.view.View;
                   ^
Main.java:21: error: package android.view.View does not exist
import android.view.View.OnClickListener;
                        ^
Main.java:22: error: package android.widget does not exist
import android.widget.Button;
                     ^
Main.java:23: error: package android.widget does not exist
import android.widget.TextView;
                     ^
Main.java:24: error: package android.widget does not exist
import android.widget.Toast;
                     ^
Main.java:26: error: cannot find symbol
public class MainActivity extends Activity {
                                  ^
  symbol: class Activity
Main.java:29: error: cannot find symbol
    Button connect, button, button2, button3, button4, button5, button6, button7, button8, button9;
    ^
  symbol:   class Button
  location: class MainActivity
Main.java:31: error: cannot find symbol
    TextView textView;
    ^
  symbol:   class TextView
  location: class MainActivity
Main.java:33: error: cannot find symbol
    TextView textView2;
    ^
  symbol:   class TextView
  location: class MainActivity
Main.java:34: error: cannot find symbol
    TextView textView3;
    ^
  symbol:   class TextView
  location: class MainActivity
Main.java:37: error: cannot find symbol
    Handler h;
    ^
  symbol:   class Handler
  location: class MainActivity
Main.java:40: error: cannot find symbol
    private BluetoothAdapter btAdapter = null;
            ^
  symbol:   class BluetoothAdapter
  location: class MainActivity
Main.java:41: error: cannot find symbol
    private BluetoothSocket btSocket = null;
            ^
  symbol:   class BluetoothSocket
  location: class MainActivity
Main.java:54: error: cannot find symbol
    public void onCreate(Bundle savedInstanceState) {
                         ^
  symbol:   class Bundle
  location: class MainActivity
Main.java:221: error: cannot find symbol
    private BluetoothSocket createBluetoothSocket(BluetoothDevice device) throws IOException {
                                                  ^
  symbol:   class BluetoothDevice
  location: class MainActivity
Main.java:221: error: cannot find symbol
    private BluetoothSocket createBluetoothSocket(BluetoothDevice device) throws IOException {
            ^
  symbol:   class BluetoothSocket
  location: class MainActivity
Main.java:315: error: cannot find symbol
        public ConnectedThread(BluetoothSocket socket) {
                               ^
  symbol:   class BluetoothSocket
  location: class MainActivity.ConnectedThread
Main.java:53: error: method does not override or implement a method from a supertype
    @Override
    ^
Main.java:55: error: cannot find symbol
        super.onCreate(savedInstanceState);
        ^
  symbol:   variable super
  location: class MainActivity
Main.java:57: error: package R does not exist
        setContentView(R.layout.activity_main);
                        ^
Main.java:59: error: cannot find symbol
        connect = (Button) findViewById(R.id.connect);
                   ^
  symbol:   class Button
  location: class MainActivity
Main.java:59: error: package R does not exist
        connect = (Button) findViewById(R.id.connect);
                                         ^
Main.java:60: error: cannot find symbol
        button = (Button) findViewById(R.id.button);
                  ^
  symbol:   class Button
  location: class MainActivity
Main.java:60: error: package R does not exist
        button = (Button) findViewById(R.id.button);
                                        ^
Main.java:61: error: cannot find symbol
        button2 = (Button) findViewById(R.id.button2);
                   ^
  symbol:   class Button
  location: class MainActivity
Main.java:61: error: package R does not exist
        button2 = (Button) findViewById(R.id.button2);
                                         ^
Main.java:62: error: cannot find symbol
        button3 = (Button) findViewById(R.id.button3);
                   ^
  symbol:   class Button
  location: class MainActivity
Main.java:62: error: package R does not exist
        button3 = (Button) findViewById(R.id.button3);
                                         ^
Main.java:63: error: cannot find symbol
        button4 = (Button) findViewById(R.id.button4);
                   ^
  symbol:   class Button
  location: class MainActivity
Main.java:63: error: package R does not exist
        button4 = (Button) findViewById(R.id.button4);
                                         ^
Main.java:64: error: cannot find symbol
        button5 = (Button) findViewById(R.id.button5);
                   ^
  symbol:   class Button
  location: class MainActivity
Main.java:64: error: package R does not exist
        button5 = (Button) findViewById(R.id.button5);
                                         ^
Main.java:65: error: cannot find symbol
        button6 = (Button) findViewById(R.id.button6);
                   ^
  symbol:   class Button
  location: class MainActivity
Main.java:65: error: package R does not exist
        button6 = (Button) findViewById(R.id.button6);
                                         ^
Main.java:66: error: cannot find symbol
        button7 = (Button) findViewById(R.id.button7);
                   ^
  symbol:   class Button
  location: class MainActivity
Main.java:66: error: package R does not exist
        button7 = (Button) findViewById(R.id.button7);
                                         ^
Main.java:67: error: cannot find symbol
        button8 = (Button) findViewById(R.id.button8);
                   ^
  symbol:   class Button
  location: class MainActivity
Main.java:67: error: package R does not exist
        button8 = (Button) findViewById(R.id.button8);
                                         ^
Main.java:68: error: cannot find symbol
        button9 = (Button) findViewById(R.id.button9);      // for display the received data from the Arduino
                   ^
  symbol:   class Button
  location: class MainActivity
Main.java:68: error: package R does not exist
        button9 = (Button) findViewById(R.id.button9);      // for display the received data from the Arduino
                                         ^
Main.java:70: error: cannot find symbol
        h = new Handler() {
                ^
  symbol:   class Handler
  location: class MainActivity
Main.java:97: error: cannot find symbol
        btAdapter = BluetoothAdapter.getDefaultAdapter();       // get Bluetooth adapter
                    ^
  symbol:   variable BluetoothAdapter
  location: class MainActivity
Main.java:100: error: cannot find symbol
        button2.setOnClickListener(new OnClickListener() {
                                       ^
  symbol:   class OnClickListener
  location: class MainActivity
Main.java:115: error: cannot find symbol
        button3.setOnClickListener(new OnClickListener() {
                                       ^
  symbol:   class OnClickListener
  location: class MainActivity
Main.java:131: error: cannot find symbol
        button4.setOnClickListener(new OnClickListener() {
                                       ^
  symbol:   class OnClickListener
  location: class MainActivity
Main.java:147: error: cannot find symbol
        button5.setOnClickListener(new OnClickListener() {
                                       ^
  symbol:   class OnClickListener
  location: class MainActivity
Main.java:163: error: cannot find symbol
        button6.setOnClickListener(new OnClickListener() {
                                       ^
  symbol:   class OnClickListener
  location: class MainActivity
Main.java:176: error: cannot find symbol
        button7.setOnClickListener(new OnClickListener() {
                                       ^
  symbol:   class OnClickListener
  location: class MainActivity
Main.java:189: error: cannot find symbol
        button8.setOnClickListener(new OnClickListener() {
                                       ^
  symbol:   class OnClickListener
  location: class MainActivity
Main.java:202: error: package View does not exist
        button.setOnClickListener(new View.OnClickListener() {
                                          ^
Main.java:222: error: package Build does not exist
        if(Build.VERSION.SDK_INT >= 10){
                ^
Main.java:225: error: cannot find symbol
                return (BluetoothSocket) m.invoke(device, MY_UUID);
                        ^
  symbol:   class BluetoothSocket
  location: class MainActivity
Main.java:227: error: cannot find symbol
                Log.e(TAG, "Could not create Insecure RFComm Connection",e);
                ^
  symbol:   variable Log
  location: class MainActivity
Main.java:233: error: method does not override or implement a method from a supertype
    @Override
    ^
Main.java:235: error: cannot find symbol
        super.onResume();
        ^
  symbol:   variable super
  location: class MainActivity
Main.java:237: error: cannot find symbol
        Log.d(TAG, "...onResume - try connect...");
        ^
  symbol:   variable Log
  location: class MainActivity
Main.java:240: error: cannot find symbol
        BluetoothDevice device = btAdapter.getRemoteDevice(address);
        ^
  symbol:   class BluetoothDevice
  location: class MainActivity
Main.java:258: error: cannot find symbol
        Log.d(TAG, "...Connecting...");
        ^
  symbol:   variable Log
  location: class MainActivity
Main.java:261: error: cannot find symbol
            Log.d(TAG, "....Connection ok...");
            ^
  symbol:   variable Log
  location: class MainActivity
Main.java:271: error: cannot find symbol
        Log.d(TAG, "...Create Socket...");
        ^
  symbol:   variable Log
  location: class MainActivity
Main.java:277: error: method does not override or implement a method from a supertype
    @Override
    ^
Main.java:279: error: cannot find symbol
        super.onPause();
        ^
  symbol:   variable super
  location: class MainActivity
Main.java:281: error: cannot find symbol
        Log.d(TAG, "...In onPause()...");
        ^
  symbol:   variable Log
  location: class MainActivity
Main.java:297: error: cannot find symbol
                Log.d(TAG, "...Bluetooth ON...");
                ^
  symbol:   variable Log
  location: class MainActivity
Main.java:300: error: cannot find symbol
                Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                ^
  symbol:   class Intent
  location: class MainActivity
Main.java:300: error: cannot find symbol
                Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                                            ^
  symbol:   class Intent
  location: class MainActivity
Main.java:300: error: cannot find symbol
                Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                                                   ^
  symbol:   variable BluetoothAdapter
  location: class MainActivity
Main.java:307: error: cannot find symbol
        Toast.makeText(getBaseContext(), title + " - " + message, Toast.LENGTH_LONG).show();
                       ^
  symbol:   method getBaseContext()
  location: class MainActivity
Main.java:307: error: cannot find symbol
        Toast.makeText(getBaseContext(), title + " - " + message, Toast.LENGTH_LONG).show();
                                                                  ^
  symbol:   variable Toast
  location: class MainActivity
Main.java:307: error: cannot find symbol
        Toast.makeText(getBaseContext(), title + " - " + message, Toast.LENGTH_LONG).show();
        ^
  symbol:   variable Toast
  location: class MainActivity
Main.java:308: error: cannot find symbol
        finish();
        ^
  symbol:   method finish()
  location: class MainActivity
Main.java:348: error: cannot find symbol
            Log.d(TAG, "...Data to send: " + message + "...");
            ^
  symbol:   variable Log
  location: class MainActivity.ConnectedThread
Main.java:353: error: cannot find symbol
                Log.d(TAG, "...Error data send: " + e.getMessage() + "...");
                ^
  symbol:   variable Log
  location: class MainActivity.ConnectedThread
83 errors
stdout
Standard output is empty