Hello friends,
today we are going to learn about Bluetooth in our regular android mobile or other Bluetooth devices. using Bluetooth we can send files, music, image or etc. Bluetooth provides many types of function for work with other Bluetooth devices. so today we are going to take an example of Bluetooth and learn how it's work.
Below code is for Android devices or android application. using below code we can on/off Bluetooth in our android mobile and paring with another device .let's take a look, please.
first of all, we need to provide some permission in android. so write in the androidmainfest.xml file.
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
now we need to create the design for work with Bluetooth. so let's create the activity_main.xml file.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="50dp"
tools:context=".MainActivity">
<RelativeLayout
android:id="@+id/blthlayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true">
<Button
android:id="@+id/blthon"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:clickable="true"
android:text="Turn On" />
<Button
android:id="@+id/blthvisible"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/blthon"
android:text="Get visible" />
<Button
android:id="@+id/blthlistofdevice"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_below="@+id/blthon"
android:onClick="getlistofdevice"
android:text="List devices" />
<Button
android:id="@+id/blthoff"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_below="@+id/blthon"
android:layout_toRightOf="@id/blthlistofdevice"
android:text="turn off" />
</RelativeLayout>
<TextView
android:id="@+id/msg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/blthlayout"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
android:text="Paired Connection"
android:textColor="@color/colorAccent"
android:textSize="20dp" />
<ListView
android:id="@+id/blthlist"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_below="@+id/msg" />
</RelativeLayout>
So now it's time create MainActivity.java so let's do it.
MainActivity.java
public class MainActivity extends AppCompatActivity {
Button blthon, blthvisible, blthoff;
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
private Set<BluetoothDevice> pairedDeviceslist;
ListView blthlist;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
blthon = (Button) findViewById(R.id.blthon);
blthvisible = (Button) findViewById(R.id.blthvisible);
blthoff = (Button) findViewById(R.id.blthoff);
blthlist = (ListView) findViewById(R.id.blthlist);
blthon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (bluetoothAdapter != null && !bluetoothAdapter.isEnabled()) {
Intent turnOn = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(turnOn, 0);
Toast.makeText(getApplicationContext(), " Bluetooth Turned on", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), " Bluetooth Already on", Toast.LENGTH_LONG).show();
}
}
});
blthvisible.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent getVisible = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
startActivityForResult(getVisible, 0);
}
});
blthoff.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
bluetoothAdapter.disable();
Toast.makeText(getApplicationContext(), " Bluetooth Turned off", Toast.LENGTH_LONG).show();
}
});
}
public void getlistofdevice(View v1) {
pairedDeviceslist = bluetoothAdapter.getBondedDevices();
ArrayList list = new ArrayList();
for (BluetoothDevice bt : pairedDeviceslist) 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);
blthlist.setAdapter(adapter);
}
}
today we are going to learn about Bluetooth in our regular android mobile or other Bluetooth devices. using Bluetooth we can send files, music, image or etc. Bluetooth provides many types of function for work with other Bluetooth devices. so today we are going to take an example of Bluetooth and learn how it's work.
Below code is for Android devices or android application. using below code we can on/off Bluetooth in our android mobile and paring with another device .let's take a look, please.
first of all, we need to provide some permission in android. so write in the androidmainfest.xml file.
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
now we need to create the design for work with Bluetooth. so let's create the activity_main.xml file.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="50dp"
tools:context=".MainActivity">
<RelativeLayout
android:id="@+id/blthlayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true">
<Button
android:id="@+id/blthon"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:clickable="true"
android:text="Turn On" />
<Button
android:id="@+id/blthvisible"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/blthon"
android:text="Get visible" />
<Button
android:id="@+id/blthlistofdevice"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_below="@+id/blthon"
android:onClick="getlistofdevice"
android:text="List devices" />
<Button
android:id="@+id/blthoff"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_below="@+id/blthon"
android:layout_toRightOf="@id/blthlistofdevice"
android:text="turn off" />
</RelativeLayout>
<TextView
android:id="@+id/msg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/blthlayout"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
android:text="Paired Connection"
android:textColor="@color/colorAccent"
android:textSize="20dp" />
<ListView
android:id="@+id/blthlist"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_below="@+id/msg" />
</RelativeLayout>
So now it's time create MainActivity.java so let's do it.
MainActivity.java
public class MainActivity extends AppCompatActivity {
Button blthon, blthvisible, blthoff;
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
private Set<BluetoothDevice> pairedDeviceslist;
ListView blthlist;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
blthon = (Button) findViewById(R.id.blthon);
blthvisible = (Button) findViewById(R.id.blthvisible);
blthoff = (Button) findViewById(R.id.blthoff);
blthlist = (ListView) findViewById(R.id.blthlist);
blthon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (bluetoothAdapter != null && !bluetoothAdapter.isEnabled()) {
Intent turnOn = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(turnOn, 0);
Toast.makeText(getApplicationContext(), " Bluetooth Turned on", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), " Bluetooth Already on", Toast.LENGTH_LONG).show();
}
}
});
blthvisible.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent getVisible = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
startActivityForResult(getVisible, 0);
}
});
blthoff.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
bluetoothAdapter.disable();
Toast.makeText(getApplicationContext(), " Bluetooth Turned off", Toast.LENGTH_LONG).show();
}
});
}
public void getlistofdevice(View v1) {
pairedDeviceslist = bluetoothAdapter.getBondedDevices();
ArrayList list = new ArrayList();
for (BluetoothDevice bt : pairedDeviceslist) 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);
blthlist.setAdapter(adapter);
}
}
Comments
Post a Comment