您可以通過使用此工具串口通信。我從來沒有使用這個工具,但提供這隻作爲一個參考,因爲android是建立在一個Linux內核這可能工作。請注意,這些示例與文檔相同。
https://github.com/eelcocramer/node-bluetooth-serial-port
基本客戶端使用
var btSerial = new (require('bluetooth-serial-port')).BluetoothSerialPort();
btSerial.on('found', function(address, name) {
btSerial.findSerialPortChannel(address, function(channel) {
btSerial.connect(address, channel, function() {
console.log('connected');
btSerial.write(new Buffer('my data', 'utf-8'), function(err, bytesWritten) {
if (err) console.log(err);
});
btSerial.on('data', function(buffer) {
console.log(buffer.toString('utf-8'));
});
}, function() {
console.log('cannot connect');
});
// close the connection when you're ready
btSerial.close();
}, function() {
console.log('found nothing');
});
});
btSerial.inquire();
基本服務器使用(僅在Linux上)
var server = new(require('bluetooth-serial-port')).BluetoothSerialPortServer();
var CHANNEL = 10; // My service channel. Defaults to 1 if omitted.
var UUID = '38e851bc-7144-44b4-9cd8-80549c6f2912'; // My own service UUID. Defaults to '1101' if omitted
server.listen(function (clientAddress) {
console.log('Client: ' + clientAddress + ' connected!');
server.on('data', function(buffer) {
console.log('Received data from client: ' + buffer);
// ...
console.log('Sending data to the client');
server.write(new Buffer('...'), function (err, bytesWritten) {
if (err) {
console.log('Error!');
} else {
console.log('Send ' + bytesWritten + ' to the client!');
}
});
});
}, function(error){
console.error("Something wrong happened!:" + error);
}, {uuid: UUID, channel: CHANNEL});
請不要只是發佈一個鏈接到一些工具或庫作爲一個答案。至少在答案中演示[它如何解決問題](http://meta.stackoverflow.com/a/251605)。 –