UNPKG

mtl-js-sdk

Version:

ynf-fw-mtl-api

145 lines (131 loc) 3.62 kB
"use strict"; function Plugin(options, exports = {}) { const { platform } = options || { platform: window.mtl.platform }; const FAIL_CODE = 1; const requireName = 'ble' function successCallBack(object = {}) { object.success && object.success({}); object.complete && object.complete({}); } function getPeripheral(obj = {}) { if (platform === "APIAndroid" || platform === "APIIos") { execPermissionBridge('getPeripheral', obj) } else { successCallBack(obj) } } function openBluetooth(obj = {}) { if (platform === "APIAndroid") { execPermissionBridge('openBluetooth', obj) } else { successCallBack(obj) } } function initManager(obj = {}) { if (platform === "APIAndroid" || platform === "APIIos") { execPermissionBridge('initManager', obj) } else { successCallBack(obj) } } function scan(obj = {}) { if (platform === "APIAndroid" || platform === "APIIos") { execPermissionBridge('scan', obj) } else { successCallBack(obj) } } function stopScan(obj = {}) { if (platform === "APIAndroid" || platform === "APIIos") { execPermissionBridge('stopScan', obj) obj.success && obj.success({state: true}) obj.complete && obj.complete({state: true}) } else { successCallBack(obj) } } function isScanning(obj = {}) { if (platform === "APIAndroid" || platform === "APIIos") { execPermissionBridge('isScanning', obj) } else { successCallBack(obj) } } // 内部方法 function execPermissionBridge(method, object) { if (platform === "APIAndroid") { mtl.requestPermission({ list: ["location"], success: function() { if (method === 'openBluetooth') { execApiBridge(method, object) } else { execOpenBlueBridge(method, object) } }, fail: function(err) { object.fail && object.fail(err) object.complete && object.complete(err) }, }); } else { execApiBridge(method, object) } } function execOpenBlueBridge(method, object) { mtl.execPluginBridge({ method: 'openBluetooth', requireName, success: function(res){ if (res) { if (method === 'initManager') { execApiBridge(method, object) } else { execInitManagerBridge(method, object) } } else { let error = res || {} error.message = 'permission not enabled' error.code = FAIL_CODE object.fail && object.fail(error) object.complete && object.complete(error) } }, fail: object.fail }); } function execInitManagerBridge(method, object) { mtl.execPluginBridge({ method: 'initManager', requireName, success: function(res){ if (res && res.state == 'poweredOn') { execApiBridge(method, object) } else { let error = res || {} error.message = 'permission not enabled' error.code = FAIL_CODE object.fail && object.fail(error) object.complete && object.complete(error) } }, fail: object.fail }); } function execApiBridge(method, object) { mtl.execPluginBridge({ ...object, method, requireName }) } let apiMethods = { getPeripheral, openBluetooth, initManager, scan, stopScan, isScanning }; const methods = apiMethods; exports.module = methods; exports.symbolPath = "ble"; return exports; } export default Plugin;