Advertisement
Guest User

Untitled

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