1const UUID_VENDOR_SERVICE = { "android": "0000FFA0-0000-1000-8000-00805F9B34FB", "ios": "FFA0" } 2const UUID_VENDOR_CHAR_READ = { "android": "0000FFA1-0000-1000-8000-00805F9B34FB", "ios": "FFA1" } 3const UUID_VENDOR_CHAR_WRITE = { "android": "0000FFA2-0000-1000-8000-00805F9B34FB", "ios": "FFA2" } 4const UUID_VENDOR_CHAR_NOTIFY = { "android": "0000FFA3-0000-1000-8000-00805F9B34FB", "ios": "FFA3" } 5 6function GetBluetoothAdapterState() { 7 return new Promise((resolve, reject) => { 8 my.getBluetoothAdapterState({ 9 success: res => { 10 console.log(res) 11 resolve(res); 12 }, 13 fail: error => { 14 reject(error); 15 }, 16 }) 17 }) 18} 19 20function OpenBluetoothAdapter() { 21 return new Promise((resolve, reject) => { 22 my.openBluetoothAdapter({ 23 success: res => { 24 if (!res.isSupportBLE) { 25 reject({ content: '抱歉,您的手机蓝牙暂不可用' }); 26 } 27 resolve({ content: '初始化成功!' }); 28 }, 29 fail: error => { 30 reject({ content: JSON.stringify(error) }); 31 }, 32 }); 33 }) 34} 35 36function CloseBluetoothAdapter() { 37 return new Promise((resolve, reject) => { 38 my.offBLECharacteristicValueChange(); 39 my.closeBluetoothAdapter({ 40 success: () => { 41 resolve({ content: '关闭蓝牙成功!' }); 42 }, 43 fail: error => { 44 reject({ content: JSON.stringify(error) }); 45 }, 46 }); 47 }) 48} 49 50function ConnectBLEDevice(deviceId) { 51 return new Promise((resolve, reject) => { 52 my.connectBLEDevice({ 53 deviceId: deviceId, 54 success: res => { 55 resolve({ content: res }); 56 }, 57 fail: error => { 58 reject({ content: JSON.stringify(error) }); 59 }, 60 }) 61 }) 62} 63 64function DisconnectBLEDevice(deviceId) { 65 return new Promise((resolve, reject) => { 66 my.disconnectBLEDevice({ 67 deviceId: deviceId, 68 success: res => { 69 resolve({ content: res }); 70 }, 71 fail: error => { 72 reject({ content: JSON.stringify(error) }); 73 }, 74 }) 75 }) 76} 77 78function DiscoveryBLEDevice(targetUUID) { 79 return new Promise((resolve, reject) => { 80 my.startBluetoothDevicesDiscovery({ 81 allowDuplicatesKey: true, 82 success: () => { 83 setTimeout(() => { 84 my.getBluetoothDevices({ 85 success: res => { 86 console.log(res) 87 let _net_config_devices = []; 88 for (let index = 0; index < res.devices.length; index++) { 89 if (res.devices[index].advertisServiceUUIDs != undefined && res.devices[index].advertisServiceUUIDs[0] == targetUUID) { 90 _net_config_devices.push(res.devices[index]); 91 } 92 } 93 if (_net_config_devices.length > 0) { 94 resolve({ devices: _net_config_devices, content: 'success' }) 95 } else { 96 resolve({ devices: null, content: '未搜索到目标设备' }) 97 } 98 }, 99 fail: error => { 100 resolve({ devices: null, content: '获取设备失败' + JSON.stringify(error) }); 101 }, 102 }); 103 }, 500) 104 }, 105 fail: error => { 106 resolve({ devices: null, content: '启动扫描失败' + JSON.stringify(error) }); 107 }, 108 }); 109 }); 110} 111 112module.exports = { 113 GetBluetoothAdapterState: GetBluetoothAdapterState, 114 OpenBluetoothAdapter: OpenBluetoothAdapter, 115 CloseBluetoothAdapter: CloseBluetoothAdapter, 116 ConnectBLEDevice: ConnectBLEDevice, 117 DisconnectBLEDevice: DisconnectBLEDevice, 118 DiscoveryBLEDevice: DiscoveryBLEDevice, 119 UUID_VENDOR_SERVICE, 120 UUID_VENDOR_CHAR_READ, 121 UUID_VENDOR_CHAR_WRITE, 122 UUID_VENDOR_CHAR_NOTIFY 123}