1/** 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15import { ProfileConnectionState } from './BluetoothModel'; 16import Log from '../../../../../../../common/utils/src/main/ets/default/baseUtil/LogDecorator'; 17 18export class Profile { 19 profileId: number = -1; 20 profileConnectionState: number = -1 21 22 constructor() { 23 } 24} 25 26/** 27 * Bluetooth device class 28 */ 29export default class BluetoothDevice { 30 deviceId: string = ''; 31 deviceName: string = ''; 32 deviceType: string = ''; 33 connectionState: number = 0; 34 profiles: Map<number, Profile> = new Map(); 35 36 constructor() { 37 } 38 39 @Log 40 setProfiles(data: Array<{ 41 profileId: number; 42 profileConnectionState: number; 43 }>): void{ 44 45 data.forEach((item: { 46 profileId: number; 47 profileConnectionState: number; 48 }) => { 49 this.setProfile({ 50 profileId: item.profileId, 51 deviceId: this.deviceId, 52 profileConnectionState: item.profileConnectionState 53 }) 54 }) 55 } 56 57 setProfile(data: { 58 profileId: number; 59 deviceId: string; 60 profileConnectionState: number; 61 }): void{ 62 if (this.deviceId !== data.deviceId) { 63 return; 64 } 65 66 this.profiles.set(data.profileId, data) 67 68 let countStateDisconnect = 0; 69 let countStateConnecting = 0; 70 let countStateConnected = 0; 71 let countStateDisconnecting = 0; 72 73 this.profiles.forEach((profile, key) => { 74 if (profile.profileConnectionState == ProfileConnectionState.STATE_DISCONNECTED) { 75 countStateDisconnect++; 76 } else if (profile.profileConnectionState == ProfileConnectionState.STATE_CONNECTING) { 77 countStateConnecting++; 78 } else if (profile.profileConnectionState == ProfileConnectionState.STATE_CONNECTED) { 79 countStateConnected++; 80 } else if (profile.profileConnectionState == ProfileConnectionState.STATE_DISCONNECTING) { 81 countStateDisconnecting++; 82 } 83 }); 84 85 if (countStateConnected > 0 || countStateDisconnecting > 0) { 86 this.connectionState = ProfileConnectionState.STATE_CONNECTED; 87 } else if (countStateConnecting > 0) { 88 this.connectionState = ProfileConnectionState.STATE_CONNECTING; 89 } else { 90 this.connectionState = ProfileConnectionState.STATE_DISCONNECTED; 91 } 92 } 93} 94