{"id":90,"date":"2019-11-24T16:58:16","date_gmt":"2019-11-24T08:58:16","guid":{"rendered":"http:\/\/cf3b5.com\/blog\/?p=90"},"modified":"2019-11-26T09:43:10","modified_gmt":"2019-11-26T01:43:10","slug":"raspberry-pi%e8%93%9d%e7%89%99%e5%bc%80%e5%8f%91%ef%bc%88%e4%b8%80%ef%bc%89","status":"publish","type":"post","link":"http:\/\/cf3b5.com\/blog\/?p=90","title":{"rendered":"Raspberry Pi\u7684\u84dd\u7259\u5f00\u53d1\uff08\u4e00\uff09"},"content":{"rendered":"\n<p>\u9996\u5148\uff0c\u5b89\u88c5\u57fa\u672c\u7684\u84dd\u7259\u5e93<\/p>\n\n\n\n<pre class=\"wp-block-syntaxhighlighter-code\">sudo apt-get install python-dev\nsudo apt-get install libbluetooth-dev\nsudo pip install pybluez<\/pre>\n\n\n\n<p>\u4fee\u6539\/etc\/systemd\/system\/bluetooth.target.wants\/bluetooth.service\u6587\u4ef6<\/p>\n\n\n\n<p class=\"has-text-color has-background has-very-dark-gray-color has-luminous-vivid-amber-background-color\">\u7279\u522b\u662f\u8981\u5207\u8bb0\u5141\u8bb8\u6253\u5f00\u4e32\u53e3\u670d\u52a1ExecStartPost\u8fd9\u4e2a\uff08\u5751\u7239\u7684\u7f51\u4e0a\u5927\u90e8\u5206\u6587\u7ae0\u90fd\u6ca1\u6709\u63d0\u53ca\u8fd9\u4e2a\uff0c\u5bfc\u81f4\u626b\u63cf\u7684\u5230\uff0c\u4f46\u662f\u5404\u79cd\u4e32\u53e3\u8c03\u8bd5\u5de5\u5177\u6b7b\u6d3b\u8fde\u4e0d\u4e0a\uff0c\u539f\u56e0\u672a\u77e5\u2026\u2026\uff09<\/p>\n\n\n\n<pre class=\"wp-block-syntaxhighlighter-code\">ExecStart=\/usr\/lib\/bluetooth\/bluetoothd\nExecStartPost=\/usr\/bin\/sdptool add SP<\/pre>\n\n\n\n<p>\u5b9e\u73b0\u4ee3\u7801\uff0cPython\u7684<\/p>\n\n\n\n<pre class=\"wp-block-syntaxhighlighter-code\">#!\/usr\/bin\/env python\n# -*- coding: utf-8 -*-\n\n'''\n    \u738b\u653f\u4e54\u7684\u4e2a\u4eba\u4ee3\u7801\u5e93\uff1a\u84dd\u7259\n    \u5f53\u524d\u7248\u672c\uff1a1.0.0\n    Python:2+;3+\n    \u8bf7\u6ce8\u610f\uff0cwindows\u548clinux\u5b58\u5728\u5dee\u5f02\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u517c\u5bb9\u9519\u8bef\u3002\n'''\n\nfrom bluetooth import *\nimport subprocess\n\n\nclass service:\n\n    uuid = \"94f39d29-7d6d-437d-973b-fba39e49d4ee\"\n    server_name=\"SampleServer\"\n    def __init__(self):\n        pass\n    \n    def set_uuid(self,new_uuid):\n        self.uuid=new_uuid\n        pass\n    \n    def set_server_name(self,newname):\n        self.server_name=newname\n        pass\n    \n    def init_service(self):\n        self.server_sock=BluetoothSocket(RFCOMM)\n        self.server_sock.bind(('',1))\n        self.server_sock.listen(1)\n        self.port=self.server_sock.getsockname()[1]\n        print(\"\u521d\u59cb\u5316\u5b8c\u6210\uff0c\u521d\u59cb\u5316\u4fe1\u606f\uff1a\")\n        print(\"  uuid\uff1a\\t\",self.uuid)\n        print(\"  server_name\uff1a\\t\",self.server_name)\n        print(\"\\n\")\n        pass\n        \n    \n    def start_service(self):\n        advertise_service(self.server_sock, self.server_name,service_id = self.uuid,service_classes = [self.uuid, SERIAL_PORT_CLASS],profiles = [SERIAL_PORT_PROFILE])\n        print(\"\u7b49\u5f85\u8bbe\u5907\u63a5\u5165\uff08\u901a\u9053\",self.port,\"\uff09\")\n        self.client_sock, self.client_info = self.server_sock.accept()\n        print(\"\u8bbe\u5907\u5df2\u8fde\u63a5\uff0c\u5bf9\u65b9\u4e3a\u5730\u5740\u4e3a\uff1a[\",self.client_info[0],\"]\uff0c\u8fde\u63a5\u7aef\u53e3\u4e3a\uff1a\",self.client_info[1])\n        try:\n            while True:\n                data=self.client_sock.recv(1024)\n                if len(data)==0: break\n                print(\"\u6536\u5230\u6d88\u606f\uff1a%s\"%data)\n                self.client_sock.send(data)\n        except IOError:\n            print(\"\u51fa\u73b0\u4e86\u5f02\u5e38\uff01\")\n            pass\n        print(\"\u901a\u4fe1\u7ed3\u675f\")\n        self.client_sock.close()\n        self.server_sock.close()\n\n\ncmd='sudo hciconfig hci0 piscan'\nsubprocess.check_output(cmd,shell=True)\nss=service()\nss.init_service()\nss.start_service()<\/pre>\n\n\n\n<p>\u8fd9\u6bb5\u4ee3\u7801\u4e0d\u662f\u6211\u5199\u7684\uff0c\u662f\u4ee3\u7801\u6ce8\u91ca\u91cc\u5934\u7684\u54e5\u4eec\uff0c\ud83d\ude4f<\/p>\n\n\n\n<pre class=\"wp-block-syntaxhighlighter-code\">sudo python3 bluetooth_joger.py<\/pre>\n\n\n\n<p>\u7528\u624b\u673a\u627e\u4e2a\u84dd\u7259\u6d4b\u8bd5\u5de5\u5177\uff0c\u8fde\u8fdb\u6765\u5c31\u53ef\u4ee5\u6d4b\u8bd5\u4e86\uff01<\/p>\n\n\n\n<p class=\"has-text-color has-small-font-size has-cyan-bluish-gray-color\"><em>\u8bf4\u4e2a\u9898\u5916\u8bdd\uff0c\u4e4b\u524d\u6d4b\u8bd5\u7684\u65f6\u5019\uff0c\u8fd0\u884c\u4e86\u65e0\u6570\u6b21\u90fd\u4e0d\u6210\u529f\uff0c\u5e76\u4e14\u53cd\u590d\u62a5import bluetooth\u8fd9\u53e5\uff0c\u627e\u4e0d\u5230bluetooth\u5e93\uff0c\u91cd\u88c5\u4e86\u65e0\u6570\u6b21\uff0c\u6700\u540e\u53d1\u73b0\u662f\u56e0\u4e3a\u6211\u628a\u8fd9\u6bb5\u6d4b\u8bd5\u7684\u4ee3\u7801\u547d\u540d\u6210\u4e86bluetooth.py\u4e86\u2026\u2026\u4f30\u8ba1\u4ed6\u662f\u81ea\u5df1\u627e\u81ea\u5df1\u53bb\u4e86\u2026\u2026\u6655\u6b7b\uff01<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9996\u5148\uff0c\u5b89\u88c5\u57fa\u672c\u7684\u84dd\u7259\u5e93 \u4fee\u6539\/etc\/systemd\/system\/bluetooth.target.want&hellip; <a href=\"http:\/\/cf3b5.com\/blog\/?p=90\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb <span class=\"screen-reader-text\">Raspberry Pi\u7684\u84dd\u7259\u5f00\u53d1\uff08\u4e00\uff09<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[6],"tags":[],"_links":{"self":[{"href":"http:\/\/cf3b5.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/90"}],"collection":[{"href":"http:\/\/cf3b5.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/cf3b5.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/cf3b5.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/cf3b5.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=90"}],"version-history":[{"count":6,"href":"http:\/\/cf3b5.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/90\/revisions"}],"predecessor-version":[{"id":116,"href":"http:\/\/cf3b5.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/90\/revisions\/116"}],"wp:attachment":[{"href":"http:\/\/cf3b5.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=90"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/cf3b5.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=90"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/cf3b5.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=90"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}