fork download
  1. package com.example.bluetoothserver;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.io.InputStreamReader;
  7. import java.io.OutputStream;
  8. import java.util.UUID;
  9. import android.os.Bundle;
  10. import android.os.Handler;
  11. import android.os.Message;
  12. import android.app.Activity;
  13. import android.bluetooth.BluetoothAdapter;
  14. import android.bluetooth.BluetoothServerSocket;
  15. import android.bluetooth.BluetoothSocket;
  16. import android.content.Intent;
  17. import android.util.Log;
  18. import android.view.Menu;
  19. import android.widget.LinearLayout;
  20. import android.widget.TextView;
  21. import android.widget.Toast;
  22. import android.widget.LinearLayout.LayoutParams;
  23.  
  24. public class MainActivity extends Activity {
  25. static TextView textview1;
  26. private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
  27. private final int REQUEST_ENABLE_BT = 2;
  28. private BluetoothAdapter mBluetoothAdapter = null;
  29. private Thread AT;
  30. private final static int MESSAGE_READ = 1;
  31. private static Handler mHandler = new Handler(){
  32. public void handleMessage (Message message, String str){
  33. if(message.what == MESSAGE_READ){
  34. Log.e("handler",str);
  35. textview1.setText(textview1.getText()+str);
  36. }
  37.  
  38. }
  39. };
  40. @Override
  41. protected void onCreate(Bundle savedInstanceState) {
  42. super.onCreate(savedInstanceState);
  43. setContentView(R.layout.activity_main);
  44. textview1 = (TextView)findViewById(R.id.textView1);
  45. mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
  46. StartBluetooth();
  47.  
  48. AT = new AcceptThread();
  49. AT.start();
  50. }
  51.  
  52. @Override
  53. public boolean onCreateOptionsMenu(Menu menu) {
  54. // Inflate the menu; this adds items to the action bar if it is present.
  55. getMenuInflater().inflate(R.menu.main, menu);
  56. return true;
  57. }
  58.  
  59. //開啟藍芽
  60. public void StartBluetooth(){
  61.  
  62.  
  63. // 如果裝置不支援藍芽
  64. if (mBluetoothAdapter == null) {
  65. Toast.makeText(this,
  66. "Bluetooth is not available.",
  67. Toast.LENGTH_LONG).show();
  68. finish();
  69. return;
  70. }
  71. // 如果藍芽沒有開啟
  72. if (!mBluetoothAdapter.isEnabled()) {
  73. Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
  74. startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
  75. }
  76.  
  77. }
  78. //bluetooth server
  79. private class AcceptThread extends Thread {
  80. private final BluetoothServerSocket mmServerSocket;
  81.  
  82. public AcceptThread() {
  83. // Use a temporary object that is later assigned to mmServerSocket,
  84. // because mmServerSocket is final
  85. BluetoothServerSocket tmp = null;
  86. try {
  87. // MY_UUID is the app's UUID string, also used by the client code
  88. tmp = mBluetoothAdapter.listenUsingRfcommWithServiceRecord("BluetoothServer", MY_UUID);
  89. } catch (IOException e) {
  90. Log.e("listenUsingRfcommWithServiceRecord","error");
  91. }
  92. mmServerSocket = tmp;
  93. }
  94.  
  95. public void run() {
  96. BluetoothSocket socket = null;
  97. // Keep listening until exception occurs or a socket is returned
  98. while (true) {
  99. try {
  100. socket = mmServerSocket.accept();
  101. Log.e("listen","listen");
  102. } catch (IOException e) {
  103. Log.e("listen","listen over");
  104. break;
  105. }
  106. // If a connection was accepted
  107. if (socket != null) {
  108. // Do work to manage the connection (in a separate thread)
  109. Thread CT = new ConnectedThread(socket);
  110. CT.start();
  111. Log.e("Connected","Connected");
  112. //Log.e("socketclose","socketclose");
  113. //mmServerSocket.close();
  114. break;
  115. }
  116. }
  117. }
  118.  
  119. /** Will cancel the listening socket, and cause the thread to finish */
  120. public void cancel() {
  121. try {
  122. mmServerSocket.close();
  123. } catch (IOException e) { }
  124. }
  125. }
  126.  
  127. private class ConnectedThread extends Thread {
  128. private BluetoothSocket mmSocket;
  129. private InputStream mmInStream;
  130.  
  131. public ConnectedThread(BluetoothSocket socket) {
  132. mmSocket = socket;
  133. Log.e("a","a");
  134. // Get the input and output streams, using temp objects because
  135. // member streams are final
  136. try {
  137. mmInStream = socket.getInputStream();
  138. Log.e("get","get");
  139. } catch (IOException e) {
  140. Log.e("error","error");
  141. }
  142. }
  143. @Override
  144. public void run() {
  145. String line = "";
  146. String tmp = "";
  147. br = new BufferedReader(new InputStreamReader(mmInStream));
  148. Log.e("br",br.toString());
  149. // Keep listening to the InputStream until an exception occurs
  150. try {
  151. while ((line = br.readLine())!=null) {
  152. MainActivity.mHandler.obtainMessage(MESSAGE_READ, line).sendToTarget();
  153. Log.e("line",line);
  154. }
  155.  
  156.  
  157. } catch (IOException e) {
  158. // TODO Auto-generated catch block
  159. Log.e("BufferedReader excrption",tmp);
  160. e.printStackTrace();
  161. }
  162.  
  163. Log.e("txt",tmp);
  164. cancel();
  165. }
  166.  
  167. /* Call this from the main activity to shutdown the connection */
  168. public void cancel() {
  169. try {
  170. mmSocket.close();
  171. } catch (IOException e) { }
  172. }
  173. }
  174. }
  175.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:24: error: class MainActivity is public, should be declared in a file named MainActivity.java
