打开和关闭蓝牙?

发布于 2024-11-02 10:00:56 字数 107 浏览 2 评论 0原文

是否有一个简单的教程或者有人有代码可以使用 Android 的 Eclipse 构建中的切换按钮打开和关闭蓝牙?

如果有人可以提供帮助,我们将不胜感激。

-提前致谢。

Is there a simple tutorial or dose anyone have code to toggle Bluetooth on and off using a Toggle-button in eclipse building for android?

If anyone can help that will be greatly appreciated.

-Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

作业与我同在 2024-11-09 10:00:56

您需要

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

在清单文件中添加以下变量:

private final integer REQUEST_ENABLE_BT = 1;

以便

BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
boolean hasBluetooth = (mBluetoothAdapter == null);

在 OnCreate 中您可以执行以下操作:

final ToggleButton togglebutton = (ToggleButton) findViewById(R.id.togglebutton);
togglebutton.setOnClickListener(new OnClickListener()
{
  public void onClick(View v)
  {
    // Perform action on clicks
    if (togglebutton.isChecked())
    {
      if (hasBluetooth && !mBluetoothAdapter.isEnabled())
      {
        // prompt the user to turn BlueTooth on
        Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
      }
    }
    else
    {
      if (hasBluetooth && mBluetoothAdapter.isEnabled())
      {
        // you should really prompt the user for permission to turn
        // the BlueTooth off as well, e.g., with a Dialog
        boolean isDisabling = mBluetoothAdapter.disable();
        if (!isDisabling)
        {
           // an immediate error occurred - perhaps the bluetooth is already off?
        }
      }
    }
  }
});

用户对“打开蓝牙”提示的响应被捕获在

protected void onActivityResult (int requestCode, int resultCode, Intent data)
{
  if ((requestCode == REQUEST_ENABLE_BT) && (resultCode == RESULT_OK))
  {
    boolean isEnabling = mBluetoothAdapter.enable();
    if (!isEnabling)
    {
      // an immediate error occurred - perhaps the bluetooth is already on?
    }
    else if (mBluetoothAdapter.getState() == BluetoothAdapter.STATE_TURNING_ON)
    {
      // the system, in the background, is trying to turn the Bluetooth on
      // while your activity carries on going without waiting for it to finish;
      // of course, you could listen for it to finish yourself - eg, using a
      // ProgressDialog that checked mBluetoothAdapter.getState() every x
      // milliseconds and reported when it became STATE_ON (or STATE_OFF, if the
      // system failed to start the Bluetooth.)
    }
  }
}

You'll need

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

in your manifest file, and variables like:

private final integer REQUEST_ENABLE_BT = 1;

and

BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
boolean hasBluetooth = (mBluetoothAdapter == null);

so that in your OnCreate you can do something like:

final ToggleButton togglebutton = (ToggleButton) findViewById(R.id.togglebutton);
togglebutton.setOnClickListener(new OnClickListener()
{
  public void onClick(View v)
  {
    // Perform action on clicks
    if (togglebutton.isChecked())
    {
      if (hasBluetooth && !mBluetoothAdapter.isEnabled())
      {
        // prompt the user to turn BlueTooth on
        Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
      }
    }
    else
    {
      if (hasBluetooth && mBluetoothAdapter.isEnabled())
      {
        // you should really prompt the user for permission to turn
        // the BlueTooth off as well, e.g., with a Dialog
        boolean isDisabling = mBluetoothAdapter.disable();
        if (!isDisabling)
        {
           // an immediate error occurred - perhaps the bluetooth is already off?
        }
      }
    }
  }
});

where the user response to the "turn bluetooth on" prompt is caught in

protected void onActivityResult (int requestCode, int resultCode, Intent data)
{
  if ((requestCode == REQUEST_ENABLE_BT) && (resultCode == RESULT_OK))
  {
    boolean isEnabling = mBluetoothAdapter.enable();
    if (!isEnabling)
    {
      // an immediate error occurred - perhaps the bluetooth is already on?
    }
    else if (mBluetoothAdapter.getState() == BluetoothAdapter.STATE_TURNING_ON)
    {
      // the system, in the background, is trying to turn the Bluetooth on
      // while your activity carries on going without waiting for it to finish;
      // of course, you could listen for it to finish yourself - eg, using a
      // ProgressDialog that checked mBluetoothAdapter.getState() every x
      // milliseconds and reported when it became STATE_ON (or STATE_OFF, if the
      // system failed to start the Bluetooth.)
    }
  }
}
南薇 2024-11-09 10:00:56

