Skip to content

Instantly share code, notes, and snippets.

@NizarETH
Created November 21, 2022 20:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NizarETH/89d6f606be5bbd7a5307e311ee345b28 to your computer and use it in GitHub Desktop.
Save NizarETH/89d6f606be5bbd7a5307e311ee345b28 to your computer and use it in GitHub Desktop.
=========================================================================
Gradle
=========================================================================
implementation 'com.karumi:dexter:6.2.3'
=========================================================================
Manifest
=========================================================================
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE" />
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
<uses-permission android:name="android.permission.BLUETOOTH_SCAN" />
=========================================================================
Layout MainActivity
=========================================================================
<?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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView android:text=""
android:id="@+id/out"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="30dp"
android:layout_marginTop="49dp"
android:text="TURN_ON" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_below="@+id/button1"
android:layout_marginTop="27dp"
android:text="DISCOVERABLE" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button2"
android:layout_below="@+id/button2"
android:layout_marginTop="28dp"
android:text="TURN_OFF" />
</RelativeLayout>
=========================================================================
MainActivity
=========================================================================
import android.bluetooth.BluetoothAdapter
import android.bluetooth.BluetoothManager
import android.content.Context
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
private val REQUEST_ENABLE_BT = 0
private val REQUEST_DISCOVERABLE_BT = 0
@RequiresApi(Build.VERSION_CODES.S)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
var button1 = findViewById<View>(R.id.button1);
var button2 = findViewById<View>(R.id.button2);
var button3 = findViewById<View>(R.id.button3);
val bluetoothManager = getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager
var mBluetoothAdapter = bluetoothManager.adapter
button1.setOnClickListener(View.OnClickListener() {
if (!mBluetoothAdapter.isEnabled()) {
//Manifest.permission.BLUETOOTH_CONNECT
val enableBtIntent = Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE)
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT)
}
})
button2.setOnClickListener(View.OnClickListener() {
//Manifest.permission.BLUETOOTH_SCAN
if (!mBluetoothAdapter.isDiscovering()) {
Toast.makeText(
getApplicationContext(), "MAKING YOUR DEVICE DISCOVERABLE",
Toast.LENGTH_LONG
);
val enableBtIntent = Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
startActivityForResult(enableBtIntent, REQUEST_DISCOVERABLE_BT);
}
})
button3.setOnClickListener( View.OnClickListener() {
//Manifest.permission.BLUETOOTH_CONNECT
mBluetoothAdapter.disable();
Toast.makeText(getApplicationContext(), "TURNING_OFF BLUETOOTH", Toast.LENGTH_LONG);
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment