Eclipse一直希望我定义一个startActivity方法,但我不知道该放入该方法的内容。我已经在androidmanifest.xml中启用了蓝牙。
我在下面的代码中提供了一段摘录:
public int enable() {
if ( isSupported() == false)
return BT_NOT_AVAILABLE;
if ( isEnabled() == true)
return Activity.RESULT_OK;
//start
if ( isEnabled() == false)
{
Intent intentBluetooth = new Intent();
intentBluetooth.setAction(android.provider.Settings.ACTION_BLUETOOTH_SETTINGS);
startActivity(intentBluetooth);
//Intent enableBTIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
//startActivityForResult(enableBTIntent, 1);
}
public class BTConnection {
// Constants
public final static int BT_NOT_AVAILABLE = 0; // BT is not available on device.
public final static int BT_NOT_ENABLED = 1; // BT is not enabled on device.
BluetoothAdapter m_adapter;
/**
* Default constructor
*/
public BTConnection() {
m_adapter = BluetoothAdapter.getDefaultAdapter();
}
/**
* Returns whether Bluetooth is supported on this device or not.
* @return - True if BT is supported, false otherwise.
*/
public boolean isSupported() {
if ( m_adapter == null)
return false;
return true;
}
/**
* Returns whether Bluetooth is enabled on the device or not.
* @return - True if BT is enabled, false otherwise.
*/
public boolean isEnabled() {
if ( isSupported() == false)
return false;
return m_adapter.isEnabled();
}
/**
* Enabled Bluetooth on the device if not already enabled.
* Does nothing is BT is not available on the device or is
* already enabled. This does not prompt user - the proper
* way to enable is not to use this method, but instead use
* intents from your activity to enable BT.
* See http://developer.android.com/guide/topics/wireless/bluetooth.html
* @return - Activity.RESULT_OK if BT is enabled,
* Activity.RESULT_CANCELED if user canceled the operation.
* BT_NOT_AVAILABLE if BT is not available on device.
*/
// Jerrell Jones
public int enable() {
if ( isSupported() == false)
return BT_NOT_AVAILABLE;
if ( isEnabled() == true)
return Activity.RESULT_OK;
//start
if ( isEnabled() == false)
{
Intent intentBluetooth = new Intent();
intentBluetooth.setAction(android.provider.Settings.ACTION_BLUETOOTH_SETTINGS);
startActivity(intentBluetooth);
//Intent enableBTIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
//startActivityForResult(enableBTIntent, 1);
}
//stop
if ( m_adapter.enable() )
return Activity.RESULT_OK;
return Activity.RESULT_CANCELED;
}
//private void startActivityForResult(Intent enableBTIntent, int i) {
// TODO Auto-generated method stub
//}
/**
* Gets already paired Bluetooth devices.
* @return Set of Bluetooth devices.
*/
public Set<BluetoothDevice> getPairedDevices() {
Set<BluetoothDevice> pairedDevices = m_adapter.getBondedDevices();
return pairedDevices;
}
/**
* Gets the bluetooth device using the address.
* @param address - Address of BT device.
* @return - BT device object if connected, null otherwise.
*/
public BluetoothDevice getDevice(String address) {
if ( isSupported() == false)
return null;
if ( isEnabled() == false)
return null;
//place intent action here
BluetoothDevice device = m_adapter.getRemoteDevice(address);
return device;
}
private void doDeviceSelection() {
// Enable BT if not already done
BTConnection bt = new BTConnection(context);
if (bt.isSupported() == false) {
setResult(Activity.RESULT_CANCELED, null);
finish();
return;
}
if (bt.isEnabled() == false) {
/*Intent enableBtIntent = new Intent(
BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT_READ);
*/
/*Intent intentBluetooth = new Intent();
intentBluetooth.setAction(android.provider.Settings.ACTION_BLUETOOTH_SETTINGS);
context.startActivity(intentBluetooth);
return;
*/
}
selectDevice(REQUEST_CONNECT_DEVICE_READ);
}
`
答案 0 :(得分:2)
尝试删除重写方法,然后编译/运行。没有理由你必须覆盖startActivity方法。如果它没有运行,请发布错误。另外 - 你的类是否扩展了某种基类Activity类?比如ListActivity还是什么?
编辑:哦,我发现了一个潜在的问题。您的类不会扩展活动,因此您无权访问辅助方法startActivity()。您需要通过BTConnection的构造函数传递主活动上下文,然后调用context.startActivity(...)。示例:
BluetoothAdapter m_adapter;
Context context;
/**
* Default constructor
*/
public BTConnection(Context c) {
m_adapter = BluetoothAdapter.getDefaultAdapter();
context = c;
}
在你的enable()方法中:
if ( isEnabled() == false)
{
Intent intentBluetooth = new Intent();
intentBluetooth.setAction(android.provider.Settings.ACTION_BLUETOOTH_SETTINGS);
context.startActivity(intentBluetooth);
//Intent enableBTIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
//startActivityForResult(enableBTIntent, 1);
}