{"id":39336,"date":"2023-10-04T20:00:30","date_gmt":"2023-10-04T12:00:30","guid":{"rendered":"https:\/\/www.techphant.cn\/?p=39336"},"modified":"2023-10-04T20:00:30","modified_gmt":"2023-10-04T12:00:30","slug":"ynljly","status":"publish","type":"post","link":"https:\/\/www.techphant.cn\/blog\/39336.html","title":{"rendered":"\u7528Node.js\u8fde\u63a5\u84dd\u7259"},"content":{"rendered":"
\u8fd1\u5e74\u6765\uff0c\u968f\u7740\u84dd\u7259\u6280\u672f\u7684\u53d1\u5c55\uff0c\u8d8a\u6765\u8d8a\u591a\u7684\u8bbe\u5907\u652f\u6301\u65e0\u7ebf\u8fde\u63a5\uff0c\u4f7f\u7528\u84dd\u7259\u6280\u672f\u53ef\u4ee5\u5b9e\u73b0\u5728\u4e0d\u540c\u8bbe\u5907\u4e4b\u95f4\u8fdb\u884c\u6570\u636e\u4f20\u8f93\u3002\u7531\u4e8eNode.js\u5177\u6709\u8de8\u5e73\u53f0\u7684\u7279\u70b9\uff0c\u53ef\u4ee5\u4f7f\u7528Node.js\u6765\u8fde\u63a5\u84dd\u7259\u8bbe\u5907\uff0c\u5b9e\u73b0\u8de8\u8bbe\u5907\u7684\u6570\u636e\u4f20\u8f93\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Node.js\u8fde\u63a5\u84dd\u7259\u8bbe\u5907\uff0c\u4ee5\u53ca\u5982\u4f55\u5728Node.js\u4e2d\u4f7f\u7528\u84dd\u7259\u6280\u672f\u3002<\/p>\n
Node.js\u53ef\u4ee5\u4f7f\u7528Bluetooth\u5e93\u6765\u8fde\u63a5\u84dd\u7259\u8bbe\u5907\u3002Bluetooth\u5e93\u662f\u4e00\u4e2a\u57fa\u4e8eNode.js\u7684JavaScript\u5e93\uff0c\u53ef\u4ee5\u8ba9\u5f00\u53d1\u8005\u5728Node.js\u4e2d\u4f7f\u7528\u84dd\u7259\u6280\u672f\u3002Bluetooth\u5e93\u652f\u6301\u591a\u79cd\u84dd\u7259\u8bbe\u5907\uff0c\u53ef\u4ee5\u8ba9\u5f00\u53d1\u8005\u5728Node.js\u4e2d\u4f7f\u7528\u84dd\u7259\u6280\u672f\u6765\u8fde\u63a5\u4e0d\u540c\u7684\u8bbe\u5907\u3002<\/p>\n
\u8981\u4f7f\u7528Bluetooth\u5e93\uff0c\u9996\u5148\u9700\u8981\u5b89\u88c5Node.js\u3002\u7136\u540e\uff0c\u901a\u8fc7npm\u5b89\u88c5Bluetooth\u5e93\uff0c\u5b89\u88c5\u547d\u4ee4\u5982\u4e0b\uff1a<\/p>\n
npm install bluetooth<\/code><\/pre>\n \u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u6765\u8fde\u63a5\u84dd\u7259\u8bbe\u5907\uff1a<\/p>\n
const Bluetooth = require('bluetooth'); \/\/ \u8fde\u63a5\u84dd\u7259\u8bbe\u5907<\/p>
Bluetooth.connect('00:00:00:00:00:00', (err, connection) => {<\/p>
if (err) {<\/p>
console.error(err);<\/p>
return;<\/p>
}<\/p>
\/\/ \u8fde\u63a5\u6210\u529f<\/p>
console.log('Connected to device!');<\/p>
});<\/p><\/code><\/pre>\n
\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0cconnect\u65b9\u6cd5\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u4e3a\u84dd\u7259\u8bbe\u5907\u7684MAC\u5730\u5740\uff0c\u53ef\u4ee5\u4f7f\u7528\u624b\u673a\u84dd\u7259\u8f6f\u4ef6\u83b7\u53d6\u84dd\u7259\u8bbe\u5907\u7684MAC\u5730\u5740\u3002\u7b2c\u4e8c\u4e2a\u53c2\u6570\u4e3a\u56de\u8c03\u51fd\u6570\uff0c\u5f53\u8fde\u63a5\u84dd\u7259\u8bbe\u5907\u6210\u529f\u65f6\uff0c\u4f1a\u8fd4\u56de\u4e00\u4e2aconnection\u5b9e\u4f8b\uff0c\u53ef\u4ee5\u4f7f\u7528\u8be5\u5b9e\u4f8b\u6765\u8fdb\u884c\u6570\u636e\u4f20\u8f93\u3002<\/p>\n
\u5982\u4f55\u5728Node.js\u4e2d\u4f7f\u7528\u84dd\u7259\u6280\u672f<\/h2>\n
\u5f53\u8fde\u63a5\u84dd\u7259\u8bbe\u5907\u6210\u529f\u540e\uff0c\u5c31\u53ef\u4ee5\u4f7f\u7528connection\u5b9e\u4f8b\u6765\u8fdb\u884c\u6570\u636e\u4f20\u8f93\u3002connection\u5b9e\u4f8b\u63d0\u4f9b\u4e86write\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5411\u84dd\u7259\u8bbe\u5907\u53d1\u9001\u6570\u636e\uff0c\u8be5\u65b9\u6cd5\u53ef\u4ee5\u63a5\u53d7\u4e00\u4e2aBuffer\u5bf9\u8c61\u4f5c\u4e3a\u53c2\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528Buffer\u5bf9\u8c61\u6765\u8868\u793a\u8981\u53d1\u9001\u7684\u6570\u636e\u3002<\/p>\n
connection\u5b9e\u4f8b\u8fd8\u63d0\u4f9b\u4e86on\u65b9\u6cd5\uff0c\u53ef\u4ee5\u63a5\u6536\u84dd\u7259\u8bbe\u5907\u53d1\u9001\u7684\u6570\u636e\u3002on\u65b9\u6cd5\u63a5\u53d7\u4e24\u4e2a\u53c2\u6570\uff0c\u7b2c\u4e00\u4e2a\u53c2\u6570\u4e3a\u4e8b\u4ef6\u540d\u79f0\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u4e3a\u56de\u8c03\u51fd\u6570\uff0c\u5f53\u63a5\u6536\u5230\u84dd\u7259\u8bbe\u5907\u53d1\u9001\u7684\u6570\u636e\u65f6\uff0c\u4f1a\u8c03\u7528\u8be5\u56de\u8c03\u51fd\u6570\uff0c\u5e76\u5c06\u63a5\u6536\u5230\u7684\u6570\u636e\u4f5c\u4e3a\u53c2\u6570\u4f20\u5165\u3002<\/p>\n
\u603b\u7ed3<\/h2>\n
\u672c\u6587\u4ecb\u7ecd\u4e86\u5982\u4f55\u4f7f\u7528Node.js\u8fde\u63a5\u84dd\u7259\u8bbe\u5907\uff0c\u4ee5\u53ca\u5982\u4f55\u5728Node.js\u4e2d\u4f7f\u7528\u84dd\u7259\u6280\u672f\u3002<\/strong>Node.js\u53ef\u4ee5\u4f7f\u7528Bluetooth\u5e93\u6765\u8fde\u63a5\u84dd\u7259\u8bbe\u5907\uff0cBluetooth\u5e93\u652f\u6301\u591a\u79cd\u84dd\u7259\u8bbe\u5907\uff0c\u53ef\u4ee5\u8ba9\u5f00\u53d1\u8005\u5728Node.js\u4e2d\u4f7f\u7528\u84dd\u7259\u6280\u672f\u6765\u8fde\u63a5\u4e0d\u540c\u7684\u8bbe\u5907\u3002\u4f7f\u7528Bluetooth\u5e93\u8fde\u63a5\u84dd\u7259\u8bbe\u5907\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528connection\u5b9e\u4f8b\u6765\u8fdb\u884c\u6570\u636e\u4f20\u8f93\uff0cconnection\u5b9e\u4f8b\u63d0\u4f9b\u4e86write\u65b9\u6cd5\u548con\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5b9e\u73b0\u53d1\u9001\u6570\u636e\u548c\u63a5\u6536\u6570\u636e\u7684\u529f\u80fd\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8fd1\u5e74\u6765\uff0c\u968f\u7740\u84dd\u7259\u6280\u672f\u7684\u53d1\u5c55\uff0c\u8d8a\u6765\u8d8a\u591a\u7684\u8bbe\u5907\u652f\u6301\u65e0\u7ebf\u8fde\u63a5\uff0c\u4f7f\u7528\u84dd\u7259\u6280\u672f\u53ef\u4ee5\u5b9e\u73b0\u5728\u4e0d\u540c\u8bbe\u5907\u4e4b\u95f4\u8fdb\u884c\u6570\u636e\u4f20\u8f93\u3002\u7531\u4e8eNode.js\u5177\u6709\u8de8\u5e73\u53f0\u7684\u7279\u70b9\uff0c\u53ef\u4ee5\u4f7f\u7528Node.js\u6765\u8fde\u63a5\u84dd<\/p>\n","protected":false},"author":1,"featured_media":4132,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"default","ast-global-header-display":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","footnotes":""},"categories":[36],"tags":[305],"class_list":["post-39336","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blog","tag-dzjs"],"_links":{"self":[{"href":"https:\/\/www.techphant.cn\/wp-json\/wp\/v2\/posts\/39336","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.techphant.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.techphant.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.techphant.cn\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.techphant.cn\/wp-json\/wp\/v2\/comments?post=39336"}],"version-history":[{"count":0,"href":"https:\/\/www.techphant.cn\/wp-json\/wp\/v2\/posts\/39336\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.techphant.cn\/wp-json\/wp\/v2\/media\/4132"}],"wp:attachment":[{"href":"https:\/\/www.techphant.cn\/wp-json\/wp\/v2\/media?parent=39336"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techphant.cn\/wp-json\/wp\/v2\/categories?post=39336"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techphant.cn\/wp-json\/wp\/v2\/tags?post=39336"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}