【问题标题】:Toggling Bluetooth on and off?打开和关闭蓝牙?
【发布时间】:2011-08-09 18:06:18
【问题描述】:

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

如果有人能提供帮助,将不胜感激。

-提前致谢。

【问题讨论】:

    标签: android eclipse bluetooth togglebutton


    【解决方案1】:

    试试这个

    public void disableBluetooth(Context context, Boolean bool) {
        WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    
        if(bool)
            wifiManager.setWifiEnabled(false);
        else
            wifiManager.setWifiEnabled(true);
    }
    

    【讨论】:

    • 蓝牙和 Wi-Fi 是不同的东西。
    【解决方案2】:

    下载此示例,这将对您有所帮助

    https://github.com/siddhpuraamitr/Blutooth-Toggle-Widget

    【讨论】:

      【解决方案3】:
      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" />
      

      【讨论】:

        【解决方案4】:

        你需要

        <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.)
            }
          }
        }
        

        【讨论】:

        • @Ben Williams,这有点工作,问题是它不会改变蓝牙状态。这是我需要的唯一许可吗?此外,它仅在我取出 REQUEST_ENABLE_BT 和整个受保护的无效代码时才有效,否则我只会在“onActivityResult”和“REQUEST_ENABLE_BT”上出现错误
        • 这可能是在抱怨,因为我在使用它之前忘记声明 REQUEST_ENABLE_BT ——这只是意图的整数标签,所以坚持private final REQUEST_ENABLE_BT = 1 可以工作。此外,您需要 android.permission.BLUETOOTH_ADMIN。 (我会将其编辑到另一个中。)
        • @Ben Williams,我不想打扰,但它仍然无法打开或关闭蓝牙。我确实摆脱了错误,谢谢。
        • 如果你做boolean b = mBluetoothAdapter.enable(),得到回报,是真的吗?启用后,mBluetoothAdapter.getState() == BluetoothAdapter.STATE_TURNING_ON 是真的吗? enable 调用是异步的,所以它会立即返回,而蓝牙本身可能需要一段时间才能启动。
        • @Ben Williams,我不确定你的意思。
        【解决方案5】:

        看看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;
            } 
        }
        

        【讨论】:

        • 除了 还有什么我应该使用的权限吗?
        • @Christian &lt;uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/&gt; 可能需要。 @Abhilash 在onDestroy() 中禁用bluetooth 可以吗
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-03-28
        • 2011-01-26
        • 1970-01-01
        • 1970-01-01
        • 2012-03-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多