123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217 |
- //已经找到的设备列表
- let foundDevices = [];
- //成功返回编码
- const SUCCESS_CODE = 0;
- const SUCCESS_MSG = "成功";
- //打开适配器失败
- const OPEN_ADAPTER_FAIL_CODE = 1001;
- // const OPEN_ADAPTER_FAIL_MSG = "打开蓝牙适配器失败"
- const OPEN_ADAPTER_FAIL_MSG = "手机蓝牙功能未开启,请开启手机蓝牙功能后重试。"
- const AUTH_OPEN_FAIL_MSG = "蓝牙功能需要用户同意隐私授权协议,请同意后重试。"
-
- //搜索设备失败
- const SCAN_DEVICE_CODE = 1002;
- // const SCAN_DEVICE_MSG = "搜索设备失败"
- const SCAN_DEVICE_MSG = "未检测到蓝牙标签设备,请确认标签设备蓝牙和微信定位权限已正确开启,并将手机靠近设备后重试。"
-
- //连接设备失败
- const CONNECT_FAIL_CODE = 1003;
- const CONNECT_FAIL_MSG = "连接设备失败";
-
- //停止搜索设备失败
- const ERR_STOPSCANBLE_CODE = 1004;
- const ERR_STOPSCANBLE_MSG = "停止搜索设备失败"
-
-
- var scanTimeout = null;
- var status = false
-
-
-
- //开启蓝牙适配器扫描设备
- function ScanDevice(deviceNameAry, needShowList, callback) {
- deviceNameAry = ['obu', 'etc', 'jl', 'td']
- status = false;
- foundDevices = [];
- let result = new Object();
- // wx.closeBluetoothAdapter();
- wx.openBluetoothAdapter({
- success: (res) => {
- console.log("openBluetoothAdapter:success", res);
- wx.startBluetoothDevicesDiscovery({
- allowDuplicatesKey: false,
- // services: ['FEE7'],
- success: (res) => {
- // 10s之后搜索不到即停止搜索
- scanTimeout = setTimeout(function() {
- console.log("StopScanDevice2222222222", status);
- CheckBle((res) => {
- console.log("蓝牙状态", res)
- if (res.errCode !== 10000 && !status) {
- StopScanDevice(result => {
- console.log(
- "StopScanDevice11111111111111",
- result);
- })
- callback({
- code: 10002,
- err_msg: SCAN_DEVICE_MSG
- });
- }
- })
- }, 10000);
- console.log("startBluetoothDevicesDiscovery:success", res);
- wx.onBluetoothDeviceFound(function(res) {
- console.log('蓝牙设备返回', res)
- let devices = res.devices;
- console.log("onBluetoothDeviceFound:", devices && devices[0] &&
- devices[0].deviceName);
- for (let i = devices.length - 1; i >= 0; i--) {
- let isHave = false;
- for (let j = 0; j < foundDevices.length; j++) {
- if (devices[i].deviceId === foundDevices[j].deviceId) {
- isHave = true;
- break
- }
- }
- if (!isHave) {
- let device = devices[i];
- for (let deviceName of deviceNameAry) {
- let boolethName = device.localName || device.name
- console.log('蓝牙名称', boolethName)
- if (boolethName && boolethName.toUpperCase()
- .includes(deviceName.toUpperCase())) {
- console.log("搜索到设备:" + device.deviceId + " " +
- device.name);
- result = {
- code: SUCCESS_CODE,
- err_msg: SUCCESS_MSG,
- data: {
- device_no: device.deviceId,
- device_name: device.name
- }
- };
- foundDevices.push(device);
- status = true;
- clearTimeout(scanTimeout);
- setTimeout(() => {
- StopScanDevice(obj => {
- if (needShowList) {
- if (foundDevices
- .length === 0) {
- callback({
- code: 10002,
- err_msg: SCAN_DEVICE_MSG
- })
- } else {
- callback({
- code: 99,
- err_msg: ""
- })
- }
- }
- })
- }, needShowList ? 10000 : 10)
- callback(result, foundDevices)
- break
- }
- }
- }
- }
- })
- },
- fail: (err) => {
- console.log("startBluetoothDevicesDiscovery:fail", err);
- result = {
- code: SCAN_DEVICE_CODE,
- err_msg: SCAN_DEVICE_MSG
- };
- callback(result)
- }
- })
- },
- fail: (err) => {
- console.log("openBluetoothAdapter:fail", err);
- if (err.errno === 104) {
- result = {
- code: err.errno,
- err_msg: AUTH_OPEN_FAIL_MSG
- };
- } else {
- result = {
- code: OPEN_ADAPTER_FAIL_CODE,
- err_msg: OPEN_ADAPTER_FAIL_MSG
- };
- }
- callback(result)
- }
- })
- };
-
- //停止设备搜索
- function StopScanDevice(callback) {
- wx.stopBluetoothDevicesDiscovery({
- success(res) {
- console.log("停止设备搜索stopDiscovary:success", res);
- let resut = {
- code: SUCCESS_CODE,
- err_msg: SUCCESS_MSG
- };
- typeof callback === 'function' && callback(resut)
- },
- fail(err) {
- console.log("stopDiscovary:fail", err);
- let resut = {
- code: ERR_STOPSCANBLE_CODE,
- err_msg: ERR_STOPSCANBLE_MSG
- };
- typeof callback === 'function' && callback(resut)
- },
- })
- };
-
- //关闭蓝牙适配器
- function CloseBle(callback) {
- wx.closeBluetoothAdapter({
- success: (res) => {
- console.log("CloseBle:success", res);
- let result = {
- code: SUCCESS_CODE,
- err_msg: SUCCESS_MSG,
- data: res
- };
- callback(result)
- },
- fail: (err) => {
- console.log("CloseBle:fail", err);
- let result = {
- code: 1000,
- err_msg: "关闭蓝牙设备失败",
- data: err
- };
- callback(result)
- }
- })
- };
-
- //检测蓝牙适配器
- function CheckBle(callback) {
- wx.getBluetoothAdapterState({
- success(res) {
- if (res) {
- console.log("蓝牙状态", res)
- callback(res)
- }
- },
- fail(res) {
- callback(res)
- }
- })
- };
- //蓝牙搜索接口
- export default {
- ScanDevice,
- CloseBle,
- StopScanDevice,
- CheckBle
- }
|