【问题标题】:sendBroadcast not working with custom intent filtersendBroadcast 不使用自定义意图过滤器
【发布时间】:2015-07-01 04:34:04
【问题描述】:

我正在尝试修改作为示例与 Android Studio 一起提供的 BluetoothLeGatt 项目,以便在每次读取特征时将 RSSI 值发送回主活动以进行显示。我创建了一个新的意图过滤器,但接收者从未收到意图。我已经尝试将它添加到清单文件中,但这也不起作用。

这是服务代码:

public class BluetoothLeService extends Service {
...
public final static String ACTION_GATT_CONNECTED =
        "com.example.bluetooth.le.ACTION_GATT_CONNECTED";
public final static String ACTION_GATT_DISCONNECTED =
        "com.example.bluetooth.le.ACTION_GATT_DISCONNECTED";
public final static String ACTION_GATT_SERVICES_DISCOVERED =
        "com.example.bluetooth.le.ACTION_GATT_SERVICES_DISCOVERED";
public final static String ACTION_DATA_AVAILABLE =
        "com.example.bluetooth.le.ACTION_DATA_AVAILABLE";
public final static String RSSI_DATA_AVAILABLE =
        "com.example.bluetooth.le.RSSI_DATA_AVAILABLE";
public final static String EXTRA_DATA =
        "com.example.bluetooth.le.EXTRA_DATA";
public final static String EXTRA_RSSI =
        "com.example.bluetooth.le.EXTRA_RSSI";

@Override
public void onCharacteristicRead(BluetoothGatt gatt,
                                     BluetoothGattCharacteristic characteristic,
                                     int status) {
    if (status == BluetoothGatt.GATT_SUCCESS) {
        broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
        mBluetoothGatt.readRemoteRssi();
    }
}

@Override
public void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status) {
    final Intent intent = new Intent();
    if (status == BluetoothGatt.GATT_SUCCESS) {
        intent.putExtra(EXTRA_RSSI, String.valueOf(rssi));
        intent.setAction(RSSI_DATA_AVAILABLE);
        sendBroadcast(intent);
    }
}

还有活动:

public class DeviceControlActivity extends Activity {
    ...
    private final BroadcastReceiver mGattUpdateReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            final String action = intent.getAction();
            if (BluetoothLeService.ACTION_GATT_CONNECTED.equals(action)) {
                mConnected = true;
                updateConnectionState(R.string.connected);
                invalidateOptionsMenu();
            } else if (BluetoothLeService.ACTION_GATT_DISCONNECTED.equals(action)) {
                mConnected = false;
                updateConnectionState(R.string.disconnected);
                invalidateOptionsMenu();
                clearUI();
            } else if (BluetoothLeService.ACTION_GATT_SERVICES_DISCOVERED.equals(action)) {
                // Show all the supported services and characteristics on the user interface.
                displayGattServices(mBluetoothLeService.getSupportedGattServices());
            } else if (BluetoothLeService.ACTION_DATA_AVAILABLE.equals(action)) {
                displayData(intent.getStringExtra(BluetoothLeService.EXTRA_DATA));
            } else if (BluetoothLeService.RSSI_DATA_AVAILABLE.equals(action)) {
                updateRssi(intent.getStringExtra(BluetoothLeService.EXTRA_RSSI));
            }
        }
        };
...

}

答案:

我必须在 Activity 中将操作添加到此方法中:

    private static IntentFilter makeGattUpdateIntentFilter() {
    final IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction(BluetoothLeService.ACTION_GATT_CONNECTED);
    intentFilter.addAction(BluetoothLeService.ACTION_GATT_DISCONNECTED);
    intentFilter.addAction(BluetoothLeService.ACTION_GATT_SERVICES_DISCOVERED);
    intentFilter.addAction(BluetoothLeService.ACTION_DATA_AVAILABLE);
    intentFilter.addAction(BluetoothLeService.RSSI_DATA_AVAILABLE);
    return intentFilter;
}

【问题讨论】:

  • 你在哪里注册 BroadcastReceiver ?
  • 不确定。安卓菜鸟。它不会出现在示例代码的任何清单文件中。
  • 你创建了一个 IntentFilter 吗?
  • 仅在服务类中:public final static String RSSI_DATA_AVAILABLE = "com.example.bluetooth.le.RSSI_DATA_AVAILABLE"; 然后我在 onReceive 函数中检查它:else if (BluetoothLeService.RSSI_DATA_AVAILABLE.equals(action)) { updateRssi(intent.getStringExtra(BluetoothLeService.EXTRA_RSSI));

标签: android android-intent bluetooth-lowenergy intentfilter


【解决方案1】:

在您的Activity 中覆盖onResume,创建一个IntentFilter,添加您的自定义操作,并注册广播接收器。

IntentFilter filter = new IntentFilter();
// call addAction for every action you want to receive in your BroadcastReceiver
filter.addAction(BluetoothLeService.ACTION_GATT_CONNECTED);

然后注册

registerReceiver(mGattUpdateReceiver, filter);

覆盖 onPause 并调用 unregeisterReceiver

 unregisterReceiver(mGattUpdateReceiver);

【讨论】:

  • 谢谢,就是这样。我所要做的就是在这个方法中添加新的操作:private static IntentFilter makeGattUpdateIntentFilter()
猜你喜欢
  • 2021-08-09
  • 2015-09-11
  • 2013-05-04
  • 2015-02-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-26
  • 1970-01-01
相关资源
最近更新 更多