使用安卓应用程序检查蓝牙是否已启用[英] Check if bluetooth is enabled using an Android application

本文是小编为大家收集整理的关于使用安卓应用程序检查蓝牙是否已启用的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我想检查使用Android应用程序是否在设备中启用了蓝牙. 我使用了.cerenabled方法.但有一个错误.我发现(通过评论行)错误是以.启用方法.你可以帮我解决这个问题吗?

final BluetoothAdapter bluetooth = BluetoothAdapter.getDefaultAdapter();

submitButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        String status = "Bluetooth";

        if(bluetooth != null) {
            if (bluetooth.isEnabled()) {
                String mydeviceaddress = bluetooth.getAddress();
                String mydevicename = bluetooth.getName();
                status = ("Address "+ mydeviceaddress + " Name" + mydevicename);
                Toast.makeText(getApplicationContext(), "" + status + "", Toast.LENGTH_LONG).show();
            } else {
                status = ("Bluetooth not enabled");
                Toast.makeText(getApplicationContext(), "" + status + "", Toast.LENGTH_LONG).show();
            }
        } else {
            Toast.makeText(getApplicationContext(), "" + status + "", Toast.LENGTH_LONG).show();
        }
    }
}

推荐答案

这对我来说最适用:

/**
 * Check for Bluetooth.
 *
 * @return true if Bluetooth is available.
 */
public boolean isBluetoothAvailable() {
    final BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

    return bluetoothAdapter != null 
        && bluetoothAdapter.isEnabled() 
        && bluetoothAdapter.getState() == BluetoothAdapter.STATE_ON;
}

其他推荐答案

试试这个.

BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (bluetoothAdapter == null) {
    // Device does not support Bluetooth
} else {
    if (!bluetoothAdapter.isEnabled()) {
        // Bluetooth is not enabled
    }
}

在您的AndroidManifest.xml File添加

<uses-permission android:name="android.permission.BLUETOOTH" />

其他推荐答案

jared burrows答案似乎是正确的,但我必须在开始工作之前添加一个添加.我不得不检查蓝牙状态.

public static boolean isBluetoothAvailable() {
    final BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

    return (bluetoothAdapter != null &&
            bluetoothAdapter.isEnabled() &&
            bluetoothAdapter.getState() == BluetoothAdapter.STATE_ON);
}

本文地址:https://www.itbaoku.cn/post/102629.html

问题描述

I want to check if bluetooth is enabled in a device using an Android application. I used the .isEnabled method. But there is an error. I found out (by commenting lines) that the error is in .isEnabled method. Can you pls help me to figure this out?

final BluetoothAdapter bluetooth = BluetoothAdapter.getDefaultAdapter();

submitButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        String status = "Bluetooth";

        if(bluetooth != null) {
            if (bluetooth.isEnabled()) {
                String mydeviceaddress = bluetooth.getAddress();
                String mydevicename = bluetooth.getName();
                status = ("Address "+ mydeviceaddress + " Name" + mydevicename);
                Toast.makeText(getApplicationContext(), "" + status + "", Toast.LENGTH_LONG).show();
            } else {
                status = ("Bluetooth not enabled");
                Toast.makeText(getApplicationContext(), "" + status + "", Toast.LENGTH_LONG).show();
            }
        } else {
            Toast.makeText(getApplicationContext(), "" + status + "", Toast.LENGTH_LONG).show();
        }
    }
}

推荐答案

This has worked best for me:

/**
 * Check for Bluetooth.
 *
 * @return true if Bluetooth is available.
 */
public boolean isBluetoothAvailable() {
    final BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

    return bluetoothAdapter != null 
        && bluetoothAdapter.isEnabled() 
        && bluetoothAdapter.getState() == BluetoothAdapter.STATE_ON;
}

其他推荐答案

Try this.

BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (bluetoothAdapter == null) {
    // Device does not support Bluetooth
} else {
    if (!bluetoothAdapter.isEnabled()) {
        // Bluetooth is not enabled
    }
}

in your AndroidManifest.xml File add

<uses-permission android:name="android.permission.BLUETOOTH" />

其他推荐答案

Jared Burrows answer seems like the correct one, however I had to add one addition before it started working. I had to check the Bluetooth state.

public static boolean isBluetoothAvailable() {
    final BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

    return (bluetoothAdapter != null &&
            bluetoothAdapter.isEnabled() &&
            bluetoothAdapter.getState() == BluetoothAdapter.STATE_ON);
}