Advertisement
Guest User

Untitled

a guest
Dec 21st, 2014
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.86 KB | None | 0 0
  1. package com.example.acc;
  2.  
  3. import java.io.IOException;
  4. import java.io.OutputStream;
  5. import java.util.UUID;
  6.  
  7. import com.example.acc.R;
  8.  
  9. import android.app.Activity;
  10. import android.bluetooth.BluetoothAdapter;
  11. import android.bluetooth.BluetoothDevice;
  12. import android.bluetooth.BluetoothSocket;
  13. import android.content.Context;
  14. import android.content.Intent;
  15. import android.os.Bundle;
  16. import android.util.Log;
  17. import android.view.MenuItem;
  18. import android.view.View;
  19. import android.view.View.OnClickListener;
  20. import android.widget.Button;
  21. import android.widget.ImageButton;
  22. import android.widget.Toast;
  23.  
  24. public class MainActivity45 extends Activity {
  25. private static final String TAG = "LEDOnOff";
  26.  
  27. ImageButton btnOn;
  28.  
  29. private static final int REQUEST_ENABLE_BT = 1;
  30. private BluetoothAdapter btAdapter = null;
  31. private BluetoothSocket btSocket = null;
  32. private OutputStream outStream = null;
  33.  
  34. // Well known SPP UUID
  35. private static final UUID MY_UUID =
  36. UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
  37.  
  38. // Insert your server's MAC address
  39. private static String address = "00:6A:8E:16:C6:26";
  40.  
  41. /** Called when the activity is first created. */
  42. @Override
  43. public void onCreate(Bundle savedInstanceState) {
  44. super.onCreate(savedInstanceState);
  45.  
  46. Log.d(TAG, "In onCreate()");
  47.  
  48. setContentView(R.layout.mainactivity45);
  49.  
  50. btnOn = (ImageButton) findViewById(R.id.btnOn);
  51.  
  52.  
  53. btAdapter = BluetoothAdapter.getDefaultAdapter();
  54. checkBTState();
  55.  
  56. btnOn.setOnClickListener(new OnClickListener() {
  57. int pressed = 0;
  58. public void onClick(View v) {
  59. if(pressed<=0){
  60. sendData("h");
  61. Toast msg = Toast.makeText(getBaseContext(),
  62. "Chargement", Toast.LENGTH_SHORT);
  63. msg.show();
  64. pressed++;}
  65.  
  66.  
  67. else if(pressed>=1){
  68. sendData("l");
  69. Toast msg = Toast.makeText(getBaseContext(),
  70. "Stop", Toast.LENGTH_SHORT);
  71. msg.show();
  72. pressed--;
  73. }
  74.  
  75.  
  76. }
  77. });
  78.  
  79.  
  80. }
  81.  
  82. @Override
  83. public void onResume() {
  84. super.onResume();
  85.  
  86. Log.d(TAG, "...In onResume - Attempting client connect...");
  87.  
  88. // Set up a pointer to the remote node using it's address.
  89. BluetoothDevice device = btAdapter.getRemoteDevice(address);
  90.  
  91. // Two things are needed to make a connection:
  92. // A MAC address, which we got above.
  93. // A Service ID or UUID. In this case we are using the
  94. // UUID for SPP.
  95. try {
  96. btSocket = device.createRfcommSocketToServiceRecord(MY_UUID);
  97. } catch (IOException e) {
  98. errorExit("Fatal Error", "In onResume() and socket create failed: " + e.getMessage() + ".");
  99. }
  100.  
  101. // Discovery is resource intensive. Make sure it isn't going on
  102. // when you attempt to connect and pass your message.
  103. btAdapter.cancelDiscovery();
  104.  
  105. // Establish the connection. This will block until it connects.
  106. Log.d(TAG, "...Connecting to Remote...");
  107. try {
  108. btSocket.connect();
  109. Log.d(TAG, "...Connection established and data link opened...");
  110. } catch (IOException e) {
  111. try {
  112. btSocket.close();
  113. } catch (IOException e2) {
  114. errorExit("Fatal Error", "In onResume() and unable to close socket during connection failure" + e2.getMessage() + ".");
  115. }
  116. }
  117.  
  118. // Create a data stream so we can talk to server.
  119. Log.d(TAG, "...Creating Socket...");
  120.  
  121. try {
  122. outStream = btSocket.getOutputStream();
  123. } catch (IOException e) {
  124. errorExit("Fatal Error", "In onResume() and output stream creation failed:" + e.getMessage() + ".");
  125. }
  126. }
  127.  
  128. @Override
  129. public void onPause() {
  130. super.onPause();
  131.  
  132. Log.d(TAG, "...In onPause()...");
  133.  
  134. if (outStream != null) {
  135. try {
  136. outStream.flush();
  137. } catch (IOException e) {
  138. errorExit("Fatal Error", "In onPause() and failed to flush output stream: " + e.getMessage() + ".");
  139. }
  140. }
  141.  
  142. try {
  143. btSocket.close();
  144. } catch (IOException e2) {
  145. errorExit("Fatal Error", "In onPause() and failed to close socket." + e2.getMessage() + ".");
  146. }
  147. }
  148.  
  149. private void checkBTState() {
  150. // Check for Bluetooth support and then check to make sure it is turned on
  151.  
  152. // Emulator doesn't support Bluetooth and will return null
  153. if(btAdapter==null) {
  154. errorExit("Fatal Error", "Bluetooth Not supported. Aborting.");
  155. } else {
  156. if (btAdapter.isEnabled()) {
  157. Log.d(TAG, "...Bluetooth is enabled...");
  158. } else {
  159. //Prompt user to turn on Bluetooth
  160. Intent enableBtIntent = new Intent(btAdapter.ACTION_REQUEST_ENABLE);
  161. startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
  162. }
  163. }
  164. }
  165.  
  166. private void errorExit(String title, String message){
  167. Toast msg = Toast.makeText(getBaseContext(),
  168. title + " - " + message, Toast.LENGTH_SHORT);
  169. msg.show();
  170. finish();
  171. }
  172.  
  173. @Override
  174. public boolean onOptionsItemSelected(MenuItem item) {
  175. // Handle action bar item clicks here. The action bar will
  176. // automatically handle clicks on the Home/Up button, so long
  177. // as you specify a parent activity in AndroidManifest.xml.
  178. int id = item.getItemId();
  179. if (id == R.id.action_search) {
  180.  
  181.  
  182.  
  183.  
  184.  
  185.  
  186.  
  187.  
  188.  
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195. return true;
  196.  
  197. }
  198. return super.onOptionsItemSelected(item);
  199. }
  200.  
  201.  
  202. private void sendData(String message) {
  203. byte[] msgBuffer = message.getBytes();
  204.  
  205. Log.d(TAG, "...Sending data: " + message + "...");
  206.  
  207. try {
  208. outStream.write(msgBuffer);
  209. } catch (IOException e) {
  210. String msg = "In onResume() and an exception occurred during write: " + e.getMessage();
  211. if (address.equals("00:00:00:00:00:00"))
  212. msg = msg + ".nnUpdate your server address from 00:00:00:00:00:00 to the correct address on line 37 in the java code";
  213. msg = msg + ".nnCheck that the SPP UUID: " + MY_UUID.toString() + " exists on server.nn";
  214.  
  215. errorExit("Fatal Error", msg);
  216. }
  217. }
  218. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement