小程序蓝牙通信例子
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

40 lines
1.6 KiB

  1. <wxs module="utils">
  2. module.exports.max = function(n1, n2) {
  3. return Math.max(n1, n2)
  4. }
  5. module.exports.len = function(arr) {
  6. arr = arr || []
  7. return arr.length
  8. }
  9. </wxs>
  10. <button bindtap="openBluetoothAdapter">开始扫描</button>
  11. <button bindtap="stopBluetoothDevicesDiscovery">停止扫描</button>
  12. <button bindtap="closeBluetoothAdapter">结束流程</button>
  13. <view class="devices_summary">已发现 {{devices.length}} 个外围设备:</view>
  14. <scroll-view class="device_list" scroll-y scroll-with-animation>
  15. <view wx:for="{{devices}}" wx:key="index"
  16. data-device-id="{{item.deviceId}}"
  17. data-name="{{item.name || item.localName}}"
  18. bindtap="createBLEConnection"
  19. class="device_item"
  20. hover-class="device_item_hover">
  21. <view style="font-size: 16px; color: #333;">{{item.name}}</view>
  22. <view style="font-size: 10px">信号强度: {{item.RSSI}}dBm ({{utils.max(0, item.RSSI + 100)}}%)</view>
  23. <view style="font-size: 10px">UUID: {{item.deviceId}}</view>
  24. <view style="font-size: 10px">Service数量: {{utils.len(item.advertisServiceUUIDs)}}</view>
  25. </view>
  26. </scroll-view>
  27. <view class="connected_info" wx:if="{{connected}}">
  28. <view>
  29. <text>已连接到 {{name}}</text>
  30. <view class="operation">
  31. <button wx:if="{{canWrite}}" size="mini" bindtap="writeBLECharacteristicValue">写数据</button>
  32. <button size="mini" bindtap="closeBLEConnection">断开连接</button>
  33. </view>
  34. </view>
  35. <view wx:for="{{chs}}" wx:key="index" style="font-size: 12px; margin-top: 10px;">
  36. <view>特性UUID: {{item.uuid}}</view>
  37. <view>特性值: {{item.value}}</view>
  38. </view>
  39. </view>