看看 http://developer.android.com/reference/android/bluetooth /蓝牙适配器.html

BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); 
if(adapter != null) {
    if(adapter.getState() == BluetoothAdapter.STATE_ON) {
        adapter.disable();
    } else if (adapter.getState() == BluetoothAdapter.STATE_OFF){
        adapter.enable();
    } else {
        //State.INTERMEDIATE_STATE;
    } 
}

Take a look at http://developer.android.com/reference/android/bluetooth/BluetoothAdapter.html

BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); 
if(adapter != null) {
    if(adapter.getState() == BluetoothAdapter.STATE_ON) {
        adapter.disable();
    } else if (adapter.getState() == BluetoothAdapter.STATE_OFF){
        adapter.enable();
    } else {
        //State.INTERMEDIATE_STATE;
    } 
}
幻想少年梦 2024-11-09 10:00:56

试试这个

public void disableBluetooth(Context context, Boolean bool) {
    WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);

    if(bool)
        wifiManager.setWifiEnabled(false);
    else
        wifiManager.setWifiEnabled(true);
}

try this

public void disableBluetooth(Context context, Boolean bool) {
    WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);

    if(bool)
        wifiManager.setWifiEnabled(false);
    else
        wifiManager.setWifiEnabled(true);
}
缺⑴份安定 2024-11-09 10:00:56
BluetoothAdapter mBluetooth = BluetoothAdapter.getDefaultAdapter();
Integer bluetooth = 1; // Turn on
Object nada = (bluetooth == 1 ? mBluetooth.enable() : mBluetooth.disable());

显现:

<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
BluetoothAdapter mBluetooth = BluetoothAdapter.getDefaultAdapter();
Integer bluetooth = 1; // Turn on
Object nada = (bluetooth == 1 ? mBluetooth.enable() : mBluetooth.disable());

Manifest:

<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
喵星人汪星人 2024-11-09 10:00:56

如果您在 Android 33 中遇到问题,请阅读我,

我今天花了很多时间来解决这个问题,因为这是谷歌搜索的顶部,这里是如何创建一个按钮,要求在 Android 33 中打开/关闭蓝牙。答案是在科特林中
添加一个名为 android:id="@+id/requestBlueToothButton" 的按钮

AndroidManifest.xml 将需要以下行

<uses-permission
        android:name="android.permission.BLUETOOTH"/>
<uses-permission
        android:name="android.permission.BLUETOOTH_ADMIN"/>
requestBlueToothButton.setOnClickListener {
                val bluetoothManager: BluetoothManager? = getSystemService(requireContext(), BluetoothManager::class.java)
                val bluetoothAdapter: BluetoothAdapter? = bluetoothManager?.getAdapter()
                if (bluetoothAdapter == null) {
                    // Device doesn't support Bluetooth
                }
                if (bluetoothAdapter?.isEnabled == false) {
                    val enableBtIntent = Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE)
                    startActivity(enableBtIntent)
                }
            }

来源

IF YOU ARE HAVING TROUBLE IN ANDROID 33 READ ME

I spent way to many hours today figuring this out, since this is the top google search here is how to create a button that will ask to turn Bluetooth on/off in android 33. Answer is in Kotlin
add a button to the fragment with the name android:id="@+id/requestBlueToothButton"

AndroidManifest.xml will need the following lines

<uses-permission
        android:name="android.permission.BLUETOOTH"/>
<uses-permission
        android:name="android.permission.BLUETOOTH_ADMIN"/>
requestBlueToothButton.setOnClickListener {
                val bluetoothManager: BluetoothManager? = getSystemService(requireContext(), BluetoothManager::class.java)
                val bluetoothAdapter: BluetoothAdapter? = bluetoothManager?.getAdapter()
                if (bluetoothAdapter == null) {
                    // Device doesn't support Bluetooth
                }
                if (bluetoothAdapter?.isEnabled == false) {
                    val enableBtIntent = Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE)
                    startActivity(enableBtIntent)
                }
            }

Source

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文