Advertisement
Guest User

Untitled

a guest
Apr 29th, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.40 KB | None | 0 0
  1. public void CheckSyncButtonPress(View view) {
  2. int REQUEST_ENABLE_BT=1;
  3. //sync button pressed
  4. //check that we can use bluetooth
  5. BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
  6. // Device supports bluetooth so let's try and enable it
  7. if (mBluetoothAdapter == null) {
  8. // Device does not support Bluetooth
  9. }
  10. else {
  11. if (!mBluetoothAdapter.isEnabled()) {
  12. //enable message popup
  13.  
  14. Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
  15. startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
  16. }
  17. else
  18. {//if it IS enabled
  19. Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
  20. android.bluetooth.BluetoothDevice targetDevice = null; //variable for found device
  21.  
  22. String targetName="HC-06";
  23. if (pairedDevices.size() > 0) {
  24. // If there are paired devices
  25. // Loop through paired devices
  26. for (BluetoothDevice device : pairedDevices) {
  27. if (Objects.equals(device.getName().trim(), targetName))
  28. {
  29. targetDevice=device;
  30. found=true;
  31. //found one that has the right name
  32. }
  33. }
  34. }
  35. if (found==true)
  36. {
  37. //we have a target device for connection
  38.  
  39. UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb"); //Standard SerialPortService ID
  40. try {
  41. android.bluetooth.BluetoothSocket mmSocket = targetDevice.createRfcommSocketToServiceRecord(uuid);
  42. mmSocket.connect();
  43. mmOutputStream = mmSocket.getOutputStream();
  44. mmInputStream = mmSocket.getInputStream();
  45. CheckBox checkBox = (CheckBox)findViewById(R.id.StatusBox);
  46. checkBox.setChecked(true);
  47. workerThread.start();
  48. } catch (IOException e) {
  49. e.printStackTrace();
  50. }
  51. }
  52.  
  53. }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement