Bluetooth
Bluetooth
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bluetooth On"
android:id="@+id/turnOn">
</Button>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Discoverable"
android:id="@+id/discover">
</Button>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bluetooth Off"
android:id="@+id/turnOff">
</Button>
</LinearLayout>
package com.example.myapplication;
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
BY VIKAS PATIL
Button turnOn, turnOff, discover ;
int REQUEST_ENABLE_BT = 0;
int REQUEST_DISCOVER_BT = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
turnOff=findViewById(R.id.turnOff);
turnOn=findViewById(R.id.turnOn);
discover=findViewById(R.id.discover);
BluetoothAdapter mBluetooth=BluetoothAdapter.getDefaultAdapter();
turnOn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(!mBluetooth.isEnabled()) {
startActivityForResult(new
Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE), REQUEST_ENABLE_BT);
Toast.makeText(MainActivity.this, "Bluetooth Turned On",
Toast.LENGTH_SHORT).show();
}
}
});
discover.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(!mBluetooth.isDiscovering()) {
startActivityForResult(new
Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE),
REQUEST_DISCOVER_BT);
Toast.makeText(MainActivity.this, "Device Discoverable",
Toast.LENGTH_SHORT).show();
}
}
});
turnOff.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(mBluetooth.isEnabled()) {
mBluetooth.disable();
Toast.makeText(MainActivity.this, "Bluetooth Turned Off",
Toast.LENGTH_SHORT).show();
}
}
});
}
}
BY VIKAS PATIL