The idea that bears will attack if they sense we are vulnerable is an idea conjured up out of our own fear. Those are fearful swats or nips toward people who behave like bad mannered bears. No black bear has ever come after me and bit me. The slaps were not that damaging usually ripped clothing and welts on the skin nothing close to the folklore that a bear can disembowel a steer with a single swipe. Their claws are strong for climbing trees but not sharp for holding prey.
Bears regard petting as an offensive act.
A defensive action might also result when a person uses food to lure a very hungry half-tame bear closer than it feels comfortable. The bear might feed calmly enough from the person's hand until the food is gone then suddenly feel crowded without the food distraction. Too fearful to turn its back and leave, it defensively slaps, giving itself an instant afterward to turn and run. In a related scenario, fearful people often jerk their hand back each time a bear opens its mouth to take food from it. Some bears just give up and leave, but very hungry bears sometimes try more quickly and bruise a finger.