gpt4 book ai didi

android - 尝试使用蓝牙代码...问题

转载 作者:行者123 更新时间:2023-11-30 04:36:28 25 4
gpt4 key购买 nike

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

每次我添加影响 BluetoothAdapter 的东西时,应用程序都会关闭

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

这是我的完整代码,来自 JocActivity.java - 我的主要且唯一的 java 文件:很多内容都被注释掉了,因为我一直在尝试不同的东西,但它基本上是这样设置的,当按下按钮时,就会查询蓝牙适配器。我正在尝试让 Logcat 工作,但它还没有告诉我太多信息:(有小费吗?我认为我的 SDK 可能兼容 2.2,但我的手机是 2.3.3我正在下载 2.3.3,但这里的速度很慢 :(

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

最佳答案

您可能需要添加 bluetooth permission到你的 AndroidManifest.xml。但是请在 logcat/ddms 中检查堆栈跟踪。

关于android - 尝试使用蓝牙代码...问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6755095/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com