更新记录
1.0.1(2024-05-16)
修改readme
1.0.0(2024-05-16)
初始版本发布
平台兼容性
Vue2 | Vue3 |
---|---|
√ | √ |
App | 快应用 | 微信小程序 | 支付宝小程序 | 百度小程序 | 字节小程序 | QQ小程序 |
---|---|---|---|---|---|---|
HBuilderX 4.13,Android:4.4,iOS:不支持,HarmonyNext:不确定 | × | × | × | × | × | × |
钉钉小程序 | 快手小程序 | 飞书小程序 | 京东小程序 |
---|---|---|---|
× | × | × | × |
H5-Safari | Android Browser | 微信浏览器(Android) | QQ浏览器(Android) | Chrome | IE | Edge | Firefox | PC-Safari |
---|---|---|---|---|---|---|---|---|
× | × | × | × | × | × | × | × | × |
dele-sunmi-btprinter
dele-sunmi-btprinter
是商米外置打印机的蓝牙操作插件
加载插件
import { useBtprint } from '@/uni_modules/dele-sunmi-btprinter';
import { OpenBluetoothAdapterOptions,GetBluetoothDevicesOptions,CommonOptions,StopBluetoothDevicesDiscoveryOptions,StartBluetoothDevicesDiscoveryOptions } from "@/uni_modules/dele-sunmi-btprinter/utssdk/interface";
import { BluetoothManager } from "@/uni_modules/dele-sunmi-btprinter/";
import { InitConfig } from '@/uni_modules/dele-sunmi-btprinter/utssdk/interface';
初始化
const btPrint : BluetoothManager = useBtprint({
/**
* 是否请求地理位置权限,可选参数
*/
needLocation: false,
/**
* 是否需要在后台访问位置信息,可选参数
*/
accessBackgroundLocation: false
} as InitConfig);
var btDeviceId = "";
var btDeviceName = "";
初始化蓝牙
btPrint.openBluetoothAdapter({
success: ((res) => {
console.log("初始化蓝牙成功" + res.errMsg);
console.log(JSON.stringify(res));
}), fail: ((res) => {
console.log("初始化蓝牙失败" + res.errMsg);
console.log(JSON.stringify(res));
}), complete: ((res) => {
console.log("初始化蓝牙结束");
console.log(JSON.stringify(res));
})
} as OpenBluetoothAdapterOptions);
蓝牙扫描
btPrint.onBluetoothDeviceFound((res) => {
console.log(res);
console.log('开始监听寻找到新设备的事件');
this.getBluetoothDevices();
});
btPrint. startBluetoothDevicesDiscovery({
success: ((res) => {
console.log('开始搜索蓝牙设备' + res.errMsg);
}),
fail: (e) => {
console.log(e);
console.log('搜索蓝牙设备失败,错误码' + e.errCode);
}
} as StartBluetoothDevicesDiscoveryOptions);
匹配蓝牙
var deviceId = btDeviceId;
console.log("conne id -->"+deviceId)
btPrint.createBond({
deviceId,
success: (res) => {
console.log(res);
console.log('匹配蓝牙返回:' + res.errMsg);
},
fail: (err) => {
}
} as CommonOptions);
连接蓝牙
var deviceId = btDeviceId;
console.log("conne id -->"+deviceId)
btPrint.createConn({
deviceId,
success: (res) => {
console.log(res);
console.log('连接蓝牙返回:' + res.errMsg);
},
fail: (err) => {
console.error("连接失败")
}
} as CommonOptions);
打印测试
btPrint.sendData("这里商米打印机测试1\n这里商米打印机测试2\n这里商米打印机测试3\n这里商米打印机测试4\n这里商米打印机测试5\n这里商米打印机测试6\n")
获取设备列表
getBluetoothDevices:function(){
btPrint.getBluetoothDevices({
success: ((res) => {
console.log(res.devices.length);
res.devices.map((item) => {
const {
deviceId,
name,
localName,
RSSI
} = item;
if(item.name.startsWith("CloudPrint")){
console.error("匹配的蓝牙名称-->"+item.name)
btDeviceName = item.name
btDeviceId = item.deviceId
//关闭搜索
this.stopBluetoothDevicesDiscovery()
}
// 其他业务逻辑
});
})
} as GetBluetoothDevicesOptions);
},
停止搜索蓝牙设备
stopBluetoothDevicesDiscovery:function(){
console.log("stopBluetoothDevicesDiscovery funciton")
btPrint.stopBluetoothDevicesDiscovery({
success: ((res) => {
console.log('停止搜索蓝牙设备' + res.errMsg);
})
} as StopBluetoothDevicesDiscoveryOptions);
更多插件使用请联系作者订阅,可以定制你想要的样式,QQ:177478191