Bluetooth Example
Using BluetoothAdapter class we will do the following operations
a set containing list of all paired or bounded bluetooth devices.
Permissions Required: BLUETOOTH, BLUETOOTH_ADMIN
- Check if Bluetooth is available or not.
- Turn On/Off Bluetooth.
- Make Bluetooth Discoverable.
- Display Paired/Bounded devices.
a set containing list of all paired or bounded bluetooth devices.
Permissions Required: BLUETOOTH, BLUETOOTH_ADMIN
Step 1: Create a new project OR Open your project
Step 2: Add 2 Images to show Bluetooth status icons
Step 3: Add BLUETOOTH, BLUETOOTH_ADMIN permission in AndroidMenifest.xml
Step 4: Code
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.blogspot.atifsoftwares.bluetoothexample"> <uses-permission android:name="android.permission.BLUETOOTH"/> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:gravity="center_horizontal" tools:context=".MainActivity"> <!--Display whether bluetooth is available or not--> <TextView android:id="@+id/statusBluetoothTv" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="" android:textAlignment="center" android:textSize="20sp" android:textColor="#000"/> <!--Bluetooth icon (on/off)--> <ImageView android:id="@+id/bluetoothIv" android:layout_width="100dp" android:layout_height="100dp" /> <!--On Button--> <Button android:id="@+id/onBtn" android:minWidth="200dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Turn On" style="@style/Base.Widget.AppCompat.Button.Colored"/> <!--Off btn--> <Button android:id="@+id/offBtn" android:minWidth="200dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Turn Off" style="@style/Base.Widget.AppCompat.Button.Colored"/> <!--Discoverable button--> <Button android:id="@+id/discoverableBtn" android:minWidth="200dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Discoverable" style="@style/Base.Widget.AppCompat.Button.Colored"/> <!--Get list of paired devices button--> <Button android:id="@+id/pairedBtn" android:minWidth="200dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Get Paired Devices" style="@style/Base.Widget.AppCompat.Button.Colored"/> <!--Show paired devices here--> <TextView android:id="@+id/pairedTv" android:minWidth="200dp" android:text="" android:textColor="#000" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>
MainActivity.java
package com.blogspot.atifsoftwares.bluetoothexample; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; import java.util.Set; public class MainActivity extends AppCompatActivity { private static final int REQUEST_ENABLE_BT = 0; private static final int REQUEST_DISCOVER_BT = 1; TextView mStatusBlueTv, mPairedTv; ImageView mBlueIv; Button mOnBtn, mOffBtn, mDiscoverBtn, mPairedBtn; BluetoothAdapter mBlueAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mStatusBlueTv = findViewById(R.id.statusBluetoothTv); mPairedTv = findViewById(R.id.pairedTv); mBlueIv = findViewById(R.id.bluetoothIv); mOnBtn = findViewById(R.id.onBtn); mOffBtn = findViewById(R.id.offBtn); mDiscoverBtn = findViewById(R.id.discoverableBtn); mPairedBtn = findViewById(R.id.pairedBtn); //adapter mBlueAdapter = BluetoothAdapter.getDefaultAdapter(); //check if bluetooth is available or not if (mBlueAdapter == null){ mStatusBlueTv.setText("Bluetooth is not available"); } else { mStatusBlueTv.setText("Bluetooth is available"); } //set image according to bluetooth status(on/off) if (mBlueAdapter.isEnabled()){ mBlueIv.setImageResource(R.drawable.ic_action_on); } else { mBlueIv.setImageResource(R.drawable.ic_action_off); } //on btn click mOnBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (!mBlueAdapter.isEnabled()){ showToast("Turning On Bluetooth..."); //intent to on bluetooth Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(intent, REQUEST_ENABLE_BT); } else { showToast("Bluetooth is already on"); } } }); //discover bluetooth btn click mDiscoverBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (!mBlueAdapter.isDiscovering()){ showToast("Making Your Device Discoverable"); Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE); startActivityForResult(intent, REQUEST_DISCOVER_BT); } } }); //off btn click mOffBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (mBlueAdapter.isEnabled()){ mBlueAdapter.disable(); showToast("Turning Bluetooth Off"); mBlueIv.setImageResource(R.drawable.ic_action_off); } else { showToast("Bluetooth is already off"); } } }); //get paired devices btn click mPairedBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (mBlueAdapter.isEnabled()){ mPairedTv.setText("Paired Devices"); Set<BluetoothDevice> devices = mBlueAdapter.getBondedDevices(); for (BluetoothDevice device: devices){ mPairedTv.append("\nDevice: " + device.getName()+ ", " + device); } } else { //bluetooth is off so can't get paired devices showToast("Turn on bluetooth to get paired devices"); } } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { switch (requestCode){ case REQUEST_ENABLE_BT: if (resultCode == RESULT_OK){ //bluetooth is on mBlueIv.setImageResource(R.drawable.ic_action_on); showToast("Bluetooth is on"); } else { //user denied to turn bluetooth on showToast("could't on bluetooth"); } break; } super.onActivityResult(requestCode, resultCode, data); } //toast message function private void showToast(String msg){ Toast.makeText(this, msg, Toast.LENGTH_SHORT).show(); } }
Comments
Post a Comment