🌈 style(eslint): 修复eslint的报错❌
This commit is contained in:
@@ -69,8 +69,10 @@ const stateObj: stateStype = {
|
||||
// 取色仪主动返回的数据
|
||||
deviceLab: null,
|
||||
}
|
||||
|
||||
export default (props) => {
|
||||
interface PropsType {
|
||||
children?: React.ReactNode
|
||||
}
|
||||
const ContextBlueTooth = (props: PropsType) => {
|
||||
const refStatus = useRef(stateObj)
|
||||
const [state, setState] = useState(refStatus.current)
|
||||
|
||||
@@ -79,50 +81,6 @@ export default (props) => {
|
||||
setState({ ...refStatus.current })
|
||||
}
|
||||
|
||||
const init = async() => {
|
||||
try {
|
||||
await openAdapter()
|
||||
}
|
||||
catch (e) {
|
||||
changeStatus({ available: false })
|
||||
}
|
||||
|
||||
// 绑定事件通知
|
||||
Taro.onBluetoothAdapterStateChange((res) => {
|
||||
emit({ type: 'stateUpdate', detail: res })
|
||||
})
|
||||
Taro.onBLEConnectionStateChange((res) => {
|
||||
emit({ type: res.connected ? 'connected' : 'disconnect', detail: res })
|
||||
})
|
||||
Taro.onBLECharacteristicValueChange(({ value }) => notifySubscriber(value))
|
||||
subscribe(async(ev) => {
|
||||
if (ev.type === 'stateUpdate') {
|
||||
// 蓝牙状态发生的变化
|
||||
changeStatus({ discovering: ev.detail.discovering, available: ev.detail.available })
|
||||
}
|
||||
else if (ev.type === 'disconnect' && refStatus.current.connected && refStatus.current.connected.deviceId === ev.detail.deviceId) {
|
||||
// 断开连接
|
||||
changeStatus({
|
||||
connected: null,
|
||||
serviceId: null,
|
||||
characteristicId: null,
|
||||
deviceLab: null,
|
||||
devices: [],
|
||||
})
|
||||
Taro.showToast({ icon: 'none', title: '蓝牙连接已断开' })
|
||||
}
|
||||
else if (ev.type === 'connected' && refStatus.current.connecting) {
|
||||
// 连接成功
|
||||
changeStatus({ connected: refStatus.current.connecting, connecting: null })
|
||||
Taro.showToast({ title: '蓝牙已连接' })
|
||||
}
|
||||
else if (ev.type === 'measure') {
|
||||
// 监听取色仪主动推送lab
|
||||
await measureAndGetLab()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/** 打开蓝牙适配器 */
|
||||
const openAdapter = () => {
|
||||
return new Promise((resolve, reject) => {
|
||||
@@ -163,7 +121,59 @@ export default (props) => {
|
||||
})
|
||||
})
|
||||
}
|
||||
const init = async() => {
|
||||
try {
|
||||
await openAdapter()
|
||||
}
|
||||
catch (e) {
|
||||
changeStatus({ available: false })
|
||||
}
|
||||
|
||||
// 绑定事件通知
|
||||
Taro.onBluetoothAdapterStateChange((res) => {
|
||||
emit({ type: 'stateUpdate', detail: res })
|
||||
})
|
||||
Taro.onBLEConnectionStateChange((res) => {
|
||||
emit({ type: res.connected ? 'connected' : 'disconnect', detail: res })
|
||||
})
|
||||
Taro.onBLECharacteristicValueChange(({ value }) => notifySubscriber(value))
|
||||
subscribe(async(ev) => {
|
||||
if (ev.type === 'stateUpdate') {
|
||||
// 蓝牙状态发生的变化
|
||||
changeStatus({ discovering: ev.detail.discovering, available: ev.detail.available })
|
||||
}
|
||||
else if (ev.type === 'disconnect' && refStatus.current.connected && refStatus.current.connected.deviceId === ev.detail.deviceId) {
|
||||
// 断开连接
|
||||
changeStatus({
|
||||
connected: null,
|
||||
serviceId: null,
|
||||
characteristicId: null,
|
||||
deviceLab: null,
|
||||
devices: [],
|
||||
})
|
||||
Taro.showToast({ icon: 'none', title: '蓝牙连接已断开' })
|
||||
}
|
||||
else if (ev.type === 'connected' && refStatus.current.connecting) {
|
||||
// 连接成功
|
||||
changeStatus({ connected: refStatus.current.connecting, connecting: null })
|
||||
Taro.showToast({ title: '蓝牙已连接' })
|
||||
}
|
||||
else if (ev.type === 'measure') {
|
||||
// 监听取色仪主动推送lab
|
||||
await measureAndGetLab()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// 获取搜索到的设备
|
||||
const getDevices = (res) => {
|
||||
res.devices.forEach((device) => {
|
||||
// 排除掉已搜索到的设备和名称不合法的设备, 将新发现的设备添加到列表中
|
||||
if (/^CM/.test(device.name) && !refStatus.current.devices.find(i => i.deviceId === device.deviceId)) {
|
||||
changeStatus({ devices: [...refStatus.current.devices, device] })
|
||||
}
|
||||
})
|
||||
}
|
||||
/**
|
||||
* 启动设备扫描
|
||||
* @param {(res: { devices: { name: string, deviceId: string, RSSI: number }[] }) => void} cb
|
||||
@@ -190,16 +200,6 @@ export default (props) => {
|
||||
})
|
||||
}
|
||||
|
||||
// 获取搜索到的设备
|
||||
const getDevices = (res) => {
|
||||
res.devices.forEach((device) => {
|
||||
// 排除掉已搜索到的设备和名称不合法的设备, 将新发现的设备添加到列表中
|
||||
if (/^CM/.test(device.name) && !refStatus.current.devices.find(i => i.deviceId === device.deviceId)) {
|
||||
changeStatus({ devices: [...refStatus.current.devices, device] })
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* 连接设备
|
||||
* @param {{ name: string, deviceId: string, RSSI: number }} device
|
||||
@@ -212,7 +212,7 @@ export default (props) => {
|
||||
await discoverService(device.deviceId)
|
||||
await discoverCharacteristic(device.deviceId)
|
||||
await notifyCharacteristicValueChange(device.deviceId)
|
||||
}
|
||||
}
|
||||
catch (e) {
|
||||
changeStatus({ connecting: null })
|
||||
Taro.showToast({ icon: 'none', title: '蓝牙连接失败' })
|
||||
@@ -273,7 +273,7 @@ export default (props) => {
|
||||
const service = services.find(i => refStatus.current.serviceRule.test(i.uuid))
|
||||
if (!service) {
|
||||
reject(new Error('服务不可用'))
|
||||
}
|
||||
}
|
||||
else {
|
||||
changeStatus({ serviceId: service.uuid })
|
||||
resolve(service)
|
||||
@@ -294,7 +294,7 @@ export default (props) => {
|
||||
const characteristic = characteristics.find(i => refStatus.current.characteristicRule.test(i.uuid))
|
||||
if (!characteristic) {
|
||||
reject(new Error('特征不可用'))
|
||||
}
|
||||
}
|
||||
else {
|
||||
changeStatus({ characteristicId: characteristic.uuid })
|
||||
resolve(characteristic)
|
||||
@@ -332,13 +332,13 @@ export default (props) => {
|
||||
if (refStatus.current.command.isComplete) {
|
||||
if (refStatus.current.command.isValid && refStatus.current.responseResolve) {
|
||||
refStatus.current.responseResolve(refStatus.current.command.response)
|
||||
}
|
||||
}
|
||||
else if (!refStatus.current.command.isValid) {
|
||||
refStatus.current.responseReject(new Error('无效数据'))
|
||||
}
|
||||
resetCommand()
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
const uint8Array = new Uint8Array(buffer)
|
||||
if (uint8Array[0] === 0xBB && uint8Array[1] === 1 && uint8Array[3] === 0) {
|
||||
@@ -354,10 +354,11 @@ export default (props) => {
|
||||
* @returns {Promise<Uint8Array>}
|
||||
*/
|
||||
function exec(command) {
|
||||
// eslint-disable-next-line no-async-promise-executor
|
||||
return new Promise(async(resolve, reject) => {
|
||||
if (refStatus.current.command) {
|
||||
reject(new Error('正在执行其他命令'))
|
||||
}
|
||||
}
|
||||
else {
|
||||
try {
|
||||
refStatus.current.command = command
|
||||
@@ -369,7 +370,7 @@ export default (props) => {
|
||||
if (command.responseSize <= 0) {
|
||||
resolve(true)
|
||||
resetCommand()
|
||||
}
|
||||
}
|
||||
else {
|
||||
refStatus.current.responseReject = reject
|
||||
refStatus.current.responseResolve = resolve
|
||||
@@ -378,7 +379,7 @@ export default (props) => {
|
||||
resetCommand()
|
||||
}, command.timeout)
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (e) {
|
||||
reject(e)
|
||||
}
|
||||
@@ -483,8 +484,10 @@ export const useBluetooth = () => {
|
||||
const res = React.useContext<any>(Context)
|
||||
if (res) {
|
||||
return { ...res }
|
||||
}
|
||||
}
|
||||
else {
|
||||
return {}
|
||||
}
|
||||
}
|
||||
|
||||
export default ContextBlueTooth
|
||||
|
||||
Reference in New Issue
Block a user