yuan
8 天以前 ce259180a1a76db6a7e89d651dc3eb1f49932202
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// 精臣打印机原生插件。插件由 DCloud 账号绑定,账号未购买/未打入基座时不能抛错,
// 否则页面一加载就白屏,所以这里做容错加载,缺失时打印功能给出提示而不是崩溃。
let jcapi = null;
let pluginMissing = false;
try {
  jcapi = uni.requireNativePlugin("JCSDK-JCApiModule");
  if (!jcapi) {
    pluginMissing = true;
  }
} catch (e) {
  pluginMissing = true;
}
 
const PLUGIN_MISSING_MSG = "未检测到精臣打印插件,请确认打包时已购买并勾选该原生插件";
 
function requirePlugin() {
  if (pluginMissing || !jcapi) {
    uni.showToast({ title: PLUGIN_MISSING_MSG, icon: "none", duration: 3000 });
    return null;
  }
  return jcapi;
}
 
let state = {
  connectedDevice: null,
  connectionStatus: "disconnected", // disconnected | connecting | connected
  listeners: [],
};
 
/** 通知所有订阅页面 */
function notify() {
  state.listeners.forEach(fn => fn({
    connectedDevice: state.connectedDevice,
    connectionStatus: state.connectionStatus,
  }));
}
 
/** 页面订阅蓝牙状态 */
function onChange(fn) {
  state.listeners.push(fn);
  fn({
    connectedDevice: state.connectedDevice,
    connectionStatus: state.connectionStatus,
  });
}
 
/** 初始化(全局只需一次) */
function init() {
  const api = requirePlugin();
  if (!api) return;
 
  api.initSDK();
 
  const saved = uni.getStorageSync("bluetoothConnection");
  if (saved) {
    state.connectedDevice = saved;
    state.connectionStatus = "connected";
  }
 
  // 断开监听
  api.didReadPrintErrorInfo((r) => {
    if (r.code === 23) {
      state.connectedDevice = null;
      state.connectionStatus = "disconnected";
      uni.removeStorageSync("bluetoothConnection");
      notify();
    }
  });
}
 
/** 搜索设备 */
function searchDevices() {
  return new Promise((resolve, reject) => {
    const api = requirePlugin();
    if (!api) {
      return reject(new Error(PLUGIN_MISSING_MSG));
    }
    uni.openBluetoothAdapter({
      success() {
        api.getBluetoothDevices(list => {
          resolve(list || []);
        });
      },
      fail: reject
    });
  });
}
 
/** 连接设备 */
function connect(device) {
  return new Promise((resolve, reject) => {
    const api = requirePlugin();
    if (!api) {
      return reject(new Error(PLUGIN_MISSING_MSG));
    }
    state.connectionStatus = "connecting";
    notify();
 
    api.openPrinterByDevice({
      address: device.address,
      name: device.name,
      deviceType: 0,
    }, (r) => {
      if (r.code === 0) {
        state.connectedDevice = device;
        state.connectionStatus = "connected";
        uni.setStorageSync("bluetoothConnection", device);
        notify();
        resolve(device);
      } else {
        state.connectionStatus = "disconnected";
        notify();
        reject(r);
      }
    });
  });
}
 
function isConnected() {
  return state.connectionStatus === "connected";
}
 
function getCurrentDevice() {
  return state.connectedDevice;
}
 
export default {
  init,
  onChange,
  searchDevices,
  connect,
  isConnected,
  getCurrentDevice,
};