您可以使用该工具通过串口进行通信。我从未使用过此工具,但仅提供其作为参考,因为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');
});
btSerial.close();
}, function() {
console.log('found nothing');
});
});
btSerial.inquire();
基本的服务器使用(仅适用于Linux)
var server = new(require('bluetooth-serial-port')).BluetoothSerialPortServer();
var CHANNEL = 10;
var UUID = '38e851bc-7144-44b4-9cd8-80549c6f2912';
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} );