通过网络蓝牙传输文件

Transmitting a File Over Web Bluetooth

我正在开发一个必须通过蓝牙传输文件的网络应用程序。这可能吗?如果可以,我将如何去做?示例代码将不胜感激。我在网上找不到任何好的文档。此外,它必须能够 运行 在移动设备上。我是 JavaScript 的新手。谢谢

虽然我强烈建议初学者不要使用蓝牙(或者通常在这个时候,因为它是许多浏览器的 WIP):

Web Bluetooth is NOT available for any mobile browser except Chrome & Opera for Android and Samsung Browser

最好的资源可能是 MDN and the specification

大致如下:

// Discovery options match any devices advertising:
// . The standard heart rate service.
// . Both 16-bit service IDs 0x1802 and 0x1803.
// . A proprietary 128-bit UUID service c48e6067-5295-48d3-8d5c-0395f61792b1.
// . Devices with name "ExampleName".
// . Devices with name starting with "Prefix".
//
// And enables access to the battery service if devices
// include it, even if devices do not advertise that service.
let options = {
  filters: [
    {services: ['<Your Device UUID>']}
  ]
}

navigator.bluetooth.requestDevice(options).then(function(device) {
  console.log('Name: ' + device.name);
  return device.gatt.getPrimaryService();
})
.then(function(service) {
  return service.getCharacheteristic('<Your Charachteristic UUID>');
})
.then(function(characteristic) {
  // Do something with the characteristic
})
.catch(function(error) {
  console.log("Something went wrong. " + error);
});