Your experience on this site will be improved by allowing cookies
To exchange data with other devices wirelessly, Bluetooth is used. The Bluetooth framework supported by the Android platform allows a device to send or receive data between two different devices. The Bluetooth API of Android is used to perform these tasks and many more:
To scan Bluetooth devices
To connect and transfer data from and to other devices
To manage multiple connections
The interfaces classes to work with Bluetooth are included in the android.bluetooth package. These are:
To perform the fundamental tasks, like to initiate a device discovery, to query a list of paired or bonded devices, to create a BluetoothServerSocket instance to listen for connection requests, etc, the BluetoothAdapter class is used.
There are various constants provided by the BluetoothAdapter class. Some of these constants are:
The BluetoothAdapter class contains various methods. Some of the important methods of the BluetoothAdapter class are:
activity_main.xml:
In the activity_main.xml file, we will drag a Textview and three buttons from the pallet.
AndroidManifest.xml:
In the AndroidManifest.xml file, we will provide the below permissions:
Syntax:
File: AndroidManifest.xml:
Activity class:(File: MainActivity.java)
In the MainActivity.java file, we will write the code to enable, disable, and to make the Bluetooth discoverable.
package com.example.radioapp; import android.app.Activity; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.ListView; import android.widget.Toast; import java.util.ArrayList; import java.util.Set; public class MainActivity extends Activity { Button b1,b2,b3,b4; private BluetoothAdapter BA; private SetpairedDevices; ListView lv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); b1 = (Button) findViewById(R.id.button); b2=(Button)findViewById(R.id.button2); b3=(Button)findViewById(R.id.button3); b4=(Button)findViewById(R.id.button4); BA = BluetoothAdapter.getDefaultAdapter(); lv = (ListView)findViewById(R.id.listView); } public void on(View v){ if (!BA.isEnabled()) { Intent turnOn = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(turnOn, 0); Toast.makeText(getApplicationContext(), "Turned on",Toast.LENGTH_LONG).show(); } else { Toast.makeText(getApplicationContext(), "Already on", Toast.LENGTH_LONG).show(); } } public void off(View v){ BA.disable(); Toast.makeText(getApplicationContext(), "Turned off" ,Toast.LENGTH_LONG).show(); } public void visible(View v){ Intent getVisible = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE); startActivityForResult(getVisible, 0); } public void list(View v){ pairedDevices = BA.getBondedDevices(); ArrayList list = new ArrayList(); for(BluetoothDevice bt : pairedDevices) list.add(bt.getName()); Toast.makeText(getApplicationContext(), "Showing Paired Devices",Toast.LENGTH_SHORT).show(); final ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1, list); lv.setAdapter(adapter); } } |
0 comments