问题描述
我想检查使用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); }
问题描述
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); }