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");
}
}
});
}
}