Web蓝牙打印Demo以及Webbluetooth的用法

释放双眼,带上耳机,听听看~!

最近项目需要连接蓝牙进行打印,一般连接连接蓝牙进行打印的都是通过APP,我们之前的项目也是通过Websoket发送打印数据,APP接收后,进行连接蓝牙打印,但是这样的方式就是不便捷的,对于用户来说,在下载一个APP,在进行打印,是比较麻烦的,所以找到了Webbluetooth,也是参考了很多文章。主要逻辑就是使用

 navigator.bluetooth.requestDevice({
          filters: [{
            namePrefix: 'CS3',
            acceptAllDevices: true,
           services: ['0000fee7-0000-1000-8000-00805f9b34fb'],

          }]
        })
                .then(device => {
                  console.log('> Found ' + device.name);
                  console.log('Connecting to GATT Server...');
                  return device.gatt.connect();
                })
                .then(server => server.getPrimaryService("0000fee7-0000-1000-8000-00805f9b34fb"))
                .then(service => service.getCharacteristic("0000fec7-0000-1000-8000-00805f9b34fb"))
                .then(characteristic => {
                  // Cache the characteristic
                  printCharacteristic = characteristic;
                 // console.log(printCharacteristic);
                  sendPrinterData();

上面的services就是打印服务的UUID,如何查找呢?主要通过:在浏览器中:输入chrome://bluetooth-internals/#adapter

就可以扫描到:

Web蓝牙打印Demo以及Webbluetooth的用法

这里就是services:

Web蓝牙打印Demo以及Webbluetooth的用法
demo:https://www.ylesb.com/demos/bluetooth-printer/index.html
就是打印服务的UUID,替换上面,然后打印数据函数自己定义好就可以了。最后附上一些参考学习的文章以及项目。如果你比较感兴趣也可以加入我们的QQ群进群讨论学习。
Web蓝牙打印Demo以及Webbluetooth的用法
参考:

https://googlechrome.github.io/samples/web-bluetooth/index.html

https://www.jianshu.com/p/d991f0fdec63

https://webbluetoothcg.github.io/demos/bluetooth-printer/

https://webbluetoothcg.github.io/demos/

https://github.com/WebBluetoothCG/demos

https://github.com/Sun-Target/print

https://www.cnblogs.com/jam-liu/p/14654548.html

 

给TA打赏
共{{data.count}}人
人已打赏
个人项目

回忆儿时游戏-井字棋(完整代码)

2021-1-13 17:22:50

个人项目

SpringCloud-Alibaba环境搭建以及注意事项。

2022-1-18 11:39:49

版权声明 1 本网站名称:怀特博客
2 本站永久网址:https://www.ylesb.com
3 本网站的文章部分内容可能来源于网络,仅供大家学习与参考,如有侵权,请联系站长QQ466534434进行删除处理。
4 本站一切资源不代表本站立场,并不代表本站赞同其观点和对其真实性负责。
5 本站一律禁止以任何方式发布或转载任何违法的相关信息,访客发现请向站长举报
6 本站资源大多存储在云盘,如发现链接失效,请联系我们我们会第一时间更新。
0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
阿里云WordPress

Optimized by WPJAM Basic