【发布时间】:2014-07-28 04:29:10
【问题描述】:
一个简单的想法。我想在通过 AJAX 调用时提供的网络浏览器中显示计算机范围内的蓝牙设备(通过蓝牙串行端口 NodeJS 包使用蓝牙加密狗)。
我遇到的问题是,使用以下脚本时,我无法同时扫描设备并提供网站服务。您能否解释一下这里的工作流程以及为什么/什么是纠正问题的好方向?
var http = require("http"),
url = require("url"),
path = require("path"),
fs = require("fs"),
port = process.argv[2] || 8888,
btSerial = new (require('bluetooth-serial-port')).BluetoothSerialPort();
btSerial.inquire();
// Get the devices
var devices = [];
btSerial.on('found', function(address, name) {
if(typeof devices[address] === 'undefined'){
devices.push({'address': address,'name': name});
console.log("Device Found: "+address+" which is named: "+name);
}
});
// Keep searching
btSerial.on('finished', function() {
console.log("Done searching");
console.log(devices);
//btSerial.inquire();
});
http.createServer(function(request, response) {
var uri = url.parse(request.url).pathname,
filename = path.join(process.cwd(), uri);
if (uri == '/devices') {
response.writeHead(200, {"Content-Type": "application/json"});
response.write(JSON.stringify(devices));
response.end();
return;
}
fs.exists(filename, function(exists) {
if(!exists) {
response.writeHead(404, {"Content-Type": "text/plain"});
response.write("404 Not Found\n");
response.end();
return;
}
if (fs.statSync(filename).isDirectory()) filename += 'public/index.html';
fs.readFile(filename, "binary", function(err, file) {
if(err) {
response.writeHead(500, {"Content-Type": "text/plain"});
response.write(err + "\n");
response.end();
return;
}
response.writeHead(200);
response.write(file, "binary");
response.end();
});
});
}).listen(parseInt(port, 10));
console.log("Static file server running at\n => http://localhost:" + port + "/\nCTRL + C to shutdown");
【问题讨论】:
-
您是否尝试过使用
setInterval在后台定期调用btSerial.inquire?
标签: javascript node.js http bluetooth