public class MainActivity extends Activity {
       ^
Main.java:9: error: package android.os does not exist
import android.os.Bundle;
                 ^
Main.java:10: error: package android.os does not exist
import android.os.Handler;
                 ^
Main.java:11: error: package android.os does not exist
import android.os.Message;
                 ^
Main.java:12: error: package android.app does not exist
import android.app.Activity;
                  ^
Main.java:13: error: package android.bluetooth does not exist
import android.bluetooth.BluetoothAdapter;
                        ^
Main.java:14: error: package android.bluetooth does not exist
import android.bluetooth.BluetoothServerSocket;
                        ^
Main.java:15: error: package android.bluetooth does not exist
import android.bluetooth.BluetoothSocket;
                        ^
Main.java:16: error: package android.content does not exist
import android.content.Intent;
                      ^
Main.java:17: error: package android.util does not exist
import android.util.Log;
                   ^
Main.java:18: error: package android.view does not exist
import android.view.Menu;
                   ^
Main.java:19: error: package android.widget does not exist
import android.widget.LinearLayout;
                     ^
Main.java:20: error: package android.widget does not exist
import android.widget.TextView;
                     ^
Main.java:21: error: package android.widget does not exist
import android.widget.Toast;
                     ^
Main.java:22: error: package android.widget.LinearLayout does not exist
import android.widget.LinearLayout.LayoutParams;
                                  ^
Main.java:24: error: cannot find symbol
public class MainActivity extends Activity {
                                  ^
  symbol: class Activity
Main.java:25: error: cannot find symbol
	static TextView textview1;
	       ^
  symbol:   class TextView
  location: class MainActivity
Main.java:28: error: cannot find symbol
	private BluetoothAdapter mBluetoothAdapter = null;
	        ^
  symbol:   class BluetoothAdapter
  location: class MainActivity
Main.java:31: error: cannot find symbol
	private static Handler mHandler = new Handler(){
	               ^
  symbol:   class Handler
  location: class MainActivity
Main.java:41: error: cannot find symbol
	protected void onCreate(Bundle savedInstanceState) {
	                        ^
  symbol:   class Bundle
  location: class MainActivity
Main.java:53: error: cannot find symbol
	public boolean onCreateOptionsMenu(Menu menu) {
	                                   ^
  symbol:   class Menu
  location: class MainActivity
Main.java:80: error: cannot find symbol
        private final BluetoothServerSocket mmServerSocket;
                      ^
  symbol:   class BluetoothServerSocket
  location: class MainActivity.AcceptThread
Main.java:128: error: cannot find symbol
        private BluetoothSocket mmSocket;
                ^
  symbol:   class BluetoothSocket
  location: class MainActivity.ConnectedThread
Main.java:131: error: cannot find symbol
        public ConnectedThread(BluetoothSocket socket) {
                               ^
  symbol:   class BluetoothSocket
  location: class MainActivity.ConnectedThread
Main.java:31: error: cannot find symbol
	private static Handler mHandler = new Handler(){
	                                      ^
  symbol:   class Handler
  location: class MainActivity
Main.java:42: error: cannot find symbol
		super.onCreate(savedInstanceState);
		^
  symbol:   variable super
  location: class MainActivity
Main.java:43: error: package R does not exist
		setContentView(R.layout.activity_main);
		                ^
Main.java:44: error: cannot find symbol
		textview1 = (TextView)findViewById(R.id.textView1);
		             ^
  symbol:   class TextView
  location: class MainActivity
Main.java:44: error: package R does not exist
		textview1 = (TextView)findViewById(R.id.textView1);
		                                    ^
Main.java:45: error: cannot find symbol
		mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
		                    ^
  symbol:   variable BluetoothAdapter
  location: class MainActivity
Main.java:40: error: method does not override or implement a method from a supertype
	@Override
	^
Main.java:55: error: package R does not exist
		getMenuInflater().inflate(R.menu.main, menu);
		                           ^
Main.java:55: error: cannot find symbol
		getMenuInflater().inflate(R.menu.main, menu);
		^
  symbol:   method getMenuInflater()
  location: class MainActivity
Main.java:52: error: method does not override or implement a method from a supertype
	@Override
	^
Main.java:67: error: cannot find symbol
			Toast.LENGTH_LONG).show();
			^
  symbol:   variable Toast
  location: class MainActivity
Main.java:65: error: cannot find symbol
			Toast.makeText(this,
			^
  symbol:   variable Toast
  location: class MainActivity
Main.java:68: error: cannot find symbol
			finish();
			^
  symbol:   method finish()
  location: class MainActivity
Main.java:73: error: cannot find symbol
			Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
			^
  symbol:   class Intent
  location: class MainActivity
Main.java:73: error: cannot find symbol
			Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
			                            ^
  symbol:   class Intent
  location: class MainActivity
Main.java:73: error: cannot find symbol
			Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
			                                   ^
  symbol:   variable BluetoothAdapter
  location: class MainActivity
Main.java:85: error: cannot find symbol
            BluetoothServerSocket tmp = null;
            ^
  symbol:   class BluetoothServerSocket
  location: class MainActivity.AcceptThread
Main.java:90: error: cannot find symbol
            	Log.e("listenUsingRfcommWithServiceRecord","error");
            	^
  symbol:   variable Log
  location: class MainActivity.AcceptThread
Main.java:96: error: cannot find symbol
            BluetoothSocket socket = null;
            ^
  symbol:   class BluetoothSocket
  location: class MainActivity.AcceptThread
Main.java:101: error: cannot find symbol
                    Log.e("listen","listen");
                    ^
  symbol:   variable Log
  location: class MainActivity.AcceptThread
Main.java:103: error: cannot find symbol
                	Log.e("listen","listen over");
                	^
  symbol:   variable Log
  location: class MainActivity.AcceptThread
Main.java:111: error: cannot find symbol
                	Log.e("Connected","Connected");
                	^
  symbol:   variable Log
  location: class MainActivity.AcceptThread
Main.java:133: error: cannot find symbol
            Log.e("a","a");
            ^
  symbol:   variable Log
  location: class MainActivity.ConnectedThread
Main.java:138: error: cannot find symbol
            	Log.e("get","get");
            	^
  symbol:   variable Log
  location: class MainActivity.ConnectedThread
Main.java:140: error: cannot find symbol
            	Log.e("error","error");
            	^
  symbol:   variable Log
  location: class MainActivity.ConnectedThread
Main.java:149: error: cannot find symbol
            Log.e("br",br.toString());
            ^
  symbol:   variable Log
  location: class MainActivity.ConnectedThread
Main.java:154: error: cannot find symbol
					Log.e("line",line);
					^
  symbol:   variable Log
  location: class MainActivity.ConnectedThread
Main.java:160: error: cannot find symbol
				Log.e("BufferedReader excrption",tmp);
				^
  symbol:   variable Log
  location: class MainActivity.ConnectedThread
Main.java:164: error: cannot find symbol
			Log.e("txt",tmp);
			^
  symbol:   variable Log
  location: class MainActivity.ConnectedThread
53 errors
stdout
Standard output is empty