检查!在Windows上,安装使用BLE操作microbit的npm包`bbc-microbit`

你好,我是@dz_,我叫大平和美。

序幕 – 开篇

听说可以通过Node-RED通过蓝牙控制microbit,我尝试在Windows上进行尝试,但在安装所需的bbc-microbit包时遇到了问题,因此留下了这个备忘录。

不仅限于Windows,其他操作系统也需要构建环境,所以如果遇到类似错误的人们可以参考原因和解决方法。

20180123_232248374_iOS.jpg

试行环境的补充

我正在使用Windows 10操作系统(Surface Pro 4)。

错误信息 (错误内容)

安装bbc-microbit后,进行node-gyp重建时会出现以下错误。

因为是在 Node-RED 中使用,所以我执行了 `npm install` 命令在路径 C:\Users\<ユーザ名>\.node-red 中。

npm install bbc-microbit
> usb@1.3.1 install C:\Users\<ユーザー名>\.node-red\node_modules\usb
> node-pre-gyp install --fallback-to-build

[usb] Success: "C:\Users\<ユーザー名>\.node-red\node_modules\usb\src\binding\usb_bindings.node" is installed via remote

> bluetooth-hci-socket@0.5.1 install C:\Users\<ユーザー名>\.node-red\node_modules\bluetooth-hci-socket
> node-gyp rebuild


C:\Users\<ユーザー名>\.node-red\node_modules\bluetooth-hci-socket>if not defined npm_config_node_gyp (node "C:\Program Files\nodejs\node_modules\npm\node_modules\npm-lifecycle\node-gyp-bin\\..\..\node_modules\node-gyp\bin\node-gyp.js" rebuild )  else (node "C:\Program Files\nodejs\node_modules\npm\node_modules\node-gyp\bin\node-gyp.js" rebuild )
gyp ERR! configure error
gyp ERR! stack Error: Can't find Python executable "C:\Python36\python.EXE", you can set the PYTHON env variable.
gyp ERR! stack     at PythonFinder.failNoPython (C:\Program Files\nodejs\node_modules\npm\node_modules\node-gyp\lib\configure.js:483:19)
gyp ERR! stack     at PythonFinder.<anonymous> (C:\Program Files\nodejs\node_modules\npm\node_modules\node-gyp\lib\configure.js:508:16)
gyp ERR! stack     at C:\Program Files\nodejs\node_modules\npm\node_modules\graceful-fs\polyfills.js:284:29
gyp ERR! stack     at FSReqWrap.oncomplete (fs.js:152:21)
gyp ERR! System Windows_NT 10.0.16299
gyp ERR! command "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\node_modules\\node-gyp\\bin\\node-gyp.js" "rebuild"
gyp ERR! cwd C:\Users\<ユーザー名>\.node-red\node_modules\bluetooth-hci-socket
gyp ERR! node -v v8.9.4
gyp ERR! node-gyp -v v3.6.2
gyp ERR! not ok
npm WARN node-red-project@0.0.1 No repository field.
npm WARN node-red-project@0.0.1 No license field.
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: xpc-connection@0.1.4 (node_modules\xpc-connection):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for xpc-connection@0.1.4: wanted {"os":"darwin","arch":"any"} (current: {"os":"win32","arch":"x64"})
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: bluetooth-hci-socket@0.5.1 (node_modules\bluetooth-hci-socket):
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: bluetooth-hci-socket@0.5.1 install: `node-gyp rebuild`
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: Exit status 1

+ bbc-microbit@0.2.0
added 6 packages in 12.503s

在这种情况下,bbc-microbit 已经被安装,但 bluetooth-hci-socket 没有被安装,因此尝试使用时会引发以下错误。

Error: Cannot find module 'bluetooth-hci-socket'

原因与解决措施 yǔ

这个 bbc-microbit 依赖于 node-gyp 包,该包需要一个包含 Python 2.7 的构建环境。不支持 Python 3系列。

BBC微比特 > 诺贝尔 > Node蓝牙HCI套接字 > Node编译系统

sandeepmistry/node-bbc-microbit

Control a BBC micro:bit from Node.js using BLE

sandeepmistry/noble

A Node.js BLE (Bluetooth Low Energy) central module

sandeepmistry/node-bluetooth-hci-socket

Bluetooth HCI socket binding for Node.js

nodejs/node-gyp

Node.js native addon build tool

在Windows上,必须满足nodejs/node-gyp的要求。

可以有两种方法安装,一种是手动安装,另一种是使用windows-build-tools进行批量安装。

如果使用Windows Build Tools,需要以管理员权限打开PowerShell,并执行以下操作,似乎会安装Python 2.7和Visual Studio Build Tools。

PS C:\WINDOWS\system32> npm install --global --production windows-build-tools
> windows-build-tools@1.4.2 postinstall C:\Users\<ユーザー名>\AppData\Roaming\npm\node_modules\windows-build-tools
> node ./lib/index.js

Downloading BuildTools_Full.exe
Downloading python-2.7.14.amd64.msi
[>                                            ] 0.0% (0 B/s)
Downloaded python-2.7.14.amd64.msi. Saved to C:\Users\<ユーザー名>\.windows-build-tools\python-2.7.14.amd64.msi.
Starting installation...
Launched installers, now waiting for them to finish.
This will likely take some time - please be patient!
Waiting for installers... |Successfully installed Python 2.7
Waiting for installers... |Successfully installed Visual Studio Build Tools.
+ windows-build-tools@1.4.2
added 128 packages in 253.664s

之后,再次安装BBC Microbit时,成功地安装了如下所示的内容。

npm install bbc-microbit
> usb@1.3.1 install C:\Users\<ユーザー名>\.node-red\node_modules\usb
> node-pre-gyp install --fallback-to-build

[usb] Success: "C:\Users\<ユーザー名>\.node-red\node_modules\usb\src\binding\usb_bindings.node" is installed via remote

> bluetooth-hci-socket@0.5.1 install C:\Users\<ユーザー名>\.node-red\node_modules\bluetooth-hci-socket
> node-gyp rebuild


C:\Users\<ユーザー名>\.node-red\node_modules\bluetooth-hci-socket>if not defined npm_config_node_gyp (node "C:\Program Files\nodejs\node_modules\npm\node_modules\npm-lifecycle\node-gyp-bin\\..\..\node_modules\node-gyp\bin\node-gyp.js" rebuild )  else (node "C:\Program Files\nodejs\node_modules\npm\node_modules\node-gyp\bin\node-gyp.js" rebuild )
Building the projects in this solution one at a time. To enable parallel build, please add the "/m" switch.
  win_delay_load_hook.cc
  Generating code
  Finished generating code
  binding.vcxproj -> C:\Users\<ユーザー名>\.node-red\node_modules\bluetooth-hci-socket\build\Release\\binding.node
  binding.vcxproj -> C:\Users\<ユーザー名>\.node-red\node_modules\bluetooth-hci-socket\build\Release\binding.pdb (Full
   PDB)
npm WARN node-red-project@0.0.1 No repository field.
npm WARN node-red-project@0.0.1 No license field.
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: xpc-connection@0.1.4 (node_modules\xpc-connection):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for xpc-connection@0.1.4: wanted {"os":"darwin","arch":"any"} (current: {"os":"win32","arch":"x64"})

+ bbc-microbit@0.2.0
added 79 packages in 26.041s

结语

在此之后,我成功地安装了 Node-RED,并且它也运行正常。不过,遗憾的是我尝试连接蓝牙时失败了。

也许是因为前几天机器掉落并且损坏了,在内部可能有一些问题(屏幕破裂)。更换硬件后,我会再试一次。

广告
将在 10 秒后关闭
bannerAds