Advertisement
Lucky134Lucky

Untitled

Feb 13th, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.15 KB | None | 0 0
  1. package com.example.lucky134.bluetoothcontroller;
  2.  
  3. import android.bluetooth.BluetoothAdapter;
  4. import android.bluetooth.BluetoothDevice;
  5. import android.bluetooth.BluetoothSocket;
  6. import android.content.Intent;
  7. import android.os.AsyncTask;
  8. import android.os.Bundle;
  9. import android.support.design.widget.Snackbar;
  10. import android.support.v7.app.AppCompatActivity;
  11. import android.support.v7.widget.Toolbar;
  12. import android.util.Log;
  13. import android.view.View;
  14. import android.widget.AdapterView;
  15. import android.widget.ArrayAdapter;
  16. import android.widget.Spinner;
  17. import android.widget.Toast;
  18.  
  19. import java.io.IOException;
  20. import java.io.OutputStream;
  21. import java.util.ArrayList;
  22. import java.util.Set;
  23.  
  24. import static com.example.lucky134.bluetoothcontroller.Constants.REQUEST_ENABLE_BT;
  25.  
  26. public class MainActivity extends AppCompatActivity implements Spinner.OnItemSelectedListener {
  27.  
  28. public static final String TAG = MainActivity.class.getSimpleName();
  29.  
  30. private OutputStream mBluetoothOutputStream; // stream in which we will send commands and etc.
  31. private ArrayList<BluetoothDevice> mPairedDevices = new ArrayList<>();
  32. private BluetoothSocket mSocketConnection;
  33.  
  34. @Override
  35. protected void onCreate(Bundle savedInstanceState) {
  36. super.onCreate(savedInstanceState);
  37. setContentView(R.layout.activity_main);
  38.  
  39. if (BluetoothAdapter.getDefaultAdapter() == null) { // if device doesnt support bluetooth we just finish activity
  40. Toast.makeText(getApplicationContext(), R.string.no_bluetooth, Toast.LENGTH_SHORT).show();
  41. finish();
  42. }
  43.  
  44. if (!BluetoothAdapter.getDefaultAdapter().isEnabled()) // get to know bluetooth enabled or no
  45. requestBluetooth();
  46. else
  47. setupActionBar();
  48. }
  49.  
  50. public void setupActionBar() { // configures actionbar
  51. setSupportActionBar((Toolbar) findViewById(R.id.toolbar));
  52.  
  53. if (getSupportActionBar() == null)
  54. return;
  55.  
  56. if (mSocketConnection == null
  57. || !mSocketConnection.isConnected())
  58. getSupportActionBar().setSubtitle(getString(R.string.device_not_connected));
  59. else
  60. getSupportActionBar().setSubtitle(mSocketConnection.getRemoteDevice().getName());
  61.  
  62. setupPairedDevicesSpinner();
  63. }
  64.  
  65. private void setupPairedDevicesSpinner() {
  66. ArrayList<String> mDevicesNames = new ArrayList<>();
  67. BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
  68. Set<BluetoothDevice> mPairedSet = mBluetoothAdapter.getBondedDevices();
  69. mDevicesNames.add(getString(R.string.nothing_selected));
  70. for (BluetoothDevice bt : mPairedSet) {
  71. if (bt == null)
  72. continue;
  73.  
  74. if (bt.getName() == null)
  75. continue;
  76.  
  77. if (bt.getName().isEmpty())
  78. continue;
  79.  
  80. mPairedDevices.add(bt);
  81. mDevicesNames.add(bt.getName());
  82. }
  83.  
  84. ArrayAdapter<String> adapter = new ArrayAdapter<>(this,
  85. android.R.layout.simple_spinner_item, mDevicesNames);
  86.  
  87. Spinner spinner = (Spinner) findViewById(R.id.s_connected_devices);
  88. spinner.setAdapter(adapter);
  89. spinner.setOnItemSelectedListener(this);
  90. }
  91.  
  92. public void sendMessage(byte message) { // use this function for sending command to bluetooth device
  93. Log.i(TAG, "Outgoing message to connected device = " + message);
  94.  
  95. if (mSocketConnection == null || !mSocketConnection.isConnected()) {
  96. makeSnackbar(getString(R.string.device_not_connected));
  97. return;
  98. }
  99.  
  100. if (mBluetoothOutputStream == null) {
  101. makeSnackbar(getString(R.string.device_not_connected));
  102. return;
  103. }
  104.  
  105. try {
  106. mBluetoothOutputStream.write(message);
  107. } catch (IOException e) {
  108. e.printStackTrace();
  109.  
  110. makeSnackbar(getString(R.string.cant_send_command));
  111. }
  112. }
  113.  
  114.  
  115. @Override
  116. protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
  117. if (resultCode != RESULT_OK) {
  118. Log.i(TAG, "User doesn't wants to enable bluetooth");
  119. finish();
  120. }
  121.  
  122. setupActionBar();
  123. }
  124.  
  125. private void requestBluetooth() {
  126. Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
  127. startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
  128. }
  129.  
  130. private void connectToBluetoothDevice(BluetoothDevice bluetoothDevice) {
  131. if (bluetoothDevice == null)
  132. return;
  133.  
  134. if (getSupportActionBar() != null)
  135. getSupportActionBar().setSubtitle(String.format(getString(R.string.connecting_to), bluetoothDevice.getName()));
  136.  
  137. new AsyncTask<Void, Void, Boolean>() {
  138. @Override
  139. protected Boolean doInBackground(Void... params) {
  140. try {
  141. if (mSocketConnection != null && mSocketConnection.isConnected()) {
  142. mBluetoothOutputStream.close();
  143. mSocketConnection.close();
  144. }
  145.  
  146. mSocketConnection = bluetoothDevice.createRfcommSocketToServiceRecord(Constants.MY_UUID_SECURE);
  147. mSocketConnection.connect();
  148. mBluetoothOutputStream = mSocketConnection.getOutputStream();
  149. return true;
  150. } catch (IOException e) {
  151. e.printStackTrace();
  152. }
  153. return false;
  154. }
  155.  
  156. @Override
  157. protected void onPostExecute(Boolean isConnected) {
  158. if (isConnected)
  159. getSupportActionBar().setSubtitle(String.format(getString(R.string.connected_to), bluetoothDevice.getName()));
  160. else {
  161. getSupportActionBar().setSubtitle(getString(R.string.device_not_connected));
  162. makeSnackbar(getString(R.string.connection_error));
  163. }
  164. }
  165. }.execute();
  166.  
  167. }
  168.  
  169. private void makeSnackbar(String text) {
  170. if (text == null)
  171. return;
  172.  
  173. if (text.isEmpty())
  174. return;
  175.  
  176. Snackbar.make(findViewById(R.id.relativeLayout), text, Snackbar.LENGTH_SHORT).show();
  177.  
  178. }
  179.  
  180. @Override
  181. public void onNothingSelected(AdapterView<?> parent) {
  182.  
  183. }
  184.  
  185. @Override
  186. public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
  187. if (position == Constants.NOTHING_SELECTED)
  188. return;
  189.  
  190. BluetoothDevice mDeviceToConnect = mPairedDevices.get(position);
  191.  
  192. if (mDeviceToConnect == null)
  193. return;
  194.  
  195. connectToBluetoothDevice(mDeviceToConnect);
  196. }
  197.  
  198. @Override
  199. protected void onDestroy() {
  200. super.onDestroy();
  201.  
  202. if (mSocketConnection != null && mSocketConnection.isConnected())
  203. try {
  204. mBluetoothOutputStream.close();
  205. mSocketConnection.close();
  206.  
  207. } catch (IOException e) {
  208. e.printStackTrace();
  209. }
  210. }
  211. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement