检查蓝牙是使用的是Android应用程序中启用 [英] Check if bluetooth is enabled using an Android application

查看:216
本文介绍了检查蓝牙是使用的是Android应用程序中启用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要检查,如果蓝牙在使用Android应用程序的设备功能。 我用.isEnabled方法。但有一个错误。我发现(通过注释线)的错误是在.isEnabled方法。你能请帮我算出这个?

 最后BluetoothAdapter蓝牙= BluetoothAdapter.getDefaultAdapter();

submitButton.setOnClickListener(新View.OnClickListener(){
    @覆盖
    公共无效的onClick(视图v){
        字符串状态=蓝牙;

        如果(蓝牙!= NULL){
            如果(bluetooth.isEnabled()){
                串mydeviceaddress = bluetooth.getAddress();
                串mydevicename = bluetooth.getName();
                状态=(地址+ mydeviceaddress +姓名+ mydevicename);
                Toast.makeText(getApplicationContext(),+状态+,Toast.LENGTH_LONG).show();
            } 其他 {
                状态=(蓝牙未启用);
                Toast.makeText(getApplicationContext(),+状态+,Toast.LENGTH_LONG).show();
            }
        } 其他 {
            Toast.makeText(getApplicationContext(),+状态+,Toast.LENGTH_LONG).show();
        }
    }
}
 

解决方案

这是为我工作最好的:

  / **
 *检查蓝牙。
 * @返回真,如果蓝牙可用。
 * /
公共静态布尔isBluetoothAvailable(){
    最终BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    返程(bluetoothAdapter = NULL和放大器;!&安培; bluetoothAdapter());
}
 

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 static boolean isBluetoothAvailable() {
    final BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    return (bluetoothAdapter != null && bluetoothAdapter());
}

这篇关于检查蓝牙是使用的是Android应用程序中启用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