如何启用蓝牙当它被禁止在我的code? [英] How to enable bluetooth when it is disable inside my code?

查看:167
本文介绍了如何启用蓝牙当它被禁止在我的code?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了两项活动的应用程序。第一项活动导入用户参数和次活动通过蓝牙发送数据。我用 .ACTION_REQUEST_ENABLE 启用蓝牙,如果它被禁止,但在BT被关闭我的APK退出​​。它不工作。任何帮助?

我用这一点;在创建活动,启动处理程序,并runnnable后code ...我测试它只是findBT和工作...

 无效findBT()
 {
  mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
  如果(mBluetoothAdapter == NULL)
  {
      myLabel.setText(没有蓝牙适配器可用);
  }

  如果(!mBluetoothAdapter.isEnabled())
  {
      //我的问题是有
      意图enableBluetooth =新的意图(BluetoothAdapter.ACTION_REQUEST_ENABLE);
      startActivityForResult(enableBluetooth,0);
  }

  设置< BluetoothDevice类> pairedDevices = mBluetoothAdapter.getBondedDevices();
  如果(pairedDevices.size()大于0)
  {
      对于(BluetoothDevice类设备:pairedDevices)
      {
          如果(device.getName()。等于(strValue2))
          {
              mmDevice =设备;
              打破;
          }
      }
  }
  myLabel.setText(蓝牙设备找到);
  }
 

解决方案

这code,检查设备是否支持蓝牙与否:

  BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
如果(mBluetoothAdapter == NULL){
Toast.makeText(this.getParent(),ConfigClass.BLUETOOTH_NOT_SUPPORTED_ERROR,Toast.LENGTH_LONG);
}
 

检查并启用蓝牙:

 否则,如果(!mBluetoothAdapter.isEnabled()){
意图enableBtIntent =新的意图(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent,RESULT_OK);
 

I made an application with two activities. The first activity imports user parameters and the second activity sends data via bluetooth. I use .ACTION_REQUEST_ENABLE to enable bluetooth if it is disabled but when bt is closed my apk exits. It doesn't work. Any help?

I use this; code on create activity and after start handler and runnnable... I test it only findBT and work...

 void findBT()
 {
  mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
  if(mBluetoothAdapter == null)
  {
      myLabel.setText("No bluetooth adapter available");
  }

  if(!mBluetoothAdapter.isEnabled())
  {
      //My problem is there
      Intent enableBluetooth = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
      startActivityForResult(enableBluetooth, 0);
  }

  Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
  if(pairedDevices.size() > 0)
  {
      for(BluetoothDevice device : pairedDevices)
      {
          if(device.getName().equals(strValue2))
          {
              mmDevice = device;
              break;
          }
      }
  }
  myLabel.setText("Bluetooth Device Found");
  }

解决方案

This code to check your device is BlueTooth enabled or not:

BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter == null) {
Toast.makeText(this.getParent(), ConfigClass.BLUETOOTH_NOT_SUPPORTED_ERROR, Toast.LENGTH_LONG);
}

Check and Enable your Bluetooth :

else if (!mBluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, RESULT_OK);

这篇关于如何启用蓝牙当它被禁止在我的code?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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