fork download
  1. import android.app.Activity;
  2. import android.bluetooth.BluetoothAdapter;
  3. import android.bluetooth.BluetoothDevice;
  4. import android.bluetooth.BluetoothSocket;
  5. import android.content.Intent;
  6. import android.os.Bundle;
  7. import android.os.Handler;
  8. import android.view.View;
  9. import android.widget.TextView;
  10. import android.widget.EditText;
  11. import android.widget.Button;
  12. import java.io.IOException;
  13. import java.io.InputStream;
  14. import java.io.OutputStream;
  15. import java.util.Set;
  16. import java.util.UUID;
  17.  
  18. public class MainActivity extends Activity
  19. {
  20. TextView myLabel;
  21. EditText myTextbox;
  22. BluetoothAdapter mBluetoothAdapter;
  23. BluetoothSocket mmSocket;
  24. BluetoothDevice mmDevice;
  25. OutputStream mmOutputStream;
  26. InputStream mmInputStream;
  27. Thread workerThread;
  28. byte[] readBuffer;
  29. int readBufferPosition;
  30. int counter;
  31. volatile boolean stopWorker;
  32.  
  33. @Override
  34. public void onCreate(Bundle savedInstanceState)
  35. {
  36. super.onCreate(savedInstanceState);
  37. setContentView(R.layout.activity_main);
  38.  
  39. Button openButton = (Button)findViewById(R.id.open);
  40. Button sendButton = (Button)findViewById(R.id.send);
  41. Button closeButton = (Button)findViewById(R.id.close);
  42. myLabel = (TextView)findViewById(R.id.label);
  43. myTextbox = (EditText)findViewById(R.id.entry);
  44.  
  45. //Open Button
  46. openButton.setOnClickListener(new View.OnClickListener()
  47. {
  48. public void onClick(View v)
  49. {
  50. try
  51. {
  52. findBT();
  53. openBT();
  54. }
  55. catch (IOException ex) { }
  56. }
  57. });
  58.  
  59. //Send Button
  60. sendButton.setOnClickListener(new View.OnClickListener()
  61. {
  62. @Override
  63. public void onClick(View v)
  64. {
  65. try
  66. {
  67. sendData();
  68. }
  69. catch (IOException ex) { }
  70. }
  71. });
  72.  
  73. //Close button
  74. closeButton.setOnClickListener(new View.OnClickListener()
  75. {
  76. public void onClick(View v)
  77. {
  78. try
  79. {
  80. closeBT();
  81. }
  82. catch (IOException ex) { }
  83. }
  84. });
  85. }
  86.  
  87. void findBT()
  88. {
  89. mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
  90. if(mBluetoothAdapter == null)
  91. {
  92. myLabel.setText("No bluetooth adapter available");
  93. }
  94.  
  95. if(!mBluetoothAdapter.isEnabled())
  96. {
  97. Intent enableBluetooth = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
  98. startActivityForResult(enableBluetooth, 0);
  99. }
  100.  
  101. Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
  102. if(pairedDevices.size() > 0)
  103. {
  104. for(BluetoothDevice device : pairedDevices)
  105. {
  106.  
  107. {
  108. myLabel.setText(device.getName());
  109. mmDevice = device;
  110. break;
  111. }
  112. }
  113. }
  114. }
  115.  
  116. void openBT() throws IOException
  117. {
  118. UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); //Standard SerialPortService ID
  119. mmSocket = mmDevice.createRfcommSocketToServiceRecord(uuid);
  120. mmSocket.connect();
  121. mmOutputStream = mmSocket.getOutputStream();
  122. mmInputStream = mmSocket.getInputStream();
  123.  
  124. beginListenForData();
  125.  
  126. myLabel.setText("Bluetooth Opened");
  127. }
  128.  
  129. void beginListenForData()
  130. {
  131. final Handler handler = new Handler();
  132. final byte delimiter = 10; //This is the ASCII code for a newline character
  133.  
  134. stopWorker = false;
  135. readBufferPosition = 0;
  136. readBuffer = new byte[1024];
  137. workerThread = new Thread(new Runnable()
  138. {
  139. public void run()
  140. {
  141. while(!Thread.currentThread().isInterrupted() && !stopWorker)
  142. {
  143. try
  144. {
  145. int bytesAvailable = mmInputStream.available();
  146. if(bytesAvailable > 0)
  147. {
  148. byte[] packetBytes = new byte[bytesAvailable];
  149. mmInputStream.read(packetBytes);
  150. for(int i=0;i<bytesAvailable;i++)
  151. {
  152. byte b = packetBytes[i];
  153. if(b == delimiter)
  154. {
  155. byte[] encodedBytes = new byte[readBufferPosition];
  156. System.arraycopy(readBuffer, 0, encodedBytes, 0, encodedBytes.length);
  157. final String data = new String(encodedBytes, "US-ASCII");
  158. readBufferPosition = 0;
  159.  
  160. handler.post(new Runnable()
  161. {
  162. public void run()
  163. {
  164. String t1=myLabel.getText().toString();
  165. t1=t1+data;
  166. myLabel.setText(data);
  167. }
  168. });
  169. }
  170. else
  171. {
  172. readBuffer[readBufferPosition++] = b;
  173. }
  174. }
  175. }
  176. }
  177. catch (IOException ex)
  178. {
  179. stopWorker = true;
  180. }
  181. }
  182. }
  183. });
  184.  
  185. workerThread.start();
  186. }
  187.  
  188. void sendData() throws IOException
  189. {
  190. String msg = myTextbox.getText().toString();
  191. msg += "\n";
  192. mmOutputStream.write(msg.getBytes());
  193. myLabel.setText("Data Sent");
  194. }
  195.  
  196. void closeBT() throws IOException
  197. {
  198. stopWorker = true;
  199. mmOutputStream.close();
  200. mmInputStream.close();
  201. mmSocket.close();
  202. myLabel.setText("Bluetooth Closed");
  203. }
  204. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:18: error: class MainActivity is public, should be declared in a file named MainActivity.java
