sendBroadcast无法使用自定义意图筛选器



我正试图修改作为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));
            }
        }
        };
...

}

答案:

我不得不在"活动:"中向该方法添加操作

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

覆盖暂停并调用unregeisterReceiver

 unregisterReceiver(mGattUpdateReceiver);

最新更新