android - sendBroadcast 不适用于自定义 Intent 过滤器

标签 android android-intent bluetooth-lowenergy intentfilter

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

这是服务代码:

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

以及 Activity :

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

最佳答案

覆盖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);

关于android - sendBroadcast 不适用于自定义 Intent 过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31147610/

相关文章:

android - 全局捕获所有可能的 android 异常并重新加载应用程序

java - Parcelable 在 Intent putExtra 上写入可序列化对象时遇到 IOException

android - java.lang.RuntimeException : Handler (android. os.Handler) 向死线程上的 Handler 发送消息

java - 在 A17-Realtag-BLE-Sensor-Wearable 上获取温度或压力

android - 以编程方式单击时更改布局背景

android - 我如何通过键盘在应用程序中获取卢比符号

android - API 级别 10 及以后的屏幕尺寸?

用于将 GPS 坐标发送到服务器的 Android 应用程序崩溃

raspberry-pi - 使用 Gatttool 连接到 BLE 设备时出错

Android BLE - 一次连接到多个设备