public class MainActivity extends Activity
       ^
Main.java:1: error: package android.app does not exist
import android.app.Activity;
                  ^
Main.java:2: error: package android.bluetooth does not exist
import android.bluetooth.BluetoothAdapter;
                        ^
Main.java:3: error: package android.bluetooth does not exist
import android.bluetooth.BluetoothDevice;
                        ^
Main.java:4: error: package android.bluetooth does not exist
import android.bluetooth.BluetoothSocket;
                        ^
Main.java:5: error: package android.content does not exist
import android.content.Intent;
                      ^
Main.java:6: error: package android.os does not exist
import android.os.Bundle;
                 ^
Main.java:7: error: package android.os does not exist
import android.os.Handler;
                 ^
Main.java:8: error: package android.view does not exist
import android.view.View;
                   ^
Main.java:9: error: package android.widget does not exist
import android.widget.TextView;
                     ^
Main.java:10: error: package android.widget does not exist
import android.widget.EditText;  
                     ^
Main.java:11: error: package android.widget does not exist
import android.widget.Button;
                     ^
Main.java:18: error: cannot find symbol
public class MainActivity extends Activity
                                  ^
  symbol: class Activity
Main.java:20: error: cannot find symbol
    TextView myLabel;
    ^
  symbol:   class TextView
  location: class MainActivity
Main.java:21: error: cannot find symbol
    EditText myTextbox;
    ^
  symbol:   class EditText
  location: class MainActivity
Main.java:22: error: cannot find symbol
    BluetoothAdapter mBluetoothAdapter;
    ^
  symbol:   class BluetoothAdapter
  location: class MainActivity
Main.java:23: error: cannot find symbol
    BluetoothSocket mmSocket;
    ^
  symbol:   class BluetoothSocket
  location: class MainActivity
Main.java:24: error: cannot find symbol
    BluetoothDevice mmDevice;
    ^
  symbol:   class BluetoothDevice
  location: class MainActivity
