Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.lucky134.bluetoothcontroller;
- import android.bluetooth.BluetoothAdapter;
- import android.bluetooth.BluetoothDevice;
- import android.bluetooth.BluetoothSocket;
- import android.content.Intent;
- import android.os.AsyncTask;
- import android.os.Bundle;
- import android.support.design.widget.Snackbar;
- import android.support.v7.app.AppCompatActivity;
- import android.support.v7.widget.Toolbar;
- import android.util.Log;
- import android.view.View;
- import android.widget.AdapterView;
- import android.widget.ArrayAdapter;
- import android.widget.Spinner;
- import android.widget.Toast;
- import java.io.IOException;
- import java.io.OutputStream;
- import java.util.ArrayList;
- import java.util.Set;
- import static com.example.lucky134.bluetoothcontroller.Constants.REQUEST_ENABLE_BT;
- public class MainActivity extends AppCompatActivity implements Spinner.OnItemSelectedListener {
- public static final String TAG = MainActivity.class.getSimpleName();
- private OutputStream mBluetoothOutputStream; // stream in which we will send commands and etc.
- private ArrayList<BluetoothDevice> mPairedDevices = new ArrayList<>();
- private BluetoothSocket mSocketConnection;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- if (BluetoothAdapter.getDefaultAdapter() == null) { // if device doesnt support bluetooth we just finish activity
- Toast.makeText(getApplicationContext(), R.string.no_bluetooth, Toast.LENGTH_SHORT).show();
- finish();
- }
- if (!BluetoothAdapter.getDefaultAdapter().isEnabled()) // get to know bluetooth enabled or no
- requestBluetooth();
- else
- setupActionBar();
- }
- public void setupActionBar() { // configures actionbar
- setSupportActionBar((Toolbar) findViewById(R.id.toolbar));
- if (getSupportActionBar() == null)
- return;
- if (mSocketConnection == null
- || !mSocketConnection.isConnected())
- getSupportActionBar().setSubtitle(getString(R.string.device_not_connected));
- else
- getSupportActionBar().setSubtitle(mSocketConnection.getRemoteDevice().getName());
- setupPairedDevicesSpinner();
- }
- private void setupPairedDevicesSpinner() {
- ArrayList<String> mDevicesNames = new ArrayList<>();
- BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
- Set<BluetoothDevice> mPairedSet = mBluetoothAdapter.getBondedDevices();
- mDevicesNames.add(getString(R.string.nothing_selected));
- for (BluetoothDevice bt : mPairedSet) {
- if (bt == null)
- continue;
- if (bt.getName() == null)
- continue;
- if (bt.getName().isEmpty())
- continue;
- mPairedDevices.add(bt);
- mDevicesNames.add(bt.getName());
- }
- ArrayAdapter<String> adapter = new ArrayAdapter<>(this,
- android.R.layout.simple_spinner_item, mDevicesNames);
- Spinner spinner = (Spinner) findViewById(R.id.s_connected_devices);
- spinner.setAdapter(adapter);
- spinner.setOnItemSelectedListener(this);
- }
- public void sendMessage(byte message) { // use this function for sending command to bluetooth device
- Log.i(TAG, "Outgoing message to connected device = " + message);
- if (mSocketConnection == null || !mSocketConnection.isConnected()) {
- makeSnackbar(getString(R.string.device_not_connected));
- return;
- }
- if (mBluetoothOutputStream == null) {
- makeSnackbar(getString(R.string.device_not_connected));
- return;
- }
- try {
- mBluetoothOutputStream.write(message);
- } catch (IOException e) {
- e.printStackTrace();
- makeSnackbar(getString(R.string.cant_send_command));
- }
- }
- @Override
- protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
- if (resultCode != RESULT_OK) {
- Log.i(TAG, "User doesn't wants to enable bluetooth");
- finish();
- }
- setupActionBar();
- }
- private void requestBluetooth() {
- Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
- startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
- }
- private void connectToBluetoothDevice(BluetoothDevice bluetoothDevice) {
- if (bluetoothDevice == null)
- return;
- if (getSupportActionBar() != null)
- getSupportActionBar().setSubtitle(String.format(getString(R.string.connecting_to), bluetoothDevice.getName()));
- new AsyncTask<Void, Void, Boolean>() {
- @Override
- protected Boolean doInBackground(Void... params) {
- try {
- if (mSocketConnection != null && mSocketConnection.isConnected()) {
- mBluetoothOutputStream.close();
- mSocketConnection.close();
- }
- mSocketConnection = bluetoothDevice.createRfcommSocketToServiceRecord(Constants.MY_UUID_SECURE);
- mSocketConnection.connect();
- mBluetoothOutputStream = mSocketConnection.getOutputStream();
- return true;
- } catch (IOException e) {
- e.printStackTrace();
- }
- return false;
- }
- @Override
- protected void onPostExecute(Boolean isConnected) {
- if (isConnected)
- getSupportActionBar().setSubtitle(String.format(getString(R.string.connected_to), bluetoothDevice.getName()));
- else {
- getSupportActionBar().setSubtitle(getString(R.string.device_not_connected));
- makeSnackbar(getString(R.string.connection_error));
- }
- }
- }.execute();
- }
- private void makeSnackbar(String text) {
- if (text == null)
- return;
- if (text.isEmpty())
- return;
- Snackbar.make(findViewById(R.id.relativeLayout), text, Snackbar.LENGTH_SHORT).show();
- }
- @Override
- public void onNothingSelected(AdapterView<?> parent) {
- }
- @Override
- public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
- if (position == Constants.NOTHING_SELECTED)
- return;
- BluetoothDevice mDeviceToConnect = mPairedDevices.get(position);
- if (mDeviceToConnect == null)
- return;
- connectToBluetoothDevice(mDeviceToConnect);
- }
- @Override
- protected void onDestroy() {
- super.onDestroy();
- if (mSocketConnection != null && mSocketConnection.isConnected())
- try {
- mBluetoothOutputStream.close();
- mSocketConnection.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement