Commit 9e392700 by lirandong

添加 蓝牙功能

parent 22d855a6
import { ComponentClass } from 'react'
import Taro, { Component } from '@tarojs/taro'
import { View, Text, Image } from '@tarojs/components'
import { IDeviceItem } from '@/pages/home/device/my_device'
import { IDeviceItem } from '@/pages/home/device/device_list'
import './index.scss'
......@@ -22,9 +22,7 @@ interface DeviceItem {
class DeviceItem extends Component {
render() {
// const { deviceInfo = {} } = this.props
const { equipmentName, equipmentState, equipmentUrl: url } = this.props
// console.log({ url })
return (
<View className="device-item">
<View className="device-item-img-wrapper">
......
......@@ -21,6 +21,11 @@ export interface IDeviceItem {
equipmentState: string
}
export interface IBluetoothServerListItem {
deviceId: string
characteristics: string
}
type PageStateProps = {
list: any[]
count: number
......@@ -125,13 +130,15 @@ class MyDevice extends Component {
this.cancelModal()
if (Taro.getEnv() !== Taro.ENV_TYPE.RN) {
try {
const { result } = await Taro.scanCode({
onlyFromCamera: true,
scanType: ['qrCode', 'barCode']
})
await api.common.addDeviceToken(result)
showMyToast({ tips: '添加成功~' })
this.getDate()
await this.bluetooth('40261886')
// const { result } = await Taro.scanCode({
// onlyFromCamera: true,
// scanType: ['qrCode', 'barCode']
// })
// await this.bluetooth(result)
// await api.common.addDeviceToken(result)
// showMyToast({ tips: '添加成功~' })
// this.getDate()
} catch (error) {
showMyToast({ result: error, tips: '添加失败~' })
console.error(error)
......@@ -139,6 +146,59 @@ class MyDevice extends Component {
}
}
async bluetooth(bluetoothCode: string) {
try {
await Taro.closeBluetoothAdapter()
Taro.showLoading({ title: '打开蓝牙...' })
await Taro.openBluetoothAdapter()
Taro.onBluetoothDeviceFound(async ({ devices }) => {
Taro.showLoading({ title: '连接蓝牙...' })
for (const item of devices) {
const newAdvertisData = item.advertisData.slice(2, item.advertisData.byteLength)
const advertisData = String.fromCharCode.apply(null, new Uint8Array(newAdvertisData))
if (advertisData === bluetoothCode) {
const { deviceId } = item
await Taro.createBLEConnection({ deviceId })
Taro.stopBluetoothDevicesDiscovery()
showMyToast({ tips: '蓝牙连接成功~' })
Taro.showLoading({ title: '获取蓝牙参数...' })
const { services } = await Taro.getBLEDeviceServices({ deviceId }) // 获取服务列表
const bluetoothServerList: IBluetoothServerListItem[] = []
for (const iterator of services) {
// const setverItem: IBluetoothServerListItem = {}
const { characteristics } = await Taro.getBLEDeviceCharacteristics({
deviceId,
serviceId: iterator.uuid
}) // 获取特征服务
console.log({ characteristics })
// setverItem.deviceId = deviceId
// setverItem.characteristics = characteristics
// bluetoothServerList.push(setverItem)
}
// await Taro.getBLEDeviceCharacteristics({ deviceId }) // 获取特征服务
// Taro.notifyBLECharacteristicValueChange()
console.log({ services })
await Taro.readBLECharacteristicValue({ deviceId, serviceId, characteristicId })
// setTimeout(async () => {
// Taro.closeBLEConnection({ deviceId })
// showMyToast({ tips: '连接已断开~' })
// }, 10000)
}
}
})
Taro.showLoading({ title: '搜索蓝牙...' })
await Taro.startBluetoothDevicesDiscovery({ services: ['8888'] })
} catch (error) {
console.error({ error })
Taro.stopBluetoothDevicesDiscovery()
const { errCode } = error
if (errCode === 10001) {
showMyToast({ tips: '请打开手机蓝牙~' })
}
}
}
shouldComponentUpdate(nextProps: IProps, _nextState) {
const { list } = this.props
const { list: _list } = nextProps
......@@ -193,6 +253,9 @@ class MyDevice extends Component {
{showModal ? (
<View className="select-modal-wrapper" onClick={this.cancelModal}>
<View className="select-modal">
{/* <View className="select-item" onClick={this.bluetooth}>
<Text>通过蓝牙添加设备</Text>
</View> */}
<View className="select-item" onClick={this.scanCode}>
<Text>扫码添加设备</Text>
</View>
......
......@@ -31,9 +31,8 @@
}
.device-select-bottom-bar {
// height: 40px;
display: flex;
flex-direction: row;
align-items: center;
flex-direction: row;
justify-content: center;
}
......@@ -48,7 +48,7 @@ class DeviceSelect extends Component {
navigationBarTitleText: '设备管理'
}
constructor(props: any) {
constructor(props) {
super(props)
const { filmId } = this.$router.params || ''
this.state = {
......@@ -89,11 +89,9 @@ class DeviceSelect extends Component {
const { checked, filmId } = this.state
if (!filmId) return
try {
// console.log({ filmId })
await api.common.updateEquipmentBinding([...checked], filmId)
showMyToast({ tips: '修改成功~' })
Taro.navigateBack()
// console.log({ res })
} catch (error) {
console.error(error)
showMyToast({ tips: '修改失败~' })
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment