开发者

Bluetooth startActivity method for Bluetooth Intent

开发者 https://www.devze.com 2023-03-25 19:06 出处:网络
Eclipse keeps wanting me to define a startActivity method, but I have no idea what to place into that method. I already have bluetooth enabled in my androidmanifest.xml.

Eclipse keeps wanting me to define a startActivity method, but I have no idea what to place into that method. I already have bluetooth enabled in my androidmanifest.xml.

I have provided an excerpt from my code below:

public int enable() {
    if ( isSup开发者_C百科ported() ==  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);
}

`


Try deleting the overridden method, and compile/running. There is no reason you should have to override the startActivity method. If it does not run, post the error. Also - does your class extend some sort of base Activity class? Such as ListActivity or something?

EDIT: Oh I see a potential problem. Your class does NOT extend an activity, so you don't have access to the helper method startActivity(). You need to pass your main Activities context in through your BTConnection's constructor, and then call context.startActivity(...).

Example:

BluetoothAdapter m_adapter;
Context context;

/**
 * Default constructor
 */
public BTConnection(Context c) {
    m_adapter = BluetoothAdapter.getDefaultAdapter();
    context = c;
}

And in your enable() method:

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

精彩评论

暂无评论...
验证码 换一张
取 消