Main.java:34: error: cannot find symbol
    public void onCreate(Bundle savedInstanceState)
                         ^
  symbol:   class Bundle
  location: class MainActivity
Main.java:33: error: method does not override or implement a method from a supertype
    @Override
    ^
Main.java:36: error: cannot find symbol
        super.onCreate(savedInstanceState);
        ^
  symbol:   variable super
  location: class MainActivity
Main.java:37: error: package R does not exist
        setContentView(R.layout.activity_main);
                        ^
Main.java:39: error: cannot find symbol
        Button openButton = (Button)findViewById(R.id.open);
        ^
  symbol:   class Button
  location: class MainActivity
Main.java:39: error: cannot find symbol
        Button openButton = (Button)findViewById(R.id.open);
                             ^
  symbol:   class Button
  location: class MainActivity
Main.java:39: error: package R does not exist
        Button openButton = (Button)findViewById(R.id.open);
                                                  ^
Main.java:40: error: cannot find symbol
        Button sendButton = (Button)findViewById(R.id.send);
        ^
  symbol:   class Button
  location: class MainActivity
Main.java:40: error: cannot find symbol
        Button sendButton = (Button)findViewById(R.id.send);
                             ^
  symbol:   class Button
  location: class MainActivity
Main.java:40: error: package R does not exist
        Button sendButton = (Button)findViewById(R.id.send);
                                                  ^
Main.java:41: error: cannot find symbol
        Button closeButton = (Button)findViewById(R.id.close);
        ^
  symbol:   class Button
  location: class MainActivity
Main.java:41: error: cannot find symbol
        Button closeButton = (Button)findViewById(R.id.close);
                              ^
  symbol:   class Button
  location: class MainActivity
Main.java:41: error: package R does not exist
        Button closeButton = (Button)findViewById(R.id.close);
                                                   ^
Main.java:42: error: cannot find symbol
        myLabel = (TextView)findViewById(R.id.label);
                   ^
  symbol:   class TextView
  location: class MainActivity
Main.java:42: error: package R does not exist
        myLabel = (TextView)findViewById(R.id.label);
                                          ^
Main.java:43: error: cannot find symbol
        myTextbox = (EditText)findViewById(R.id.entry);
                     ^
  symbol:   class EditText
  location: class MainActivity
Main.java:43: error: package R does not exist
        myTextbox = (EditText)findViewById(R.id.entry);
                                            ^
Main.java:46: error: package View does not exist
        openButton.setOnClickListener(new View.OnClickListener()
                                              ^
Main.java:60: error: package View does not exist
        sendButton.setOnClickListener(new View.OnClickListener()
                                              ^
Main.java:74: error: package View does not exist
        closeButton.setOnClickListener(new View.OnClickListener()
                                               ^
Main.java:89: error: cannot find symbol
        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
                            ^
  symbol:   variable BluetoothAdapter
  location: class MainActivity
Main.java:97: error: cannot find symbol
            Intent enableBluetooth = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            ^
  symbol:   class Intent
  location: class MainActivity
Main.java:97: error: cannot find symbol
            Intent enableBluetooth = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                                         ^
  symbol:   class Intent
  location: class MainActivity
Main.java:97: error: cannot find symbol
            Intent enableBluetooth = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                                                ^
  symbol:   variable BluetoothAdapter
  location: class MainActivity
Main.java:101: error: cannot find symbol
        Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
            ^
  symbol:   class BluetoothDevice
  location: class MainActivity
Main.java:104: error: cannot find symbol
            for(BluetoothDevice device : pairedDevices)
                ^
  symbol:   class BluetoothDevice
  location: class MainActivity
Main.java:131: error: cannot find symbol
        final Handler handler = new Handler(); 
              ^
  symbol:   class Handler
  location: class MainActivity
Main.java:131: error: cannot find symbol
        final Handler handler = new Handler(); 
                                    ^
  symbol:   class Handler
  location: class MainActivity
46 errors
stdout
Standard output is empty