Este equipo de expertos despúes de muchos días de trabajo y de juntar de datos, hallamos la respuesta, queremos que resulte útil para ti para tu plan.
Solución:
Puede usar “nodo-bluetooth” para enviar y recibir datos desde y hacia un dispositivo, respectivamente. Este es un código de muestra: –
const bluetooth = require('node-bluetooth');
// create bluetooth device instance
const device = new bluetooth.DeviceINQ();
device
.on('finished', console.log.bind(console, 'finished'))
.on('found', function found(address, name)
console.log('Found: ' + address + ' with name ' + name);
device.findSerialPortChannel(address, function(channel)
console.log('Found RFCOMM channel for serial port on %s: ', name, channel);
// make bluetooth connect to remote device
bluetooth.connect(address, channel, function(err, connection)
if (err) return console.error(err);
connection.write(new Buffer('Hello!', 'utf-8'));
);
);
// make bluetooth connect to remote device
bluetooth.connect(address, channel, function(err, connection)
if (err) return console.error(err);
connection.on('data', (buffer) =>
console.log('received message:', buffer.toString());
);
connection.write(new Buffer('Hello!', 'utf-8'));
);
).inquire();
Busca el nombre del dispositivo dado en la variable “dispositivo”.
Pruebe la biblioteca noble. Así obtengo información sobre mi dispositivo Xiaomi Mi Band 3:
const arrayBufferToHex = require('array-buffer-to-hex')
const noble = require('noble')
const DEVICE_INFORMATION_SERVICE_UUID = '180a'
noble.on('stateChange', state =>
console.log(`State changed: $state`)
if (state === 'poweredOn')
noble.startScanning()
)
noble.on('discover', peripheral =>
console.log(`Found device, name: $peripheral.advertisement.localName, uuid: $peripheral.uuid`)
if (peripheral.advertisement.localName === 'Mi Band 3')
noble.stopScanning()
peripheral.on('connect', () => console.log('Device connected'))
peripheral.on('disconnect', () => console.log('Device disconnected'))
peripheral.connect(error =>
peripheral.discoverServices([DEVICE_INFORMATION_SERVICE_UUID], (error, services) =>
console.log(`Found service, name: $services[0].name, uuid: $services[0].uuid, type: $services[0].type`)
const service = services[0]
service.discoverCharacteristics(null, (error, characteristics) =>
characteristics.forEach(characteristic =>
console.log(`Found characteristic, name: $characteristic.name, uuid: $characteristic.uuid, type: $characteristic.type, properties: $characteristic.properties.join(',')`)
)
characteristics.forEach(characteristic =>
if (characteristic.name === 'System ID' )
)
)
)
)
Puedes usar nodo-ble una biblioteca Node.JS que aprovecha D-Bus y evita los enlaces de C++. https://github.com/chrvadala/node-ble
Aquí un ejemplo básico
async function main ()
const bluetooth, destroy = createBluetooth()
// get bluetooth adapter
const adapter = await bluetooth.defaultAdapter()
await adapter.startDiscovery()
console.log('discovering')
// get device and connect
const device = await adapter.waitDevice(TEST_DEVICE)
console.log('got device', await device.getAddress(), await device.getName())
await device.connect()
console.log('connected')
const gattServer = await device.gatt()
// read write characteristic
const service1 = await gattServer.getPrimaryService(TEST_SERVICE)
const characteristic1 = await service1.getCharacteristic(TEST_CHARACTERISTIC)
await characteristic1.writeValue(Buffer.from('Hello world'))
const buffer = await characteristic1.readValue()
console.log('read', buffer, buffer.toString())
// subscribe characteristic
const service2 = await gattServer.getPrimaryService(TEST_NOTIFY_SERVICE)
const characteristic2 = await service2.getCharacteristic(TEST_NOTIFY_CHARACTERISTIC)
await characteristic2.startNotifications()
await new Promise(done =>
characteristic2.once('valuechanged', buffer =>
console.log('subscription', buffer)
done()
)
)
await characteristic2.stopNotifications()
destroy()
Al final de la artículo puedes encontrar los comentarios de otros creadores, tú aún tienes la habilidad mostrar el tuyo si lo deseas.
¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)