1
我試圖從4天開始運行一個小應用程序。我正在使用BluetoothChat的代碼來檢查BT是否啓用。如果沒有,它應該顯示啓用對話框。 我嘗試在HTC野火運行代碼的Android 2.2Android 2.2藍牙問題(採用BluetoothChat示例)
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();
}
}
}
凡在此代碼的錯誤? 感謝您的幫助! 基督教
謝謝你本!我的錯誤是我只寫了「BLUETOOTH」而不是「android.permission.BLUETOOTH」。 – 2011-04-24 13:53:14