First you need to know the bears body language. It will most likely ; stand on its hind legs to get a better look at you, make different grunts, moans and it drool a lot with its mouth, lower its head with ears pointed back while facing you, charge forward or swap the ground. These signals are to let you know you are too close. The louder the bear is, the less dangerous it will be if you stay away. All it really wants you to do is to not get any closer, and they mostly would just rather flee the seen if you let them.
For close encounters, although it is hard, it is very important to stay calm... it can save your life. Do not run, get closer, scream, turn your back on the bear, kneel, make direct eye contact, and do not climb a tree. Your best bet, if the bear does not get any closer, slowly back away talking to it in a quiet monotone voice and watch it till it leaves but still keeping little or no eye contact. If the bear does not leave and starts to approach you, start yelling and waving your arms, making you appear larger. Throw objects at it or blow a whistle or air horn. If it still moves towards you, then stand your ground and ready your bear pepper spray for when it is about 7 metres away, or anything else you can find that will distract or threaten the bear.
If a black bear were to attack it would probably be because of its cubs, you seem like a threat, or defending its food. A black bear is rare, is a predatory bear and It mostly just wants space between you and it. Attacks are mostly in rural or remote areas and predatory bears approach quietly and may still continue to move closer no matter how "big" you make yourself seem.
If it ever comes down to an attack. Use your pepper spray, fight with everything you have, do NOT play dead unless you for sure know it was in defence for a mother bear's cubs and finally, good luck.