方式一
<p>开发工具:</p>
<p>[HBudiler](<a href="https://dcloud.io/runtime.html#toDownload">https://dcloud.io/runtime.html#toDownload</a> "HBudiler")</p>
<p><img src="http://doc.szzkc.com/Public/Uploads/2018-03-08/5aa10a3b30bc9.png" alt="" />
<img src="https://www.showdoc.com.cn/server/api/attachment/visitFile?sign=c118a20d4af4ad5904ec9c56566cdd58&amp;file=file.png" alt="" /></p>
<p>蓝牙打印参考:
& 以下代码仅供测试参考,具体打印流程请跟进实际需求调整
& JS方式实现打印,操作非常简单,代码如下:</p>
<pre><code> function print(mac_address) {
if (!mac_address) {
mui.toast('请选择蓝牙打印机');
return;
}
main = plus.android.runtimeMainActivity();
BluetoothAdapter = plus.android.importClass(&quot;android.bluetooth.BluetoothAdapter&quot;);
UUID = plus.android.importClass(&quot;java.util.UUID&quot;);
uuid = UUID.fromString(&quot;00001101-0000-1000-8000-00805F9B34FB&quot;);
BAdapter = BluetoothAdapter.getDefaultAdapter();
device = BAdapter.getRemoteDevice(mac_address);
plus.android.importClass(device);
bluetoothSocket = device.createInsecureRfcommSocketToServiceRecord(uuid);
plus.android.importClass(bluetoothSocket);
if (!bluetoothSocket.isConnected()) {
console.log('检测到设备未连接,尝试连接....');
bluetoothSocket.connect();
}
console.log('设备已连接');
if (bluetoothSocket.isConnected()) {
var outputStream = bluetoothSocket.getOutputStream();
plus.android.importClass(outputStream);
var string = &quot;打印测试\r\n&quot;;
var bytes = plus.android.invoke(string, 'getBytes', 'gbk');
outputStream.write(bytes);
outputStream.flush();
device = null //这里关键
bluetoothSocket.close(); //必须关闭蓝牙连接否则意外断开的话打印错误
}
}
//页面调用
&lt;input id=&quot;btnPrint&quot; type=&quot;button&quot; value=&quot;打印&quot; onclick=&quot;print('00:11:22:33:44:55')&quot; /&amp;</code></pre>