当我的代码中禁用蓝牙时如何启用蓝牙?

use*_*897 1 android bluetooth

我提出了两项​​活动的申请。第一个活动导入用户参数,第二个活动通过蓝牙发送数据。如果蓝牙被禁用,我.ACTION_REQUEST_ENABLE会启用它,但是当 bt 关闭时,我的 apk 就会退出。这不起作用。有什么帮助吗?

我用这个;创建活动以及启动处理程序和可运行后的代码...我只测试它 findBT 并工作...

 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");
  }
Run Code Online (Sandbox Code Playgroud)

Nip*_*gia 5

这是代码:

 public class AndroidBluetooth extends Activity {

    private static final int REQUEST_ENABLE_BT = 1;

    /** Called when the activity is first created. */

    TextView stateBluetooth;
    BluetoothAdapter bluetoothAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        stateBluetooth = (TextView)findViewById(R.id.bluetoothstate);
        bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

        CheckBlueToothState();
    }

    private void CheckBlueToothState(){
        if (bluetoothAdapter == null){
            stateBluetooth.setText("Bluetooth NOT support");
        }else{
            if (bluetoothAdapter.isEnabled()){
                if(bluetoothAdapter.isDiscovering()){
                    stateBluetooth.setText("Bluetooth is currently in device discovery process.");
                }else{
                    stateBluetooth.setText("Bluetooth is Enabled.");
                }
            }else{
                stateBluetooth.setText("Bluetooth is NOT Enabled!");
                Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
            }
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        if(requestCode == REQUEST_ENABLE_BT){
            CheckBlueToothState();
        }

    }


}
Run Code Online (Sandbox Code Playgroud)