logo-StatusNeo
d

WE ARE EXPERTS IN TECHNOLOGY

Let’s Work Together

Image Alt

StatusNeo

Bluetooth’s basic Connection for Initial Setup : Android

The Android platform supports Bluetooth connectivity, allowing data to be exchanged with other Bluetooth devices. The application framework provides access to Bluetooth functionality by connecting the Android Bluetooth API to other Bluetooth devices. All Bluetooth APIs are available in the Android Bluetooth package.

Here is a summary of the classes you need to do as below:

  1. BluetoothAdapter – Represents a local Bluetooth adapter (Bluetooth radio). 
  2. BluetoothDevice – Represents a remote Bluetooth device that requires information such as its name, address, class, and connection status. 
  3. BluetoothSocket – Represents an interface for a Bluetooth socket (such as a TCP socket). 
  4. BluetoothServerSocket – Represents an open server socket that listens for incoming requests (like a TCP ServerSocket). 
  5. BluetoothClass – For Bluetooth device’s general characteristics and capabilities.

Step 1- Check the Bluetooth connection 

First we need to check the Bluetooth connection. So create a new class called Connections.

public class Connections {

    private static boolean state = false;

    public static boolean blueTooth() {

        BluetoothAdapter bluetooth =      BluetoothAdapter.getDefaultAdapter();

        if (!bluetooth.isEnabled()) {

            System.out.println(“Bluetooth is Disable…”);

            state = true;

        } else if (bluetooth.isEnabled()) {

            String address = bluetooth.getAddress();

            String name = bluetooth.getName();

            System.out.println(name + ” : ” + address);

            state = false;

        }

        return state;

    }

}

Step 2- Use of Adapter and fetch devices

Now when we receive a response from the Connections category in our main work with BT turned off, the On BT dialog will appear. So let’s create a new method in the class to set all bluetooth info in view.

public class MainActivity extends Activity {

    private static final int REQUEST_ENABLE_BT = 12;

    private TextView out;

    private BluetoothAdapter adapter;

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        out = (TextView) findViewById(R.id.tvBluetoothInfo);

        setBluetoothData();

        if (Connections.blueTooth()) {

            Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);

            startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);

        }

    }

    @Override

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        // TODO Auto-generated method stub

        super.onActivityResult(requestCode, resultCode, data);

        out.setText(“”);

        setBluetoothData();

    }

    private void setBluetoothData() {

        // Getting the Bluetooth adapter

        adapter = BluetoothAdapter.getDefaultAdapter();

        out.append(“\nAdapter: ” + adapter.toString() + “\n\nName: ” + adapter.getName() + “\nAddress: ” + adapter.getAddress());

        // Check for Bluetooth support in the first place

        // Emulator doesn’t support Bluetooth and will return null

        if (adapter == null) {

            Toast.makeText(this, “Bluetooth NOT supported. Aborting.”,

            Toast.LENGTH_LONG).show();

        }

        // Starting the device discovery

        out.append(“\n\nStarting discovery…”);

        adapter.startDiscovery();

        out.append(“\nDone with discovery…\n”);

        // Listing paired devices

        out.append(“\nDevices Pared:”);

        Set<BluetoothDevice> devices = adapter.getBondedDevices();

        for (BluetoothDevice device : devices) {

            out.append(“\nFound device: ” + device.getName() + ” Add: ” + device.getAddress());

        }

    }

}

Add Comment