feat: 添加设备绑定状态管理,优化设备连接逻辑
This commit is contained in:
parent
5465c4f2dd
commit
f6cf8037ac
|
|
@ -517,6 +517,8 @@ const DeviceItem = observer(({ device }: { device: any }) => {
|
|||
const [nameShow, setNameShow] = useState(bindDevice ? `${bindDevice?.name}` : name)
|
||||
const tempName = useRef(nameShow)
|
||||
|
||||
const connectFinal = isConnected && bindDevice
|
||||
|
||||
const canEdit = !!bindDevice && isConnected
|
||||
|
||||
// 绑定过的设备
|
||||
|
|
@ -537,13 +539,19 @@ const DeviceItem = observer(({ device }: { device: any }) => {
|
|||
.then(() => {
|
||||
console.log('设备连接成功')
|
||||
if (userId) {
|
||||
bleManager.bindDevice(userId).then((res) => {
|
||||
// console.log('Bind device response------------:', res)
|
||||
if (res.success !== 1) {
|
||||
bleManager
|
||||
.bindDevice(userId)
|
||||
.then((res) => {
|
||||
console.log('Bind device response------------:', res)
|
||||
})
|
||||
.catch((e) => {
|
||||
console.log('Bind device error------------:', e)
|
||||
Toast.show({ title: '设备已经被其他用户绑定过了' })
|
||||
bleManager.disconnectDevice()
|
||||
}
|
||||
})
|
||||
})
|
||||
.finally(() => {
|
||||
Toast.hideLoading()
|
||||
})
|
||||
}
|
||||
})
|
||||
.catch(() => {
|
||||
|
|
@ -581,7 +589,6 @@ const DeviceItem = observer(({ device }: { device: any }) => {
|
|||
try {
|
||||
await bleManager.unBindDevice(userId!)
|
||||
bleManager.disconnectDevice()
|
||||
bleStore.removeBindDeviceItem(device.id)
|
||||
setNameShow(device.name || 'Unknown Device')
|
||||
Toast.show({ title: '解绑成功' })
|
||||
} catch (error: any) {
|
||||
|
|
@ -622,7 +629,7 @@ const DeviceItem = observer(({ device }: { device: any }) => {
|
|||
|
||||
return (
|
||||
<Block className="relative mb-[16px] flex-row items-center overflow-hidden border-[3px] border-black bg-white p-[16px] shadow-large-black">
|
||||
{isConnected && <Block className="absolute inset-y-0 left-0 w-[16px] border-r-[3px] border-black bg-accent" />}
|
||||
{connectFinal && <Block className="absolute inset-y-0 left-0 w-[16px] border-r-[3px] border-black bg-accent" />}
|
||||
|
||||
<Block className="size-[48px] items-center justify-center border-[3px] border-black bg-gray-100 shadow-soft-black-10">
|
||||
<Ionicons color="black" name="phone-portrait-outline" size={24} />
|
||||
|
|
@ -641,16 +648,16 @@ const DeviceItem = observer(({ device }: { device: any }) => {
|
|||
<Block className="flex-row">
|
||||
<Block className="">
|
||||
<Text
|
||||
className={`border-2 border-black px-[6px] text-[10px] font-[900] ${isConnected ? 'bg-black text-accent' : 'bg-gray-200 text-gray-500'}`}
|
||||
className={`border-2 border-black px-[6px] text-[10px] font-[900] ${connectFinal ? 'bg-black text-accent' : 'bg-gray-200 text-gray-500'}`}
|
||||
>
|
||||
{isConnected ? '已连接' : '未连接'}
|
||||
{connectFinal ? '已连接' : '未连接'}
|
||||
</Text>
|
||||
</Block>
|
||||
|
||||
{hasBind && (
|
||||
<Block className="ml-[8px]">
|
||||
<Text
|
||||
className={`border-2 border-black px-[6px] text-[10px] font-[900] ${isConnected ? 'bg-black text-accent' : 'bg-gray-200 text-gray-500'}`}
|
||||
className={`border-2 border-black px-[6px] text-[10px] font-[900] ${connectFinal ? 'bg-black text-accent' : 'bg-gray-200 text-gray-500'}`}
|
||||
>
|
||||
已绑定
|
||||
</Text>
|
||||
|
|
@ -662,10 +669,10 @@ const DeviceItem = observer(({ device }: { device: any }) => {
|
|||
|
||||
<Block className="relative z-10 flex-col items-end space-x-[8px]">
|
||||
<Block
|
||||
className={`h-[40px] items-center justify-center border-[3px] border-black px-[16px] text-[12px] font-[900] ${isConnected ? 'bg-accent text-black shadow-medium-black' : 'bg-white text-black shadow-medium-gray'}`}
|
||||
className={`h-[40px] items-center justify-center border-[3px] border-black px-[16px] text-[12px] font-[900] ${connectFinal ? 'bg-accent text-black shadow-medium-black' : 'bg-white text-black shadow-medium-gray'}`}
|
||||
onClick={() => onConnectToggle(device)}
|
||||
>
|
||||
<Text className="text-[12px] font-[900]">{isConnected ? '已连接' : '连接'}</Text>
|
||||
<Text className="text-[12px] font-[900]">{connectFinal ? '已连接' : '连接'}</Text>
|
||||
</Block>
|
||||
{canEdit && (
|
||||
<Block
|
||||
|
|
|
|||
|
|
@ -117,6 +117,7 @@ class BleManager {
|
|||
this.resolvePendingOperation('bindDevice', status)
|
||||
} else {
|
||||
this.rejectPendingOperation('bindDevice', '绑定失败')
|
||||
bleStore.removeBindDeviceItem(bleStore?.state?.connectedDevice?.id!)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -641,7 +642,7 @@ class BleManager {
|
|||
try {
|
||||
const resultPromise = this.createWaitableOperation<UnBindResponse>('unbindDevice')
|
||||
await this.deviceInfoService.unbindDevice(state.connectedDevice.id, userId)
|
||||
|
||||
bleStore.removeBindDeviceItem(state.connectedDevice.id)
|
||||
return await resultPromise
|
||||
} catch (e: any) {
|
||||
this.setError(`Request failed: ${e.message}`)
|
||||
|
|
|
|||
|
|
@ -15,6 +15,8 @@ interface BleState {
|
|||
sn: string
|
||||
isActivated: boolean
|
||||
transferProgress: number
|
||||
|
||||
canBind: boolean
|
||||
discoveredDevices: BleDevice[]
|
||||
loading: {
|
||||
connecting: boolean
|
||||
|
|
@ -43,7 +45,6 @@ class BleStore {
|
|||
connectedDevice: null,
|
||||
deviceInfo: null,
|
||||
version: '',
|
||||
|
||||
sn: '',
|
||||
isActivated: false,
|
||||
transferProgress: 0,
|
||||
|
|
|
|||
Loading…
Reference in New Issue