Включение и выключение Bluetooth? - PullRequest
2 голосов
/ 20 апреля 2011

Есть ли простой урок или доза, у кого-нибудь есть код для включения и выключения Bluetooth с помощью кнопки-переключателя в Eclipse Building для Android?

Если кто-то может помочь, это будет оценено.

-Спасибо заранее.

Ответы [ 5 ]

4 голосов
/ 20 апреля 2011

Вам понадобится

<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?
        }
      }
    }
  }
});

, где пользовательский ответ на приглашение «включить Bluetooth» перехватывается в

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.)
    }
  }
}
3 голосов
/ 20 апреля 2011

Взгляните на 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;
    } 
}
1 голос
/ 06 августа 2015

попробуйте

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

    if(bool)
        wifiManager.setWifiEnabled(false);
    else
        wifiManager.setWifiEnabled(true);
}
0 голосов
/ 27 ноября 2013

скачайте этот пример, это вам поможет

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

0 голосов
/ 15 декабря 2011
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" />
...