将数据传递给已经具有来自活动的意图过滤器的广播接收器

时间:2016-04-26 16:23:45

标签: java android android-intent broadcastreceiver android-bluetooth

当手机连接到车内的蓝牙时。我希望我的应用程序自动打开。要做到这一点,我必须将配对的汽车蓝牙设备名称保存为字符串。然后当手机蓝牙连接到什么东西。我得检查一下这辆车。如果是我想要开始服务。

我很难将包含蓝牙汽车设备名称的字符串传递给接收器,因为我的接收器已经收到了一个意图过滤器来监听ACTION_ACL_CONNECTED。是否有可能发送意图&对同一接收者的意图过滤器。

在这种情况下,如何将btdeviceName字符串从活动发送到接收者。

主要活动

 private void addDrawerItems() {

    final BroadcastReceiver bluetoothBroadcast = new BluetoothReceiver();
    final IntentFilter blueToothFilter = new IntentFilter(BluetoothDevice.ACTION_ACL_CONNECTED);


    final Intent btbroadcastIntent = new Intent(this, BluetoothReceiver.class);
    btbroadcastIntent.putExtra("btDeviceName", mPairedBluetoothDevice);


    String[] osArray = {"Bluetooth Auto Start", "Reply to Calls", "Reply to sms", "Customise Message"};

    mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, osArray);


    if (mIsPremiumUser) {
        mDrawerList.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE);
    } else {
        mDrawerList.setChoiceMode(AbsListView.CHOICE_MODE_NONE);
    }

    mDrawerList.setAdapter(mAdapter);


    mDrawerList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {


            if (position == 0) {
                Toast.makeText(getApplicationContext(), "blue", Toast.LENGTH_LONG).show();
                showBluetoothDialog();
            }
            return true;
        }
    });


    mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            CheckedTextView ctv = (CheckedTextView) view;

            if (!mIsPremiumUser) {
                Toast.makeText(getApplication(), "Upgrade", Toast.LENGTH_LONG).show();
                return;
            }


            switch (position) {

                case 0:

                    if (ctv.isChecked()) {

                        if (!isblueToothRegistered) {
                            registerReceiver(bluetoothBroadcast, blueToothFilter);
                            sendBroadcast(btbroadcastIntent);
                            isblueToothRegistered = true;
                        }

                    } else {
                        if (isblueToothRegistered) {
                            unregisterReceiver(bluetoothBroadcast);
                            isblueToothRegistered = false;
                        }
                    }

                    break;

BluetoothReceiver

public class BluetoothReceiver extends BroadcastReceiver {

private MainActivity ma;
private String pairedDevice;


public void onReceive(Context context, Intent intent) {


    Toast.makeText(context, "Receieved", Toast.LENGTH_LONG).show();
    String action = intent.getAction();

    pairedDevice = intent.getStringExtra("btDeviceName");
    Toast.makeText(context, pairedDevice + "2", Toast.LENGTH_LONG).show();

    final BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);


    if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) {
        Toast.makeText(context, "Bluetooth Connected", Toast.LENGTH_LONG).show();


        if (device.getName().equals(pairedDevice)) {
            Toast.makeText(context, device.getName() + " 1", Toast.LENGTH_LONG).show();
        }


    } else if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)) {
        Toast.makeText(context, "Bluetooth Disconnected", Toast.LENGTH_LONG).show();
    }


}

}

1 个答案:

答案 0 :(得分:1)

IntentFilter可以有多个操作。首先,创建自己的自定义操作名称,然后将其添加到blueToothFilter

blueToothFilter.addAction("my.custom.action");

使用此bluetoothBroadcast注册IntentFilter接收器后,它现在将接收两个动作的呼叫。在onReceive中添加其他条件以处理新的自定义操作。

最后在你的Activity发送带有自定义操作和设备名称的广播。

Intent intent = new Intent()
        .setAction("my.custom.action")
        .putExtra("btDeviceName", mPairedBluetoothDevice);

sendBroadcast(intent);

更新

我现在明白,您只需要BluetoothDevice device一次调用String pairedDeviceonReceive()。这是不可能的,因为这些变量来自单独的操作,每个操作都会调用onReceive()一次。

要解决此问题,您可以将BluetoothReceiver更改为Activity的内部类,以便您可以保留对所需数据的引用。