0% found this document useful (0 votes)
4 views3 pages

Bluetooth Coding

The document contains an Android application code that implements Bluetooth functionality using XML for the layout and Java for the logic. It includes a layout with a TextView and a Switch to toggle Bluetooth on and off, along with the necessary permissions for Bluetooth operations. The Java code manages the Bluetooth state and updates the UI based on the user's interaction with the Switch.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

Bluetooth Coding

The document contains an Android application code that implements Bluetooth functionality using XML for the layout and Java for the logic. It includes a layout with a TextView and a Switch to toggle Bluetooth on and off, along with the necessary permissions for Bluetooth operations. The Java code manages the Bluetooth state and updates the UI based on the user's interaction with the Switch.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Bluetooth Coding

XML

<RelativeLayout xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context=".MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/bluetoothStatus"
android:text="Bluetooth"
android:textSize="40sp"
android:layout_marginTop="30dp"
android:layout_gravity="center" />

<Switch
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/bluetooth_switch"
android:gravity="center"
android:layout_marginRight="100dp"
android:layout_marginLeft="100dp"
android:layout_marginTop="100dp" />

</RelativeLayout>

Permission –

<uses-permission android:name="[Link]"/>
<uses-permission android:name="[Link].BLUETOOTH_ADMIN"/>
<uses-permission android:name="[Link].ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="[Link].BLUETOOTH_CONNECT" />
Java –
package [Link];

import [Link];

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

public class MainActivity extends AppCompatActivity {


TextView bluetoothStatus;
Switch aSwitch;
BluetoothAdapter bluetoothAdapter;
final static int i=1;

@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);

bluetoothStatus=findViewById([Link]);
aSwitch=findViewById([Link].bluetooth_switch);

bluetoothAdapter=[Link]();
[Link](new
[Link]() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean
isChecked) {
if(isChecked){
Intent intent=new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(intent, i);
[Link]("Bluetooth is ON");
}else{
[Link]();
[Link]("Bluetooth is OFF");
}

}
});

}
}

You might also like