Chrish
Chrish

Reputation: 9

Using setresult() to pass data to activity

I'm developing a program in Android Studio to connect to a specific BLE device. I'm using setresult() to return the BLE device name etc once the BLE device is discovered. Unfortunately, setresult() is giving an error:

Error:(201, 25) error: method setResult in class BroadcastReceiver cannot be applied to given types;
required: int,String,Bundle,found: int,Intent, reason: actual and formal argument lists differ in length

Why is there an error and how do I resolve it?

 private final BroadcastReceiver bleServiceReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent sintent) {
        final String action = sintent.getAction();
        if (MldpBluetoothService.ACTION_BLE_SCAN_RESULT.equals(action)) {                       //Service has sent a scan result
            Log.d(TAG, "Scan scan result received");
            final BleDevice device = new BleDevice(sintent.getStringExtra(MldpBluetoothService.INTENT_EXTRA_SERVICE_ADDRESS), sintent.getStringExtra(MldpBluetoothService.INTENT_EXTRA_SERVICE_NAME)); //Create new item to hold name and address

            if(device.getName() != null) {

                if (device.getName().contains("Prodigy")) {                                         //+++++ Added by Chris
                    bleDeviceListAdapter.addDevice(device);                                         //+++++ if Prodigy add to the device to list adapter that displays a list on the screen
                    bleDeviceListAdapter.notifyDataSetChanged();                                    //+++++ Refresh the list on the screen
                    scanStopHandler.removeCallbacks(stopScan);                                                  //Stop the scan timeout handler from calling the runnable to stop the scan
                    scanStop();
                    final Intent intent = new Intent();                                                         //Create Intent to return information to the MldpTerminalActivity that started this activity
                    intent.putExtra(INTENT_EXTRA_SCAN_AUTO_CONNECT, alwaysConnectCheckBox.isChecked());          //Add to the Intent whether to automatically connect next time
                    intent.putExtra(INTENT_EXTRA_SCAN_NAME, device.getName());                                  //Add BLE device name to the intent
                    intent.putExtra(INTENT_EXTRA_SCAN_ADDRESS, device.getAddress());                             //Add BLE device address to the intent
                    setResult(Activity.RESULT_OK, intent);                                                  //Return an intent to the calling activity with the selected BLE name and address
                    finish();
                }
            }

        }
    }
};

Upvotes: 0

Views: 782

Answers (1)

Alan
Alan

Reputation: 296

As your code is placed in broadcastReceiver, you are using the setResult() for BroadcastReceiver. If this broadcastReceiver is in your activity, please try

YourActivity.this.setResult();

If it is outside your activity, you may need to keep the activity reference in broadcastReceiver for calling

yourActivityReference.setResult();

Upvotes: 1

Related Questions