Android 2.2蓝牙问题(采用蓝牙样本)

时间:2011-04-24 13:35:05

标签: android bluetooth

我正在尝试4天才能运行一个小应用程序。我正在使用BluetoothChat的代码来检查BT是否启用。如果不是,则应显示启用对话框。 我尝试在Android 2.2的HTC Wildfire上运行该代码

package com.example.testagain;

import android.app.Activity;
import android.os.Bundle;
import android.bluetooth.BluetoothAdapter;
import android.util.Log;
import android.widget.Toast;
import android.content.Intent;


public class testit extends Activity {
    /** Called when the activity is first created. */
    private static final String TAG = "BluetoothChat";
    private static final boolean D = true;

    private BluetoothAdapter mBluetoothAdapter = null;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if(D) Log.e(TAG, "+++ ON CREATE +++");

        // Set up the window layout
        setContentView(R.layout.main);

        // 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, 1);
        // Otherwise, setup the chat session
        } else {
            Toast.makeText(this, "done!", Toast.LENGTH_LONG).show();
        }
    }

}

此代码中的错误在哪里? 谢谢你的帮助! 基督教

1 个答案:

答案 0 :(得分:1)

添加

<uses-permission android:name="android.permission.BLUETOOTH" />

到您的AndroidManifest文件。