如何在整个应用程序中启用蓝牙功能

时间:2017-02-06 22:59:13

标签: android bluetooth

我试图在整个应用程序中要求启用蓝牙,但我该怎么做呢?

当我只使用主要活动时,代码效果很好。但是,当我去另一个活动然后禁用BT广播发送信号以启用BT但在那之后" onActivityResult"方法永远不会被称为。

如何编写需要BT的全局?对于我的应用程序中的所有活动(和首选项)。

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    switch (requestCode)
    {
        case Bluetooth.REQUEST_ENABLE_BLUETOOTH:
            if (resultCode == RESULT_OK)
            {
                // some stuff here
            }
            else
            {
                // exitapp();
            }
            break;
    }
}

接收器

protected final BroadcastReceiver mReceiver = new BroadcastReceiver()
    {
        @Override
        public void onReceive(Context context, Intent intent)
        {
            if (intent.getAction().equals(BluetoothAdapter.ACTION_STATE_CHANGED))
            {
                final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE,
                        BluetoothAdapter.ERROR);

                if(state == BluetoothAdapter.STATE_OFF)
                {
                    startActivityForResult(new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE), Bluetooth.REQUEST_ENABLE_BLUETOOTH);
                }
            }
        }
    };

当然

startReceiver(mReceiver); // onCreate()
stopReceiver(mReceiver); // onDestroy()

非常感谢。

更新:在我进入另一项活动后,接收器再次无法工作(是的,可能是在onDestroy()中停止了?)我不知道,也许我需要把接收器放到所有活动中?天啊,我希望它不是真的。

    import android.app.Service;
import android.bluetooth.BluetoothAdapter;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;

public class BluetoothService extends Service {

    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();

        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
        this.registerReceiver(bluetoothReceiver, intentFilter);
    }

    @Override
    public void onDestroy() {
        if (bluetoothReceiver != null) {
            this.unregisterReceiver(bluetoothReceiver);
        }
        super.onDestroy();
    }

    public BroadcastReceiver bluetoothReceiver = new BroadcastReceiver()
    {
        @Override
        public void onReceive(Context context, Intent intent)
        {
            if (intent.getAction().equals(BluetoothAdapter.ACTION_STATE_CHANGED))
            {
                final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);
                Intent x = new Intent("btfilter");
                intent.putExtra("state", state);
                sendBroadcast(x);
            }
        }
    };
}

MainActivity onCreate

    btintent= new Intent(this, BluetoothService.class);
        startService(btintent);
registerReceiver(mServiceUpdatesReceiver, new IntentFilter("btfilter"));

MainActivity onDestroy()

unregisterReceiver(mServiceUpdatesReceiver);

接收器

private BroadcastReceiver mServiceUpdatesReceiver = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent)
    {
        int state = intent.getIntExtra("state", -1);

        if(state == BluetoothAdapter.STATE_OFF)
        {
            // enable request
        }
    }
};

1 个答案:

答案 0 :(得分:0)

您应该考虑使用Service。它甚至可以在应用程序关闭时工作。如果您对此不了解,请快速说明:它是没有用户界面的上下文。