Android Bluetooth On Off Visible
Hello there. In this article, we will briefly explain how to turn on, turn off, and make bluetooth on android applications.
Set the look of the progeny that you have created as follows. I put 3 buttons as simple as simple.
- File:res/layout/activity_main.xml
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.taha.bluetoothexample.MainActivity" android:orientation="vertical" android:gravity="center"> <Button android:text="Open" android:onClick="openBtn" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:text="Close" android:onClick="closeBtn" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:text="Visible" android:onClick="visibleBtn" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> |
Now we need to define 2 permissions in our AndroidManifest file. You can either define them by writing them in Permission or by typing them manually in the xml section.
2 3 |
<uses-permission android:name="android.permission.BLUETOOTH" /> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /> |
We add in this way:
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.taha.bluetoothexample"> <uses-permission android:name="android.permission.BLUETOOTH" /> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> |
MainActivity
- File:MainActivity.class
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
public class MainActivity extends AppCompatActivity { public BluetoothAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); adapter=BluetoothAdapter.getDefaultAdapter(); } public void openBtn(View view) { if(adapter==null){ Toast.makeText(getApplicationContext(),"No find Bluetooth devices",Toast.LENGTH_SHORT).show(); } else{ if(!adapter.isEnabled()){ Intent intent= new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(intent,1); } else{ adapter.disable(); } } } public void closeBtn(View view) { if(!adapter.isEnabled()) { }else{ adapter.disable(); } } public void visibleBtn(View view) { if(adapter.isEnabled()) { Intent intent=new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE); startActivityForResult(intent,1); } } } |
- We wrote the codes of our buttons for bluetooth on off as above.
Demo
- Open Button Action
- Allow Button Action
- Close Button Action
if you have question do not forget to write from the chat button next to it or from the comment
Recent Comments