Commit 9e392700 by lirandong

添加 蓝牙功能

parent 22d855a6
import { ComponentClass } from 'react' import { ComponentClass } from 'react'
import Taro, { Component } from '@tarojs/taro' import Taro, { Component } from '@tarojs/taro'
import { View, Text, Image } from '@tarojs/components' 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' import './index.scss'
...@@ -22,9 +22,7 @@ interface DeviceItem { ...@@ -22,9 +22,7 @@ interface DeviceItem {
class DeviceItem extends Component { class DeviceItem extends Component {
render() { render() {
// const { deviceInfo = {} } = this.props
const { equipmentName, equipmentState, equipmentUrl: url } = this.props const { equipmentName, equipmentState, equipmentUrl: url } = this.props
// console.log({ url })
return ( return (
<View className="device-item"> <View className="device-item">
<View className="device-item-img-wrapper"> <View className="device-item-img-wrapper">
......
...@@ -21,6 +21,11 @@ export interface IDeviceItem { ...@@ -21,6 +21,11 @@ export interface IDeviceItem {
equipmentState: string equipmentState: string
} }
export interface IBluetoothServerListItem {
deviceId: string
characteristics: string
}
type PageStateProps = { type PageStateProps = {
list: any[] list: any[]
count: number count: number
...@@ -125,13 +130,15 @@ class MyDevice extends Component { ...@@ -125,13 +130,15 @@ class MyDevice extends Component {
this.cancelModal() this.cancelModal()
if (Taro.getEnv() !== Taro.ENV_TYPE.RN) { if (Taro.getEnv() !== Taro.ENV_TYPE.RN) {
try { try {
const { result } = await Taro.scanCode({ await this.bluetooth('40261886')
onlyFromCamera: true, // const { result } = await Taro.scanCode({
scanType: ['qrCode', 'barCode'] // onlyFromCamera: true,
}) // scanType: ['qrCode', 'barCode']
await api.common.addDeviceToken(result) // })
showMyToast({ tips: '添加成功~' }) // await this.bluetooth(result)
this.getDate() // await api.common.addDeviceToken(result)
// showMyToast({ tips: '添加成功~' })
// this.getDate()
} catch (error) { } catch (error) {
showMyToast({ result: error, tips: '添加失败~' }) showMyToast({ result: error, tips: '添加失败~' })
console.error(error) console.error(error)
...@@ -139,6 +146,59 @@ class MyDevice extends Component { ...@@ -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) { shouldComponentUpdate(nextProps: IProps, _nextState) {
const { list } = this.props const { list } = this.props
const { list: _list } = nextProps const { list: _list } = nextProps
...@@ -193,6 +253,9 @@ class MyDevice extends Component { ...@@ -193,6 +253,9 @@ class MyDevice extends Component {
{showModal ? ( {showModal ? (
<View className="select-modal-wrapper" onClick={this.cancelModal}> <View className="select-modal-wrapper" onClick={this.cancelModal}>
<View className="select-modal"> <View className="select-modal">
{/* <View className="select-item" onClick={this.bluetooth}>
<Text>通过蓝牙添加设备</Text>
</View> */}
<View className="select-item" onClick={this.scanCode}> <View className="select-item" onClick={this.scanCode}>
<Text>扫码添加设备</Text> <Text>扫码添加设备</Text>
</View> </View>
......
...@@ -31,9 +31,8 @@ ...@@ -31,9 +31,8 @@
} }
.device-select-bottom-bar { .device-select-bottom-bar {
// height: 40px;
display: flex; display: flex;
flex-direction: row;
align-items: center; align-items: center;
flex-direction: row;
justify-content: center; justify-content: center;
} }
...@@ -48,7 +48,7 @@ class DeviceSelect extends Component { ...@@ -48,7 +48,7 @@ class DeviceSelect extends Component {
navigationBarTitleText: '设备管理' navigationBarTitleText: '设备管理'
} }
constructor(props: any) { constructor(props) {
super(props) super(props)
const { filmId } = this.$router.params || '' const { filmId } = this.$router.params || ''
this.state = { this.state = {
...@@ -89,11 +89,9 @@ class DeviceSelect extends Component { ...@@ -89,11 +89,9 @@ class DeviceSelect extends Component {
const { checked, filmId } = this.state const { checked, filmId } = this.state
if (!filmId) return if (!filmId) return
try { try {
// console.log({ filmId })
await api.common.updateEquipmentBinding([...checked], filmId) await api.common.updateEquipmentBinding([...checked], filmId)
showMyToast({ tips: '修改成功~' }) showMyToast({ tips: '修改成功~' })
Taro.navigateBack() Taro.navigateBack()
// console.log({ res })
} catch (error) { } catch (error) {
console.error(error) console.error(error)
showMyToast({ tips: '修改失败~' }) 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