scan.vue 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <template>
  2. <view class="container">
  3. <button @click="initBluetooth" :disabled="isInitializing">初始化蓝牙</button>
  4. <button @click="startDiscovery" :disabled="isDiscovering || !isBluetoothOpened">开始搜索设备</button>
  5. <button @click="connectDevice" :disabled="!selectedDevice || isConnecting">连接设备</button>
  6. <button @click="startDataMonitor" :disabled="!isConnected">开始监听数据</button>
  7. <view v-if="scanData">扫描结果: {{ scanData }}</view>
  8. <view v-for="item in devices">
  9. {{item.name}}
  10. </view>
  11. </view>
  12. </template>
  13. <script setup>
  14. import { ref } from 'vue';
  15. import { onLoad } from '@dcloudio/uni-app';
  16. const isInitializing = ref(false);
  17. const isBluetoothOpened = ref(false);
  18. const isDiscovering = ref(false);
  19. const isConnecting = ref(false);
  20. const isConnected = ref(false);
  21. const devices = ref([]);
  22. const selectedDevice = ref(null);
  23. const scanData = ref(null);
  24. const deviceId = ref('');
  25. const serviceId = ref('');
  26. const characteristicId = ref('');
  27. const initBluetooth = () => {
  28. isInitializing.value = true;
  29. uni.openBluetoothAdapter({
  30. success: (res) => {
  31. console.log('蓝牙适配器初始化成功');
  32. isBluetoothOpened.value = true;
  33. isInitializing.value = false;
  34. },
  35. fail: (err) => {
  36. console.log('蓝牙适配器初始化失败:', err);
  37. isInitializing.value = false;
  38. }
  39. });
  40. };
  41. const startDiscovery = () => {
  42. isDiscovering.value = true;
  43. uni.startBluetoothDevicesDiscovery({
  44. success: (res) => {
  45. console.log('开始搜索蓝牙设备成功');
  46. uni.onBluetoothDeviceFound((devicesRes) => {
  47. const newDevices = devicesRes.devices;
  48. devices.value = devices.value.concat(newDevices);
  49. console.log(devices.value,'devices.value')
  50. const scanGunDevice = newDevices.find((device) => device.name.includes('Z0'));
  51. if (scanGunDevice) {
  52. selectedDevice.value = scanGunDevice;
  53. deviceId.value = scanGunDevice.deviceId;
  54. }
  55. });
  56. uni.onBluetoothAdapterStateChange((stateRes) => {
  57. if (!stateRes.available) {
  58. isDiscovering.value = false;
  59. console.log('蓝牙适配器不可用,停止搜索');
  60. }
  61. });
  62. },
  63. fail: (err) => {
  64. console.log('开始搜索蓝牙设备失败:', err);
  65. isDiscovering.value = false;
  66. }
  67. });
  68. };
  69. const connectDevice = () => {
  70. isConnecting.value = true;
  71. uni.createBLEConnection({
  72. deviceId: deviceId.value,
  73. success: (res) => {
  74. console.log('连接蓝牙设备成功');
  75. isConnected.value = true;
  76. isConnecting.value = false;
  77. getBLEDeviceServices(deviceId.value);
  78. },
  79. fail: (err) => {
  80. console.log('连接蓝牙设备失败:', err);
  81. isConnecting.value = false;
  82. }
  83. });
  84. };
  85. const getBLEDeviceServices = (deviceId) => {
  86. uni.getBLEDeviceServices({
  87. deviceId: deviceId,
  88. success: (res) => {
  89. console.log('获取服务列表成功:', res.services);
  90. const targetService = res.services.find((service) => service.uuid === '目标服务UUID');
  91. if (targetService) {
  92. serviceId.value = targetService.uuid;
  93. getBLEDeviceCharacteristics(deviceId, serviceId.value);
  94. }
  95. },
  96. fail: (err) => {
  97. console.log('获取服务列表失败:', err);
  98. }
  99. });
  100. };
  101. const getBLEDeviceCharacteristics = (deviceId, serviceId) => {
  102. uni.getBLEDeviceCharacteristics({
  103. deviceId: deviceId,
  104. serviceId: serviceId,
  105. success: (res) => {
  106. console.log('获取特征列表成功:', res.characteristics);
  107. const targetCharacteristic = res.characteristics.find((characteristic) => characteristic.properties.read && characteristic.properties.notify);
  108. if (targetCharacteristic) {
  109. characteristicId.value = targetCharacteristic.uuid;
  110. }
  111. },
  112. fail: (err) => {
  113. console.log('获取特征列表失败:', err);
  114. }
  115. });
  116. };
  117. const startDataMonitor = () => {
  118. uni.onBLECharacteristicValueChange((characteristicRes) => {
  119. const value = characteristicRes.value;
  120. const data = ab2str(value);
  121. console.log('获取到扫码枪扫描的数据:', data);
  122. scanData.value = data;
  123. });
  124. uni.notifyBLECharacteristicValueChange({
  125. state: true,
  126. deviceId: deviceId.value,
  127. characteristicId: characteristicId.value,
  128. success: (res) => {
  129. console.log('开启特征值通知成功');
  130. },
  131. fail: (err) => {
  132. console.log('开启特征值通知失败:', err);
  133. }
  134. });
  135. };
  136. const ab2str = (buffer) => {
  137. return String.fromCharCode.apply(null, new Uint8Array(buffer));
  138. };
  139. </script>
  140. <style>
  141. .container {
  142. display: flex;
  143. flex-direction: column;
  144. align-items: center;
  145. padding: 20px;
  146. }
  147. button {
  148. margin: 10px 0;
  149. width: 200px;
  150. }
  151. </style>