如何以编程方式启用或者禁用Android中的蓝牙

时间:2020-02-23 14:29:27  来源:igfitidea点击:

在本教程中,我们将看到如何以编程方式在Android中启用或者禁用蓝牙。

从Android代码启用或者禁用蓝牙非常简单。

用于启用蓝牙:

BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
adapter.enable();

用于禁用蓝牙:

BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
adapter.disable();

因此,我们可以使用BluetoothAdapter类启用或者禁用蓝牙。

我们需要在Androidmanifest.xml中添加另外的权限以启用或者禁用蓝牙。

步骤1 :

创建一个Android应用程序ProjectNamed"EnabledisableBluetoothApph"。

第2步:

在AndoridManifest.xml中添加以下权限。

<uses-permission android:name="android.permission.BLUETOOTH"
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" Go to app -> src -> main -> AndroidManifest.xml and add above permissions. AndroidManifest.xml will look like below xml.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
 package="com.theitroad.enabledisablebluetoothapp">
 <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: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>

第3步:

更改RES - >布局 - > Activity_main.xml如下所示:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:layout_margin="16dp"
 android:gravity="center"
 android:orientation="vertical">
 
 <ToggleButton
 android:id="@+id/toggleButton"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:checked="false"
 
 
 <TextView
 android:id="@+id/textView"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_marginTop="16dp"
 
 
</LinearLayout>

第四步:

更改src/main/packageName/mainActivity.java如下:

package com.theitroad.enabledisablebluetoothapp;
 
import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.ToggleButton;
 
import com.theitroad.enabledisablebluetoothapp.R;
 
public class MainActivity extends AppCompatActivity {
 
    ToggleButton toggleButton;
    TextView textView;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        //Getting toggle button and textView from activity_main
        toggleButton = (ToggleButton) findViewById(R.id.toggleButton);
        textView = (TextView) findViewById(R.id.textView);
 
        //Put listener on toggle button
        toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
                if (checked) {
                    textView.setText("Bluetooth is ON");
                    BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
                    adapter.enable();
                } else {
                    textView.setText("Bluetooth is OFF");
                    BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
                    adapter.disable();
                }
            }
        });
        //For initial setting
        if (toggleButton.isChecked()) {
            textView.setText("Bluetooth is ON");
            BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
            adapter.enable();
        } else {
            textView.setText("Bluetooth is OFF");
            BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
            adapter.disable();
        }
    }
}

第5步:

运行应用程序