试验蓝牙代码...问题

发布于 2024-11-24 19:37:27 字数 4158 浏览 0 评论 0原文

使用运行cyanogenmod 7.03的HTC Desire来调试一些新的android蓝牙代码。 我使用BluetoothChat项目最初只是使用一个按钮来打开/关闭蓝牙...

每次我添加一些影响BluetoothAdapter的内容时,应用程序都会关闭

例如mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); if (!mBluetoothAdapter.isEnabled()) 导致我的应用程序关闭

这是我的完整代码 JocActivity.java - 我的主要也是唯一的 java 文件: 其中很多内容都被注释掉了,因为我一直在尝试不同的事情,但它的基本设置是这样的:当按下按钮时,会查询蓝牙适配器。 我正在尝试让 Logcat 工作,但它还没有告诉我太多:( 有什么建议吗? 我想我的 SDK 可能兼容 2.2,但我的手机是 2.3.3 我正在下载 2.3.3,但速度很慢wwwwww :(

package test.joc;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class JocActivity extends Activity implements OnClickListener{
    /** Called when the activity is first created. */

    // Debugging
    private static final String TAG = "joc";
    private static final boolean D = true;

    // Intent request codes
    private static final int REQUEST_CONNECT_DEVICE_SECURE = 1;
    private static final int REQUEST_CONNECT_DEVICE_INSECURE = 2;
    private static final int REQUEST_ENABLE_BT = 3;

    // Local Bluetooth adapter
    private BluetoothAdapter mBluetoothAdapter = null;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button button = (Button)findViewById(R.id.button1);
        button.setOnClickListener(this);

        // Get local Bluetooth adapter
        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

        // If the adapter is null, then Bluetooth is not supported
        if (mBluetoothAdapter == null) {
            Toast.makeText(this, "Bluetooth is not available", Toast.LENGTH_LONG).show();
            finish();
            return;
        }
    }

   /* @Override
    public void onStart() {
        super.onStart();
        if(D) Log.e(TAG, "++ ON START ++");

        // If BT is not on, request that it be enabled.
        // setupChat() will then be called during onActivityResult
        if (!mBluetoothAdapter.isEnabled()) {
            Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
        // Otherwise, setup the chat session
        } else {
            //if (mChatService == null) setupChat();
            Context context = getApplicationContext();
            CharSequence text = "Bluetooth enabled!";
            int duration = Toast.LENGTH_SHORT;
            Toast.makeText(context, text, duration).show();
        }
    }*/


 // Implement the OnClickListener callback
    public void onClick(View v) {
      // do something when the button is clicked
        Context context = getApplicationContext();
        int duration = Toast.LENGTH_SHORT;
        if(D) Log.e(TAG, "++ ON START ++");

        //BluetoothAdapter.enable();
        //Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        //InstanceOfAnActivity.startActivity(enableIntent);
        /*Intent btIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        btIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(btIntent);*/

        // If BT is not on, request that it be enabled.
        // setupChat() will then be called during onActivityResult
        // Get local Bluetooth adapter
        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        if (!mBluetoothAdapter.isEnabled()) {/*
            Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
            Toast.makeText(context, "Bluetooth Enabled", duration).show();
        // Otherwise, setup the chat session
        } else {
            //if (mChatService == null) setupChat();*/
            Toast.makeText(context, "Ready", duration).show();
        }   
    }
}

Using a HTC Desire running cyanogenmod 7.03 to debug some new android bluetooth code.
Im using the BluetoothChat project to just use a button to turn on/off Bluetooth initially...

Everytime i add something that affects the BluetoothAdapter the application closes

EgmBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (!mBluetoothAdapter.isEnabled())

Causes my application to close

Here's my code in full from JocActivity.java - my main and only java file:
A lot of this is commented out, as i have been trying different things, but its basically set up so that when the button is pressed, the the Bluetoothadapter is queried.
im trying to get Logcat working but its not telling me much yet :(
Any tips?
Im thinking it might bew that my SDK is 2.2 compliant, but my phone is 2.3.3
Im downloading 2.3.3 but its slowwwwww here :(

package test.joc;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class JocActivity extends Activity implements OnClickListener{
    /** Called when the activity is first created. */

    // Debugging
    private static final String TAG = "joc";
    private static final boolean D = true;

    // Intent request codes
    private static final int REQUEST_CONNECT_DEVICE_SECURE = 1;
    private static final int REQUEST_CONNECT_DEVICE_INSECURE = 2;
    private static final int REQUEST_ENABLE_BT = 3;

    // Local Bluetooth adapter
    private BluetoothAdapter mBluetoothAdapter = null;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button button = (Button)findViewById(R.id.button1);
        button.setOnClickListener(this);

        // Get local Bluetooth adapter
        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

        // If the adapter is null, then Bluetooth is not supported
        if (mBluetoothAdapter == null) {
            Toast.makeText(this, "Bluetooth is not available", Toast.LENGTH_LONG).show();
            finish();
            return;
        }
    }

   /* @Override
    public void onStart() {
        super.onStart();
        if(D) Log.e(TAG, "++ ON START ++");

        // If BT is not on, request that it be enabled.
        // setupChat() will then be called during onActivityResult
        if (!mBluetoothAdapter.isEnabled()) {
            Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
        // Otherwise, setup the chat session
        } else {
            //if (mChatService == null) setupChat();
            Context context = getApplicationContext();
            CharSequence text = "Bluetooth enabled!";
            int duration = Toast.LENGTH_SHORT;
            Toast.makeText(context, text, duration).show();
        }
    }*/


 // Implement the OnClickListener callback
    public void onClick(View v) {
      // do something when the button is clicked
        Context context = getApplicationContext();
        int duration = Toast.LENGTH_SHORT;
        if(D) Log.e(TAG, "++ ON START ++");

        //BluetoothAdapter.enable();
        //Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        //InstanceOfAnActivity.startActivity(enableIntent);
        /*Intent btIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        btIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(btIntent);*/

        // If BT is not on, request that it be enabled.
        // setupChat() will then be called during onActivityResult
        // Get local Bluetooth adapter
        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        if (!mBluetoothAdapter.isEnabled()) {/*
            Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
            Toast.makeText(context, "Bluetooth Enabled", duration).show();
        // Otherwise, setup the chat session
        } else {
            //if (mChatService == null) setupChat();*/
            Toast.makeText(context, "Ready", duration).show();
        }   
    }
}

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

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

发布评论

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

评论(1

怕倦 2024-12-01 19:37:27

您可能需要将蓝牙权限添加到您的AndroidManifest.xml中。但是请检查 logcat/ddms 中的堆栈跟踪。

You probably need to add bluetooth permission to your AndroidManifest.xml. But please, check your stacktrace in logcat/ddms.

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