{"id":396772,"date":"2024-06-29T13:02:36","date_gmt":"2024-06-29T13:02:36","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=396772"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=396772","title":{"rendered":"<span>Android: Bluetooth \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0441\u0435\u0440\u0432\u0438\u0441\u0430<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/9eb\/8bf\/213\/9eb8bf213be58e05bac032b7a92d795b.png\" width=\"780\" height=\"439\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/9eb\/8bf\/213\/9eb8bf213be58e05bac032b7a92d795b.png\"\/><figcaption><\/figcaption><\/figure>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/b7d\/197\/205\/b7d197205473e384b753beffd05dbaba.png\" alt=\"\u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f Bluetooth \u043a\u0430\u043a \u0441\u0435\u0440\u0432\u0438\u0441\u0430 \u0432 Android\" title=\"\u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f Bluetooth \u043a\u0430\u043a \u0441\u0435\u0440\u0432\u0438\u0441\u0430 \u0432 Android\" width=\"1126\" height=\"538\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/b7d\/197\/205\/b7d197205473e384b753beffd05dbaba.png\"\/><figcaption>\u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f Bluetooth \u043a\u0430\u043a \u0441\u0435\u0440\u0432\u0438\u0441\u0430 \u0432 Android<\/figcaption><\/figure>\n<h3>\u041f\u043e\u0447\u0435\u043c\u0443? \u0418 \u043a\u0430\u043a?<\/h3>\n<p>\u0412\u044b \u043a\u043e\u0433\u0434\u0430-\u043d\u0438\u0431\u0443\u0434\u044c \u0437\u0430\u0434\u0430\u0432\u0430\u043b\u0438 \u0441\u0435\u0431\u0435 \u0432\u043e\u043f\u0440\u043e\u0441, \u043f\u0440\u043e\u0447\u0438\u0442\u0430\u0432 <a href=\"https:\/\/developer.android.com\/guide\/topics\/connectivity\/bluetooth\"><u>\u043e\u0444\u0438\u0446\u0438\u0430\u043b\u044c\u043d\u043e\u0435 \u0440\u0443\u043a\u043e\u0432\u043e\u0434\u0441\u0442\u0432\u043e \u043f\u043e bluetooth<\/u><\/a> \u0434\u043b\u044f Android, \u043a\u0430\u043a \u0443\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c \u0438\u043c \u0432\u043d\u0443\u0442\u0440\u0438 \u0432\u0430\u0448\u0435\u0433\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f? \u041a\u0430\u043a \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435 \u0430\u043a\u0442\u0438\u0432\u043d\u044b\u043c, \u0434\u0430\u0436\u0435 \u043a\u043e\u0433\u0434\u0430 \u0432\u044b \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u0442\u0435 \u043e\u0442 \u043e\u0434\u043d\u043e\u0433\u043e \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f \u043a \u0434\u0440\u0443\u0433\u043e\u043c\u0443?<\/p>\n<p>\u0427\u0442\u043e \u0436, \u0432 \u044d\u0442\u043e\u043c \u0440\u0443\u043a\u043e\u0432\u043e\u0434\u0441\u0442\u0432\u0435 \u044f \u043f\u043e\u0441\u0442\u0430\u0440\u0430\u044e\u0441\u044c \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u044c \u0432\u0430\u043c, \u043a\u0430\u043a \u044f \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043b \u0441\u0432\u044f\u0437\u044c bluetooth \u0447\u0435\u0440\u0435\u0437 Service, \u0447\u0442\u043e\u0431\u044b \u0443\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c bluetooth \u0438 \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435\u043c \u0441 \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u043c\u0438 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f\u043c\u0438, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f Service Binding, \u0430 \u0442\u0430\u043a\u0436\u0435 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u043b \u0441\u043b\u0443\u0448\u0430\u0442\u0435\u043b\u044c \u043e\u0431\u0440\u0430\u0442\u043d\u043e\u0433\u043e \u0432\u044b\u0437\u043e\u0432\u0430 \u0434\u043b\u044f \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439, \u043f\u043e\u043b\u0443\u0447\u0430\u044e\u0449\u0438\u0445 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u043e \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0438 \u0441\u0432\u044f\u0437\u0438 bluetooth.<\/p>\n<p>\u0412 \u044d\u0442\u043e\u043c \u0440\u0443\u043a\u043e\u0432\u043e\u0434\u0441\u0442\u0432\u0435 \u043c\u044b \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0447\u0435\u0442\u044b\u0440\u0435 \u0444\u0430\u0439\u043b\u0430:<\/p>\n<ul>\n<li>\n<p><code>BluetoothSDKService<\/code> :\u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u0442 \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u044b\u0435 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438 bluetooth \u0438 \u0432\u044b\u0434\u0430\u0435\u0442 <code>LocalBroadcast<\/code> \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u044f \u0432\u043e \u0432\u0440\u0435\u043c\u044f \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439<\/p>\n<\/li>\n<li>\n<p><code>BluetoothSDKListenerHelper<\/code> : \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442 <code>BroadcastReceiver<\/code> \u0438 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u0442 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <code>IBluetoothSDKListener<\/code>\u00a0<\/p>\n<\/li>\n<li>\n<p><code>IBluetoothSDKListener<\/code>: \u043d\u0430\u0448 Interface, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043e\u0431\u0440\u0430\u0442\u043d\u043e\u0433\u043e \u0432\u044b\u0437\u043e\u0432\u0430<\/p>\n<\/li>\n<li>\n<p><code>BluetoothUtils<\/code> : \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u0438\u043c\u0435\u043d\u0430 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0439, \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u044b\u0445 \u0434\u043b\u044f \u0444\u0438\u043b\u044c\u0442\u0440\u0430\u0446\u0438\u0438 \u0441\u043e\u0431\u044b\u0442\u0438\u0439 \u0432 <code>BroadcastReceiver<\/code><\/p>\n<\/li>\n<\/ul>\n<h4>1) \u041e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u0435 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f<\/h4>\n<p>\u041f\u0435\u0440\u0432\u044b\u043c \u0448\u0430\u0433\u043e\u043c \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0444\u0430\u0439\u043b\u0430 BluetoothUtils.kt , \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f, \u043e \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u043c\u044b \u0445\u043e\u0442\u0438\u043c \u043f\u043e\u043b\u0443\u0447\u0430\u0442\u044c \u0443\u0432\u0435\u0434\u043e\u043c\u043b\u0435\u043d\u0438\u044f \u0432 \u043d\u0430\u0448\u0435\u0439 \u0430\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u0438:<\/p>\n<pre><code class=\"java\">class BluetoothUtils {     companion object {         val ACTION_DISCOVERY_STARTED = \"ACTION_DISCOVERY_STARTED\"         val ACTION_DISCOVERY_STOPPED = \"ACTION_DISCOVERY_STOPPED\"         val ACTION_DEVICE_FOUND = \"ACTION_DEVICE_FOUND\"         val ACTION_DEVICE_CONNECTED = \"ACTION_DEVICE_CONNECTED\"         val ACTION_DEVICE_DISCONNECTED = \"ACTION_DEVICE_DISCONNECTED\"         val ACTION_MESSAGE_RECEIVED = \"ACTION_MESSAGE_RECEIVED\"         val ACTION_MESSAGE_SENT = \"ACTION_MESSAGE_SENT\"         val ACTION_CONNECTION_ERROR = \"ACTION_CONNECTION_ERROR\"         val EXTRA_DEVICE = \"EXTRA_DEVICE\"         val EXTRA_MESSAGE = \"EXTRA_MESSAGE\"     } }<\/code><\/pre>\n<p>\u042f \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u043b \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e, \u043d\u043e \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0442\u044c \u0438\u0445 \u043f\u043e \u0441\u0432\u043e\u0435\u043c\u0443 \u0443\u0441\u043c\u043e\u0442\u0440\u0435\u043d\u0438\u044e.<\/p>\n<h4>2) \u041e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u0435 \u0441\u043e\u0431\u044b\u0442\u0438\u044f-\u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043e\u0431\u0440\u0430\u0442\u043d\u043e\u0433\u043e \u0432\u044b\u0437\u043e\u0432\u0430<\/h4>\n<p>\u0412\u0442\u043e\u0440\u043e\u0439 \u0448\u0430\u0433 &#8212; \u044d\u0442\u043e \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u043d\u0430\u0448\u0435\u0433\u043e \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u044c \u0441\u043e\u0431\u044b\u0442\u0438\u044f, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0435 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f\u043c, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u044b \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u043b\u0438 \u0432 \u043f\u0435\u0440\u0432\u043e\u043c \u0448\u0430\u0433\u0435. \u0418\u0442\u0430\u043a, \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0438\u043c \u0438 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u043c <code>IBluetoothSDKListener<\/code> \u043a\u0430\u043a:<\/p>\n<pre><code class=\"java\">interface IBluetoothSDKListener {     \/**      * from action BluetoothUtils.ACTION_DISCOVERY_STARTED      *\/     fun onDiscoveryStarted()      \/**      * from action BluetoothUtils.ACTION_DISCOVERY_STOPPED      *\/     fun onDiscoveryStopped()      \/**      * from action BluetoothUtils.ACTION_DEVICE_FOUND      *\/     fun onDeviceDiscovered(device: BluetoothDevice?)      \/**      * from action BluetoothUtils.ACTION_DEVICE_CONNECTED      *\/     fun onDeviceConnected(device: BluetoothDevice?)      \/**      * from action BluetoothUtils.ACTION_MESSAGE_RECEIVED      *\/     fun onMessageReceived(device: BluetoothDevice?, message: String?)      \/**      * from action BluetoothUtils.ACTION_MESSAGE_SENT      *\/     fun onMessageSent(device: BluetoothDevice?)      \/**      * from action BluetoothUtils.ACTION_CONNECTION_ERROR      *\/     fun onError(message: String?)      \/**      * from action BluetoothUtils.ACTION_DEVICE_DISCONNECTED      *\/     fun onDeviceDisconnected() }<\/code><\/pre>\n<p>\u042d\u0442\u043e\u0442 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 \u0431\u0443\u0434\u0435\u0442 \u043f\u043e\u0437\u0436\u0435 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d \u0432 \u043d\u0430\u0448\u0435\u0439 \u0430\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u0438, \u0438\u043b\u0438 \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442\u0435, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f \u043f\u0440\u0438 \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0438 \u0441\u043e\u0431\u044b\u0442\u0438\u044f. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043a\u043e\u0433\u0434\u0430 \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u043e \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0430\u0435\u0442\u0441\u044f, \u0441\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u0442 \u0444\u0443\u043d\u043a\u0446\u0438\u044f <code>onDeviceDiscovered<\/code>, \u0438 \u0437\u0430\u0442\u0435\u043c \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u043f\u0435\u0440\u0435\u0439\u0442\u0438 \u043a \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044e \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u044b\u0445 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043a\u0430\u043a \u043c\u044b \u0443\u0432\u0438\u0434\u0438\u043c \u0432 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0445 \u0448\u0430\u0433\u0430\u0445, \u043e\u0442\u043f\u0440\u0430\u0432\u0438\u0442\u044c \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435 \u043f\u043e bluetooth \u043d\u0430 \u0442\u043e\u043b\u044c\u043a\u043e \u0447\u0442\u043e \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u043d\u043e\u0435 \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u043e \u0447\u0435\u0440\u0435\u0437 \u043d\u0430\u0448 <code>BluetoothSDKService<\/code>.<\/p>\n<h4>3) \u041e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 BroadcastReceiver<\/h4>\n<p>\u0421\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u0448\u0430\u0433\u043e\u043c \u0431\u0443\u0434\u0435\u0442 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u043d\u0430\u0448\u0435\u0433\u043e <code>BroadcastReceiver<\/code>, \u0437\u0430\u0434\u0430\u0447\u0435\u0439 \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u0431\u0443\u0434\u0435\u0442 \u0444\u0438\u043b\u044c\u0442\u0440\u0430\u0446\u0438\u044f \u043d\u0430\u043c\u0435\u0440\u0435\u043d\u0438\u0439 \u0441 \u043d\u0430\u0448\u0438\u043c\u0438 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f\u043c\u0438, \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u044b\u043c\u0438 \u0434\u043e \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f <code>LocalBroadcastManager<\/code>, \u0434\u043b\u044f \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u0439 \u043e\u0431\u0440\u0430\u0442\u043d\u043e\u0433\u043e \u0432\u044b\u0437\u043e\u0432\u0430, \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u044b\u0445 \u0432 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u043c \u0440\u0430\u0437\u0434\u0435\u043b\u0435. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u043c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c <code>BluetoothSDKListenerHelper<\/code> \u043a\u0430\u043a:<\/p>\n<pre><code class=\"java\">class BluetoothSDKListenerHelper {     companion object {          private var mBluetoothSDKBroadcastReceiver: BluetoothSDKBroadcastReceiver? = null          class BluetoothSDKBroadcastReceiver : BroadcastReceiver() {             private var mGlobalListener: IBluetoothSDKListener? = null              public fun setBluetoothSDKListener(listener: IBluetoothSDKListener) {                 mGlobalListener = listener             }              public fun removeBluetoothSDKListener(listener: IBluetoothSDKListener): Boolean {                 if (mGlobalListener == listener) {                     mGlobalListener = null                 }                  return mGlobalListener == null             }              override fun onReceive(context: Context?, intent: Intent?) {                 val device =                     intent!!.getParcelableExtra&lt;BluetoothDevice>(BluetoothUtils.EXTRA_DEVICE)                 val message = intent.getStringExtra(BluetoothUtils.EXTRA_MESSAGE)                  when (intent.action) {                     BluetoothUtils.ACTION_DEVICE_FOUND -> {                         mGlobalListener!!.onDeviceDiscovered(device)                     }                     BluetoothUtils.ACTION_DISCOVERY_STARTED -> {                         mGlobalListener!!.onDiscoveryStarted()                     }                     BluetoothUtils.ACTION_DISCOVERY_STOPPED -> {                         mGlobalListener!!.onDiscoveryStopped()                     }                     BluetoothUtils.ACTION_DEVICE_CONNECTED -> {                         mGlobalListener!!.onDeviceConnected(device)                     }                     BluetoothUtils.ACTION_MESSAGE_RECEIVED -> {                         mGlobalListener!!.onMessageReceived(device, message)                     }                     BluetoothUtils.ACTION_MESSAGE_SENT -> {                         mGlobalListener!!.onMessageSent(device)                     }                     BluetoothUtils.ACTION_CONNECTION_ERROR -> {                         mGlobalListener!!.onError(message)                     }                     BluetoothUtils.ACTION_DEVICE_DISCONNECTED -> {                         mGlobalListener!!.onDeviceDisconnected()                     }                 }             }         }          public fun registerBluetoothSDKListener(             context: Context?,             listener: IBluetoothSDKListener         ) {             if (mBluetoothSDKBroadcastReceiver == null) {                 mBluetoothSDKBroadcastReceiver = BluetoothSDKBroadcastReceiver()                  val intentFilter = IntentFilter().also {                     it.addAction(BluetoothUtils.ACTION_DEVICE_FOUND)                     it.addAction(BluetoothUtils.ACTION_DISCOVERY_STARTED)                     it.addAction(BluetoothUtils.ACTION_DISCOVERY_STOPPED)                     it.addAction(BluetoothUtils.ACTION_DEVICE_CONNECTED)                     it.addAction(BluetoothUtils.ACTION_MESSAGE_RECEIVED)                     it.addAction(BluetoothUtils.ACTION_MESSAGE_SENT)                     it.addAction(BluetoothUtils.ACTION_CONNECTION_ERROR)                     it.addAction(BluetoothUtils.ACTION_DEVICE_DISCONNECTED)                 }                   LocalBroadcastManager.getInstance(context!!).registerReceiver(                     mBluetoothSDKBroadcastReceiver!!, intentFilter                 )             }              mBluetoothSDKBroadcastReceiver!!.setBluetoothSDKListener(listener)         }          public fun unregisterBluetoothSDKListener(             context: Context?,             listener: IBluetoothSDKListener         ) {              if (mBluetoothSDKBroadcastReceiver != null) {                 val empty = mBluetoothSDKBroadcastReceiver!!.removeBluetoothSDKListener(listener)                   if (empty) {                     LocalBroadcastManager.getInstance(context!!)                         .unregisterReceiver(mBluetoothSDKBroadcastReceiver!!)                     mBluetoothSDKBroadcastReceiver = null                 }             }         }     } }<\/code><\/pre>\n<p>\u0412 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0438 \u0438\u043b\u0438 \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442\u0435 \u043c\u044b \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c \u043d\u0430\u0448 <code>IBluetoothSDKListener<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u044b \u0437\u0430\u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0438\u0440\u0443\u0435\u043c \u0447\u0435\u0440\u0435\u0437 \u0434\u0432\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <code>registerBluetoothSDKListner()<\/code> \u0438 <code>unregisterBluetoothSDKListner()<\/code>. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440:<\/p>\n<pre><code class=\"java\">class CoolFragment() : BottomSheetDialogFragment() {      private lateinit var mService: BluetoothSDKService     private lateinit var binding: FragmentPopupDiscoveredLabelerDeviceBinding      override fun onCreate(savedInstanceState: Bundle?) {         super.onCreate(savedInstanceState)     }     override fun onCreateView(         inflater: LayoutInflater,         container: ViewGroup?,         savedInstanceState: Bundle?     ): View? {         val view = inflater.inflate(R.layout.fragment_popup_discovered_labeler_device, container,false)         binding = FragmentPopupDiscoveredLabelerDeviceBinding.bind(view)                  bindBluetoothService()                  \/\/ Register Listener         BluetoothSDKListenerHelper.registerBluetoothSDKListener(requireContext(), mBluetoothListener)          return view     }      \/**      * Bind Bluetooth Service      *\/     private fun bindBluetoothService() {         \/\/ Bind to LocalService         Intent(             requireActivity().applicationContext,             BluetoothSDKService::class.java         ).also { intent ->             requireActivity().applicationContext.bindService(                 intent,                 connection,                 Context.BIND_AUTO_CREATE             )         }     }      \/**      * Handle service connection      *\/     private val connection = object : ServiceConnection {          override fun onServiceConnected(className: ComponentName, service: IBinder) {             val binder = service as BluetoothSDKService.LocalBinder             mService = binder.getService()         }          override fun onServiceDisconnected(arg0: ComponentName) {          }     }      private val mBluetoothListener: IBluetoothSDKListener = object : IBluetoothSDKListener {         override fun onDiscoveryStarted() {         }          override fun onDiscoveryStopped() {         }          override fun onDeviceDiscovered(device: BluetoothDevice?) {         }          override fun onDeviceConnected(device: BluetoothDevice?) {             \/\/ Do stuff when is connected         }          override fun onMessageReceived(device: BluetoothDevice?, message: String?) {         }          override fun onMessageSent(device: BluetoothDevice?) {         }          override fun onError(message: String?) {         }      }      override fun onDestroy() {         super.onDestroy()         \/\/ Unregister Listener         BluetoothSDKListenerHelper.unregisterBluetoothSDKListener(requireContext(), mBluetoothListener)     } }<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043d\u0430\u0448 \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0437\u0430\u043f\u0443\u0449\u0435\u043d \u0434\u043b\u044f \u0441\u043e\u0431\u044b\u0442\u0438\u0439, \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u044b\u0445 <code>BroadcastListener<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u0442 \u0438\u0445 \u0447\u0435\u0440\u0435\u0437 \u043e\u0431\u0440\u0430\u0442\u043d\u044b\u0435 \u0432\u044b\u0437\u043e\u0432\u044b \u0432 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 \u043d\u0430\u0448\u0435\u0433\u043e \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442\u0430. \u0427\u0435\u0433\u043e \u0442\u0435\u043f\u0435\u0440\u044c \u043d\u0435 \u0445\u0432\u0430\u0442\u0430\u0435\u0442? \u041d\u0443, \u0432\u0430\u0436\u043d\u0430\u044f \u0447\u0430\u0441\u0442\u044c: \u0441\u0435\u0440\u0432\u0438\u0441 Bluetooth!\u00a0<\/p>\n<h4>4) \u041e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u0435 \u0441\u0435\u0440\u0432\u0438\u0441 Bluetooth<\/h4>\n<p>\u0410 \u0442\u0435\u043f\u0435\u0440\u044c \u0441\u0430\u043c\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u0430\u044f \u0447\u0430\u0441\u0442\u044c &#8212; Bluetooth Service. \u041c\u044b \u0441\u043e\u0431\u0438\u0440\u0430\u0435\u043c\u0441\u044f \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u043a\u043b\u0430\u0441\u0441, \u0440\u0430\u0441\u0448\u0438\u0440\u044f\u044e\u0449\u0438\u0439 <code>Service<\/code>, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u043c\u044b \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0449\u0438\u0435 \u043f\u0440\u0438\u0432\u044f\u0437\u044b\u0432\u0430\u0442\u044c Service \u0438 \u0443\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c \u043f\u043e\u0442\u043e\u043a\u0430\u043c\u0438 Bluetooth-\u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u044f:<\/p>\n<pre><code class=\"java\">class BluetoothSDKService : Service() {      \/\/ Service Binder     private val binder = LocalBinder()      \/\/ Bluetooth stuff     private lateinit var bluetoothAdapter: BluetoothAdapter     private lateinit var pairedDevices: MutableSet&lt;BluetoothDevice>     private var connectedDevice: BluetoothDevice? = null     private val MY_UUID = \"...\"     private val RESULT_INTENT = 15      \/\/ Bluetooth connections     private var connectThread: ConnectThread? = null     private var connectedThread: ConnectedThread? = null     private var mAcceptThread: AcceptThread? = null      \/\/ Invoked only first time     override fun onCreate() {         super.onCreate()         bluetoothAdapter = BluetoothAdapter.getDefaultAdapter()     }      \/\/ Invoked every service star     override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {         return START_STICKY     }      \/**      * Class used for the client Binder.      *\/     inner class LocalBinder : Binder() {         \/*         Function that can be called from Activity or Fragment         *\/     }       \/**      * Broadcast Receiver for catching ACTION_FOUND aka new device discovered      *\/     private val discoveryBroadcastReceiver = object : BroadcastReceiver() {          override fun onReceive(context: Context, intent: Intent) {             \/*               Our broadcast receiver for manage Bluetooth actions             *\/         }     }      private inner class AcceptThread : Thread() {       \/\/ Body     }      private inner class ConnectThread(device: BluetoothDevice) : Thread() {       \/\/ Body     }      @Synchronized     private fun startConnectedThread(         bluetoothSocket: BluetoothSocket?,     ) {         connectedThread = ConnectedThread(bluetoothSocket!!)         connectedThread!!.start()     }      private inner class ConnectedThread(private val mmSocket: BluetoothSocket) : Thread() {       \/\/ Body     }       override fun onDestroy() {         super.onDestroy()         try {             unregisterReceiver(discoveryBroadcastReceiver)         } catch (e: Exception) {             \/\/ already unregistered         }     }      override fun onBind(intent: Intent?): IBinder? {         return binder     }      private fun pushBroadcastMessage(action: String, device: BluetoothDevice?, message: String?) {         val intent = Intent(action)         if (device != null) {             intent.putExtra(BluetoothUtils.EXTRA_DEVICE, device)         }         if (message != null) {             intent.putExtra(BluetoothUtils.EXTRA_MESSAGE, message)         }         LocalBroadcastManager.getInstance(applicationContext).sendBroadcast(intent)     }  }<\/code><\/pre>\n<p>\u0427\u0442\u043e\u0431\u044b \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0441\u0443\u0442\u044c \u0431\u043e\u043b\u0435\u0435 \u0447\u0438\u0442\u0430\u0431\u0435\u043b\u044c\u043d\u043e\u0439, \u044f \u0437\u0430\u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043b \u0447\u0430\u0441\u0442\u0438 \u043e \u043f\u043e\u0442\u043e\u043a\u0430\u0445, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u0437 <a href=\"https:\/\/developer.android.com\/guide\/topics\/connectivity\/bluetooth\"><u>\u043e\u0444\u0438\u0446\u0438\u0430\u043b\u044c\u043d\u043e\u0439 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438<\/u><\/a>.<\/p>\n<p>\u041a\u0430\u043a \u0432\u044b \u0432\u0438\u0434\u0438\u0442\u0435, \u0432 <code>LocalBinder<\/code> \u043c\u043e\u0436\u043d\u043e \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0431\u0443\u0434\u0443\u0442 \u0432\u0438\u0434\u043d\u044b \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f\u043c \u043f\u043e\u0441\u043b\u0435 \u043f\u0440\u0438\u0432\u044f\u0437\u043a\u0438 \u043a \u043d\u0438\u043c. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0434\u043b\u044f \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 \u043e\u0431\u043d\u0430\u0440\u0443\u0436\u0435\u043d\u0438\u044f, \u043e\u0442\u043f\u0440\u0430\u0432\u043a\u0438 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u044f \u0438\u043b\u0438 \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u044f, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0437\u0430\u0442\u0435\u043c \u0431\u0443\u0434\u0443\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c\u0441\u044f \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0432\u043d\u0443\u0442\u0440\u0438 \u0441\u0435\u0440\u0432\u0438\u0441\u0430.<\/p>\n<pre><code class=\"java\">     \/**      * Class used for the client Binder.      *\/     inner class LocalBinder : Binder() {          \/**          * Enable the discovery, registering a broadcastreceiver {@link discoveryBroadcastReceiver}          * The discovery filter by LABELER_SERVER_TOKEN_NAME          *\/         public fun startDiscovery(context: Context) {             val filter = IntentFilter(BluetoothDevice.ACTION_FOUND)             filter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED)             registerReceiver(discoveryBroadcastReceiver, filter)             bluetoothAdapter.startDiscovery()             pushBroadcastMessage(BluetoothUtils.ACTION_DISCOVERY_STARTED, null, null)         }                           \/**          * stop discovery          *\/         public fun stopDiscovery() {             bluetoothAdapter.cancelDiscovery()             pushBroadcastMessage(BluetoothUtils.ACTION_DISCOVERY_STOPPED, null, null)         }                  \/\/ other stuff      }<\/code><\/pre>\n<p>\u0417\u0430\u0442\u0435\u043c \u0432 \u043f\u043e\u0442\u043e\u043a\u0430\u0445, \u0443\u043f\u0440\u0430\u0432\u043b\u044f\u044e\u0449\u0438\u0445 \u0441\u043e\u043a\u0435\u0442\u0430\u043c\u0438, \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u044e <code>pushBroadcastMessage()<\/code> \u0434\u043b\u044f \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u0441\u043e\u0431\u044b\u0442\u0438\u0439 \u0438 \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u043e\u043d\u043d\u043e\u0433\u043e \u043d\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f, \u0442\u0430\u043a\u043e\u0433\u043e \u043a\u0430\u043a \u0443\u0434\u0430\u043b\u0435\u043d\u043d\u043e\u0435 \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u043e \u0438 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440:<\/p>\n<pre><code class=\"java\">private inner class ConnectedThread(private val mmSocket: BluetoothSocket) : Thread() {          private val mmInStream: InputStream = mmSocket.inputStream         private val mmOutStream: OutputStream = mmSocket.outputStream         private val mmBuffer: ByteArray = ByteArray(1024) \/\/ mmBuffer store for the stream          override fun run() {             var numBytes: Int \/\/ bytes returned from read()              \/\/ Keep listening to the InputStream until an exception occurs.             while (true) {                 \/\/ Read from the InputStream.                 numBytes = try {                     mmInStream.read(mmBuffer)                 } catch (e: IOException) {                     pushBroadcastMessage(                         BluetoothUtils.ACTION_CONNECTION_ERROR,                         null,                         \"Input stream was disconnected\"                     )                     break                 }                  val message = String(mmBuffer, 0, numBytes)                  \/\/ Send to broadcast the message                 pushBroadcastMessage(                     BluetoothUtils.ACTION_MESSAGE_RECEIVED,                     mmSocket.remoteDevice,                     message                 )             }         }          \/\/ Call this from the main activity to send data to the remote device.         fun write(bytes: ByteArray) {             try {                 mmOutStream.write(bytes)                  \/\/ Send to broadcast the message                 pushBroadcastMessage(                     BluetoothUtils.ACTION_MESSAGE_SENT,                     mmSocket.remoteDevice,                     null                 )             } catch (e: IOException) {                 pushBroadcastMessage(                     BluetoothUtils.ACTION_CONNECTION_ERROR,                     null,                     \"Error occurred when sending data\"                 )                 return             }         }          \/\/ Call this method from the main activity to shut down the connection.         fun cancel() {             try {                 mmSocket.close()             } catch (e: IOException) {                 pushBroadcastMessage(                     BluetoothUtils.ACTION_CONNECTION_ERROR,                     null,                     \"Could not close the connect socket\"                 )             }         }     }<\/code><\/pre>\n<p>\u041c\u044b \u0437\u0430\u043a\u043e\u043d\u0447\u0438\u043b\u0438!<\/p>\n<h3>\u0417\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/h3>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w780q1\/getpro\/habr\/upload_files\/964\/56e\/6b5\/96456e6b599694f788096e36b5c5c51f.jpeg\" width=\"1280\" height=\"500\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/964\/56e\/6b5\/96456e6b599694f788096e36b5c5c51f.jpeg\" data-blurred=\"true\"\/><figcaption><\/figcaption><\/figure>\n<p>\u041c\u044b \u0432\u0438\u0434\u0435\u043b\u0438, \u043a\u0430\u043a \u0438\u0437 \u043d\u0430\u0448\u0435\u0439 \u0430\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u0438 \u043c\u043e\u0436\u0435\u043c \u0441\u0432\u044f\u0437\u0430\u0442\u044c \u0441\u0435\u0440\u0432\u0438\u0441 Bluetooth (1), \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442 \u0438 \u0443\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u0442 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f\u043c\u0438 Bluetooth. \u0412 \u043d\u0435\u043c \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0442\u044c \u043c\u043d\u043e\u0433\u043e\u0430\u0434\u0440\u0435\u0441\u043d\u043e\u0435 \u0441\u043e\u0431\u044b\u0442\u0438\u0435 (broadcast event) (2), \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442 Bluetooth-\u043f\u0440\u0438\u0435\u043c\u043d\u0438\u043a. \u041f\u043e\u043b\u0443\u0447\u0438\u0432 \u0438\u0445, Bluetooth-\u043f\u0440\u0438\u0435\u043c\u043d\u0438\u043a, \u0432 \u0441\u0432\u043e\u044e \u043e\u0447\u0435\u0440\u0435\u0434\u044c, \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442 \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0430, \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u043d\u0443\u044e (4) \u0432 \u043d\u0430\u0448\u0435\u0439 \u0430\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u0438, \u0437\u0430\u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0439 \u043d\u0430 bluetooth-\u043f\u0440\u0438\u0435\u043c\u043d\u0438\u043a(3)<\/p>\n<p><em>\u041c\u043e\u0439 \u0441\u043e\u0432\u0435\u0442 &#8212; \u0432\u0441\u0435\u0433\u0434\u0430 \u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u044c \u043e\u0444\u0438\u0446\u0438\u0430\u043b\u044c\u043d\u043e\u043c\u0443 \u0440\u0443\u043a\u043e\u0432\u043e\u0434\u0441\u0442\u0432\u0443 \u0438 \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0430\u0446\u0438\u044f\u043c \u043f\u043e \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u044e \u0447\u0438\u0441\u0442\u043e\u0433\u043e \u043a\u043e\u0434\u0430.<\/em><\/p>\n<hr\/>\n<blockquote>\n<p>\u041c\u0430\u0442\u0435\u0440\u0438\u0430\u043b \u043f\u043e\u0434\u0433\u043e\u0442\u043e\u0432\u043b\u0435\u043d \u0432 \u0440\u0430\u043c\u043a\u0430\u0445 \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438<a href=\"https:\/\/otus.pw\/PgXu\/\"> \u00abAndroid Developer\u00bb.<\/a><\/p>\n<p>\u0412\u0441\u0435\u0445 \u0436\u0435\u043b\u0430\u044e\u0449\u0438\u0445 \u043f\u0440\u0438\u0433\u043b\u0430\u0448\u0430\u0435\u043c \u043d\u0430 \u0434\u0432\u0443\u0445\u0434\u043d\u0435\u0432\u043d\u044b\u0439 \u043e\u043d\u043b\u0430\u0439\u043d-\u0438\u043d\u0442\u0435\u043d\u0441\u0438\u0432 <strong>\u00ab\u0414\u0435\u043b\u0430\u0435\u043c \u043c\u043e\u0431\u0438\u043b\u044c\u043d\u0443\u044e \u043c\u0438\u043d\u0438-\u0438\u0433\u0440\u0443 \u0437\u0430 2 \u0434\u043d\u044f\u00bb<\/strong>. \u0417\u0430 2 \u0434\u043d\u044f \u0432\u044b \u0441\u0434\u0435\u043b\u0430\u0435\u0442\u0435 \u043c\u043e\u0431\u0438\u043b\u044c\u043d\u0443\u044e \u0432\u0435\u0440\u0441\u0438\u044e PopIt \u043d\u0430 \u044f\u0437\u044b\u043a\u0435 Kotlin. \u0412 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0438 \u0431\u0443\u0434\u0435\u0442 \u043f\u0440\u043e\u0441\u0442\u0430\u044f \u0430\u043d\u0438\u043c\u0430\u0446\u0438\u044f, \u0437\u0432\u0443\u043a \u0445\u043b\u043e\u043f\u043a\u0430, \u0432\u0438\u0431\u0440\u0430\u0446\u0438\u044f, \u0442\u0430\u0439\u043c\u0435\u0440 \u043a\u0430\u043a \u0441\u043e\u0440\u0435\u0432\u043d\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442. \u0418\u043d\u0442\u0435\u043d\u0441\u0438\u0432 \u043f\u043e\u0434\u043e\u0439\u0434\u0435\u0442 \u0434\u043b\u044f \u0442\u0435\u0445, \u043a\u0442\u043e \u0445\u043e\u0447\u0435\u0442 \u043f\u043e\u043f\u0440\u043e\u0431\u043e\u0432\u0430\u0442\u044c \u0441\u0435\u0431\u044f \u0432 \u0440\u043e\u043b\u0438 Android-\u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0430.  <a href=\"https:\/\/otus.pw\/pZHI\/\"><strong>>> \u0420\u0415\u0413\u0418\u0421\u0422\u0420\u0410\u0426\u0418\u042f<\/strong><\/a><\/p>\n<\/blockquote>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/575532\/\"> https:\/\/habr.com\/ru\/articles\/575532\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<figure class=\"full-width\"><figcaption>\u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f Bluetooth \u043a\u0430\u043a \u0441\u0435\u0440\u0432\u0438\u0441\u0430 \u0432 Android<\/figcaption><\/figure>\n<h3>\u041f\u043e\u0447\u0435\u043c\u0443? \u0418 \u043a\u0430\u043a?<\/h3>\n<p>\u0412\u044b \u043a\u043e\u0433\u0434\u0430-\u043d\u0438\u0431\u0443\u0434\u044c \u0437\u0430\u0434\u0430\u0432\u0430\u043b\u0438 \u0441\u0435\u0431\u0435 \u0432\u043e\u043f\u0440\u043e\u0441, \u043f\u0440\u043e\u0447\u0438\u0442\u0430\u0432 <a href=\"https:\/\/developer.android.com\/guide\/topics\/connectivity\/bluetooth\"><u>\u043e\u0444\u0438\u0446\u0438\u0430\u043b\u044c\u043d\u043e\u0435 \u0440\u0443\u043a\u043e\u0432\u043e\u0434\u0441\u0442\u0432\u043e \u043f\u043e bluetooth<\/u><\/a> \u0434\u043b\u044f Android, \u043a\u0430\u043a \u0443\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c \u0438\u043c \u0432\u043d\u0443\u0442\u0440\u0438 \u0432\u0430\u0448\u0435\u0433\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f? \u041a\u0430\u043a \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435 \u0430\u043a\u0442\u0438\u0432\u043d\u044b\u043c, \u0434\u0430\u0436\u0435 \u043a\u043e\u0433\u0434\u0430 \u0432\u044b \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u0442\u0435 \u043e\u0442 \u043e\u0434\u043d\u043e\u0433\u043e \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f \u043a \u0434\u0440\u0443\u0433\u043e\u043c\u0443?<\/p>\n<p>\u0427\u0442\u043e \u0436, \u0432 \u044d\u0442\u043e\u043c \u0440\u0443\u043a\u043e\u0432\u043e\u0434\u0441\u0442\u0432\u0435 \u044f \u043f\u043e\u0441\u0442\u0430\u0440\u0430\u044e\u0441\u044c \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u044c \u0432\u0430\u043c, \u043a\u0430\u043a \u044f \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043b \u0441\u0432\u044f\u0437\u044c bluetooth \u0447\u0435\u0440\u0435\u0437 Service, \u0447\u0442\u043e\u0431\u044b \u0443\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c bluetooth \u0438 \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435\u043c \u0441 \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u043c\u0438 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f\u043c\u0438, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f Service Binding, \u0430 \u0442\u0430\u043a\u0436\u0435 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u043b \u0441\u043b\u0443\u0448\u0430\u0442\u0435\u043b\u044c \u043e\u0431\u0440\u0430\u0442\u043d\u043e\u0433\u043e \u0432\u044b\u0437\u043e\u0432\u0430 \u0434\u043b\u044f \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439, \u043f\u043e\u043b\u0443\u0447\u0430\u044e\u0449\u0438\u0445 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u043e \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0438 \u0441\u0432\u044f\u0437\u0438 bluetooth.<\/p>\n<p>\u0412 \u044d\u0442\u043e\u043c \u0440\u0443\u043a\u043e\u0432\u043e\u0434\u0441\u0442\u0432\u0435 \u043c\u044b \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0447\u0435\u0442\u044b\u0440\u0435 \u0444\u0430\u0439\u043b\u0430:<\/p>\n<ul>\n<li>\n<p><code>BluetoothSDKService<\/code> :\u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u0442 \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u044b\u0435 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438 bluetooth \u0438 \u0432\u044b\u0434\u0430\u0435\u0442 <code>LocalBroadcast<\/code> \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u044f \u0432\u043e \u0432\u0440\u0435\u043c\u044f \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439<\/p>\n<\/li>\n<li>\n<p><code>BluetoothSDKListenerHelper<\/code> : \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442 <code>BroadcastReceiver<\/code> \u0438 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u0442 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <code>IBluetoothSDKListener<\/code>\u00a0<\/p>\n<\/li>\n<li>\n<p><code>IBluetoothSDKListener<\/code>: \u043d\u0430\u0448 Interface, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043e\u0431\u0440\u0430\u0442\u043d\u043e\u0433\u043e \u0432\u044b\u0437\u043e\u0432\u0430<\/p>\n<\/li>\n<li>\n<p><code>BluetoothUtils<\/code> : \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u0438\u043c\u0435\u043d\u0430 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0439, \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u044b\u0445 \u0434\u043b\u044f \u0444\u0438\u043b\u044c\u0442\u0440\u0430\u0446\u0438\u0438 \u0441\u043e\u0431\u044b\u0442\u0438\u0439 \u0432 <code>BroadcastReceiver<\/code><\/p>\n<\/li>\n<\/ul>\n<h4>1) \u041e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u0435 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f<\/h4>\n<p>\u041f\u0435\u0440\u0432\u044b\u043c \u0448\u0430\u0433\u043e\u043c \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0444\u0430\u0439\u043b\u0430 BluetoothUtils.kt , \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f, \u043e \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u043c\u044b \u0445\u043e\u0442\u0438\u043c \u043f\u043e\u043b\u0443\u0447\u0430\u0442\u044c \u0443\u0432\u0435\u0434\u043e\u043c\u043b\u0435\u043d\u0438\u044f \u0432 \u043d\u0430\u0448\u0435\u0439 \u0430\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u0438:<\/p>\n<pre><code class=\"java\">class BluetoothUtils {     companion object {         val ACTION_DISCOVERY_STARTED = \"ACTION_DISCOVERY_STARTED\"         val ACTION_DISCOVERY_STOPPED = \"ACTION_DISCOVERY_STOPPED\"         val ACTION_DEVICE_FOUND = \"ACTION_DEVICE_FOUND\"         val ACTION_DEVICE_CONNECTED = \"ACTION_DEVICE_CONNECTED\"         val ACTION_DEVICE_DISCONNECTED = \"ACTION_DEVICE_DISCONNECTED\"         val ACTION_MESSAGE_RECEIVED = \"ACTION_MESSAGE_RECEIVED\"         val ACTION_MESSAGE_SENT = \"ACTION_MESSAGE_SENT\"         val ACTION_CONNECTION_ERROR = \"ACTION_CONNECTION_ERROR\"         val EXTRA_DEVICE = \"EXTRA_DEVICE\"         val EXTRA_MESSAGE = \"EXTRA_MESSAGE\"     } }<\/code><\/pre>\n<p>\u042f \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u043b \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e, \u043d\u043e \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0442\u044c \u0438\u0445 \u043f\u043e \u0441\u0432\u043e\u0435\u043c\u0443 \u0443\u0441\u043c\u043e\u0442\u0440\u0435\u043d\u0438\u044e.<\/p>\n<h4>2) \u041e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u0435 \u0441\u043e\u0431\u044b\u0442\u0438\u044f-\u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043e\u0431\u0440\u0430\u0442\u043d\u043e\u0433\u043e \u0432\u044b\u0437\u043e\u0432\u0430<\/h4>\n<p>\u0412\u0442\u043e\u0440\u043e\u0439 \u0448\u0430\u0433 &#8212; \u044d\u0442\u043e \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u043d\u0430\u0448\u0435\u0433\u043e \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u044c \u0441\u043e\u0431\u044b\u0442\u0438\u044f, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0435 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f\u043c, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u044b \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u043b\u0438 \u0432 \u043f\u0435\u0440\u0432\u043e\u043c \u0448\u0430\u0433\u0435. \u0418\u0442\u0430\u043a, \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0438\u043c \u0438 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u043c <code>IBluetoothSDKListener<\/code> \u043a\u0430\u043a:<\/p>\n<pre><code class=\"java\">interface IBluetoothSDKListener {     \/**      * from action BluetoothUtils.ACTION_DISCOVERY_STARTED      *\/     fun onDiscoveryStarted()      \/**      * from action BluetoothUtils.ACTION_DISCOVERY_STOPPED      *\/     fun onDiscoveryStopped()      \/**      * from action BluetoothUtils.ACTION_DEVICE_FOUND      *\/     fun onDeviceDiscovered(device: BluetoothDevice?)      \/**      * from action BluetoothUtils.ACTION_DEVICE_CONNECTED      *\/     fun onDeviceConnected(device: BluetoothDevice?)      \/**      * from action BluetoothUtils.ACTION_MESSAGE_RECEIVED      *\/     fun onMessageReceived(device: BluetoothDevice?, message: String?)      \/**      * from action BluetoothUtils.ACTION_MESSAGE_SENT      *\/     fun onMessageSent(device: BluetoothDevice?)      \/**      * from action BluetoothUtils.ACTION_CONNECTION_ERROR      *\/     fun onError(message: String?)      \/**      * from action BluetoothUtils.ACTION_DEVICE_DISCONNECTED      *\/     fun onDeviceDisconnected() }<\/code><\/pre>\n<p>\u042d\u0442\u043e\u0442 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 \u0431\u0443\u0434\u0435\u0442 \u043f\u043e\u0437\u0436\u0435 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d \u0432 \u043d\u0430\u0448\u0435\u0439 \u0430\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u0438, \u0438\u043b\u0438 \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442\u0435, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f \u043f\u0440\u0438 \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0438 \u0441\u043e\u0431\u044b\u0442\u0438\u044f. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043a\u043e\u0433\u0434\u0430 \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u043e \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0430\u0435\u0442\u0441\u044f, \u0441\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u0442 \u0444\u0443\u043d\u043a\u0446\u0438\u044f <code>onDeviceDiscovered<\/code>, \u0438 \u0437\u0430\u0442\u0435\u043c \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u043f\u0435\u0440\u0435\u0439\u0442\u0438 \u043a \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044e \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u044b\u0445 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043a\u0430\u043a \u043c\u044b \u0443\u0432\u0438\u0434\u0438\u043c \u0432 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0445 \u0448\u0430\u0433\u0430\u0445, \u043e\u0442\u043f\u0440\u0430\u0432\u0438\u0442\u044c \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435 \u043f\u043e bluetooth \u043d\u0430 \u0442\u043e\u043b\u044c\u043a\u043e \u0447\u0442\u043e \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u043d\u043e\u0435 \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u043e \u0447\u0435\u0440\u0435\u0437 \u043d\u0430\u0448 <code>BluetoothSDKService<\/code>.<\/p>\n<h4>3) \u041e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 BroadcastReceiver<\/h4>\n<p>\u0421\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u0448\u0430\u0433\u043e\u043c \u0431\u0443\u0434\u0435\u0442 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u043d\u0430\u0448\u0435\u0433\u043e <code>BroadcastReceiver<\/code>, \u0437\u0430\u0434\u0430\u0447\u0435\u0439 \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u0431\u0443\u0434\u0435\u0442 \u0444\u0438\u043b\u044c\u0442\u0440\u0430\u0446\u0438\u044f \u043d\u0430\u043c\u0435\u0440\u0435\u043d\u0438\u0439 \u0441 \u043d\u0430\u0448\u0438\u043c\u0438 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f\u043c\u0438, \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u044b\u043c\u0438 \u0434\u043e \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f <code>LocalBroadcastManager<\/code>, \u0434\u043b\u044f \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u0439 \u043e\u0431\u0440\u0430\u0442\u043d\u043e\u0433\u043e \u0432\u044b\u0437\u043e\u0432\u0430, \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u044b\u0445 \u0432 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u043c \u0440\u0430\u0437\u0434\u0435\u043b\u0435. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u043c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c <code>BluetoothSDKListenerHelper<\/code> \u043a\u0430\u043a:<\/p>\n<pre><code class=\"java\">class BluetoothSDKListenerHelper {     companion object {          private var mBluetoothSDKBroadcastReceiver: BluetoothSDKBroadcastReceiver? = null          class BluetoothSDKBroadcastReceiver : BroadcastReceiver() {             private var mGlobalListener: IBluetoothSDKListener? = null              public fun setBluetoothSDKListener(listener: IBluetoothSDKListener) {                 mGlobalListener = listener             }              public fun removeBluetoothSDKListener(listener: IBluetoothSDKListener): Boolean {                 if (mGlobalListener == listener) {                     mGlobalListener = null                 }                  return mGlobalListener == null             }              override fun onReceive(context: Context?, intent: Intent?) {                 val device =                     intent!!.getParcelableExtra&lt;BluetoothDevice>(BluetoothUtils.EXTRA_DEVICE)                 val message = intent.getStringExtra(BluetoothUtils.EXTRA_MESSAGE)                  when (intent.action) {                     BluetoothUtils.ACTION_DEVICE_FOUND -> {                         mGlobalListener!!.onDeviceDiscovered(device)                     }                     BluetoothUtils.ACTION_DISCOVERY_STARTED -> {                         mGlobalListener!!.onDiscoveryStarted()                     }                     BluetoothUtils.ACTION_DISCOVERY_STOPPED -> {                         mGlobalListener!!.onDiscoveryStopped()                     }                     BluetoothUtils.ACTION_DEVICE_CONNECTED -> {                         mGlobalListener!!.onDeviceConnected(device)                     }                     BluetoothUtils.ACTION_MESSAGE_RECEIVED -> {                         mGlobalListener!!.onMessageReceived(device, message)                     }                     BluetoothUtils.ACTION_MESSAGE_SENT -> {                         mGlobalListener!!.onMessageSent(device)                     }                     BluetoothUtils.ACTION_CONNECTION_ERROR -> {                         mGlobalListener!!.onError(message)                     }                     BluetoothUtils.ACTION_DEVICE_DISCONNECTED -> {                         mGlobalListener!!.onDeviceDisconnected()                     }                 }             }         }          public fun registerBluetoothSDKListener(             context: Context?,             listener: IBluetoothSDKListener         ) {             if (mBluetoothSDKBroadcastReceiver == null) {                 mBluetoothSDKBroadcastReceiver = BluetoothSDKBroadcastReceiver()                  val intentFilter = IntentFilter().also {                     it.addAction(BluetoothUtils.ACTION_DEVICE_FOUND)                     it.addAction(BluetoothUtils.ACTION_DISCOVERY_STARTED)                     it.addAction(BluetoothUtils.ACTION_DISCOVERY_STOPPED)                     it.addAction(BluetoothUtils.ACTION_DEVICE_CONNECTED)                     it.addAction(BluetoothUtils.ACTION_MESSAGE_RECEIVED)                     it.addAction(BluetoothUtils.ACTION_MESSAGE_SENT)                     it.addAction(BluetoothUtils.ACTION_CONNECTION_ERROR)                     it.addAction(BluetoothUtils.ACTION_DEVICE_DISCONNECTED)                 }                   LocalBroadcastManager.getInstance(context!!).registerReceiver(                     mBluetoothSDKBroadcastReceiver!!, intentFilter                 )             }              mBluetoothSDKBroadcastReceiver!!.setBluetoothSDKListener(listener)         }          public fun unregisterBluetoothSDKListener(             context: Context?,             listener: IBluetoothSDKListener         ) {              if (mBluetoothSDKBroadcastReceiver != null) {                 val empty = mBluetoothSDKBroadcastReceiver!!.removeBluetoothSDKListener(listener)                   if (empty) {                     LocalBroadcastManager.getInstance(context!!)                         .unregisterReceiver(mBluetoothSDKBroadcastReceiver!!)                     mBluetoothSDKBroadcastReceiver = null                 }             }         }     } }<\/code><\/pre>\n<p>\u0412 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0438 \u0438\u043b\u0438 \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442\u0435 \u043c\u044b \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c \u043d\u0430\u0448 <code>IBluetoothSDKListener<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u044b \u0437\u0430\u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0438\u0440\u0443\u0435\u043c \u0447\u0435\u0440\u0435\u0437 \u0434\u0432\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <code>registerBluetoothSDKListner()<\/code> \u0438 <code>unregisterBluetoothSDKListner()<\/code>. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440:<\/p>\n<pre><code class=\"java\">class CoolFragment() : BottomSheetDialogFragment() {      private lateinit var mService: BluetoothSDKService     private lateinit var binding: FragmentPopupDiscoveredLabelerDeviceBinding      override fun onCreate(savedInstanceState: Bundle?) {         super.onCreate(savedInstanceState)     }     override fun onCreateView(         inflater: LayoutInflater,         container: ViewGroup?,         savedInstanceState: Bundle?     ): View? {         val view = inflater.inflate(R.layout.fragment_popup_discovered_labeler_device, container,false)         binding = FragmentPopupDiscoveredLabelerDeviceBinding.bind(view)                  bindBluetoothService()                  \/\/ Register Listener         BluetoothSDKListenerHelper.registerBluetoothSDKListener(requireContext(), mBluetoothListener)          return view     }      \/**      * Bind Bluetooth Service      *\/     private fun bindBluetoothService() {         \/\/ Bind to LocalService         Intent(             requireActivity().applicationContext,             BluetoothSDKService::class.java         ).also { intent ->             requireActivity().applicationContext.bindService(                 intent,                 connection,                 Context.BIND_AUTO_CREATE             )         }     }      \/**      * Handle service connection      *\/     private val connection = object : ServiceConnection {          override fun onServiceConnected(className: ComponentName, service: IBinder) {             val binder = service as BluetoothSDKService.LocalBinder             mService = binder.getService()         }          override fun onServiceDisconnected(arg0: ComponentName) {          }     }      private val mBluetoothListener: IBluetoothSDKListener = object : IBluetoothSDKListener {         override fun onDiscoveryStarted() {         }<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-396772","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/396772","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=396772"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/396772\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=396772"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=396772"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=396772"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}