A reliable way to get the entity object (block in this case) attached to a plugin form is from the form_state object.
$formObject = $form_state->getFormObject()
This will return an instance of a FormInterface. From there you can get the entity object:
/** @var \Drupal\block\BlockInterface $block */
$block = $formObject->getEntity();
So if you have a Block Plugin with a block instance tied to it you can use the approach above to get the instance of the $block entity.
The good thing about this is you can use it in any of the functions that utilize the FormStateInterface object (functions like blockForm,blockSubmit, etc..)
So, to put it all together:
public function defaultConfiguration() {
return ['block_id' => NULL] + parent::defaultConfiguration();
}
public function blockForm($form, FormStateInterface $form_state) {
$block = $form_state->getFormObject()->getEntity();
if (empty($config['block_id'])) {
$this->setConfigurationValue('machine_name', $block->id());
}
}
public function build(){
$config = $this->getConfiguration();
$bid = $config['block_id'];
// Put code here to build your render array with the necessary information.
}
Please use the necessary precautions to account for null values. Block plugins don't necessarily have a block instance attached to them.