android 蓝牙激活错误

发布于 2024-09-24 08:02:02 字数 2553 浏览 4 评论 0原文

我试图在 android 中以编程方式激活蓝牙,并且应用程序安装正常,但是当我单击按钮激活 BT 时,它会出现异常。我无法处理异常。非常感谢任何帮助。我对此很陌生。这是代码:

package com.example.helloandroid2;


import java.io.IOException;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class HelloAndroid extends Activity {
 // Declare our Views, so we can access them later

 private Button activate_buletooth;
    static final int REQUEST_ENABLE_BT = 0;
    Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
    BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Set Activity Layout
        setContentView(R.layout.main);

         activate_buletooth = (Button)findViewById(R.id.activate_buletooth);

        // Set Click Listener

            activate_buletooth.setOnClickListener(new OnClickListener() {
         @Override
         public void onClick(View v){
          if (mBluetoothAdapter == null) {
              // Device does not support Bluetooth
           Context context = getApplicationContext();
           CharSequence text = "BT not suported";
           int duration = Toast.LENGTH_SHORT;

           Toast toast = Toast.makeText(context, text, duration);
           toast.show();
          }     
          if (!mBluetoothAdapter.isEnabled()) {
              Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
              startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
          }

          // startActivityForResult(discoverableIntent, REQUEST_ENABLE_BT);

         }
        });

    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
     if(requestCode == REQUEST_ENABLE_BT)
        {
      if(resultCode == RESULT_CANCELED)
            {
       Context context = getApplicationContext();
       CharSequence text = "bt not available";
       int duration = Toast.LENGTH_SHORT;

       Toast toast = Toast.makeText(context, text, duration);
       toast.show();
            }
       else
             {

             }
        }
    }
}

I am trying to activate bluetooth programatically in android and the application installs fine, but when I click the button to activate BT , it gives exception. I am not able to handle the exception. Any help is greatly appreciated. I am new to this. Here is the code :

package com.example.helloandroid2;


import java.io.IOException;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class HelloAndroid extends Activity {
 // Declare our Views, so we can access them later

 private Button activate_buletooth;
    static final int REQUEST_ENABLE_BT = 0;
    Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
    BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Set Activity Layout
        setContentView(R.layout.main);

         activate_buletooth = (Button)findViewById(R.id.activate_buletooth);

        // Set Click Listener

            activate_buletooth.setOnClickListener(new OnClickListener() {
         @Override
         public void onClick(View v){
          if (mBluetoothAdapter == null) {
              // Device does not support Bluetooth
           Context context = getApplicationContext();
           CharSequence text = "BT not suported";
           int duration = Toast.LENGTH_SHORT;

           Toast toast = Toast.makeText(context, text, duration);
           toast.show();
          }     
          if (!mBluetoothAdapter.isEnabled()) {
              Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
              startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
          }

          // startActivityForResult(discoverableIntent, REQUEST_ENABLE_BT);

         }
        });

    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
     if(requestCode == REQUEST_ENABLE_BT)
        {
      if(resultCode == RESULT_CANCELED)
            {
       Context context = getApplicationContext();
       CharSequence text = "bt not available";
       int duration = Toast.LENGTH_SHORT;

       Toast toast = Toast.makeText(context, text, duration);
       toast.show();
            }
       else
             {

             }
        }
    }
}

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

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

发布评论

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

评论(1

只有一腔孤勇 2024-10-01 08:02:02

这是由于默认适配器启用蓝牙需要时间。
我也遇到了同样的问题,我查了一下。

预计在此onClick() 事件之后“执行”的代码将在适配器打开蓝牙之前执行。
可以有一个解决方案,比如有一个循环,在蓝牙完全打开之前不允许进一步执行代码。或者
有一种 setDelay() 类型的方法可以提供帮助。

欢迎任何进一步的帮助。

This is due to the time taken to enable the bluetooth by default adapter.
I am having the same problem, and I traced it out.

The code which is expected to be "executed" after thisonClick() event gets executed before the adapter switches the bluetooth on.
There can be solution like having a loop which won't allow the further code exec until the bluetooth is completely turned on. Or
there can be a setDelay() kind of method which could help.

Any further help is welcomed.

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