• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2021-2022 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 */
15
16import bluetooth from '@ohos.bluetooth';
17import Log from '../../../../../../../../common/src/main/ets/default/Log';
18import createOrGet from '../../../../../../../../common/src/main/ets/default/SingleInstanceHelper';
19
20const TAG = 'BluetoothModel';
21
22export interface BlueupdateStateListener {
23  updateState(state: boolean): void;
24}
25
26function isBluetoothOpen(state: number): boolean {
27  Log.showInfo(TAG, `BluetoothState is: ${state}`);
28  return state == bluetooth.BluetoothState.STATE_ON || state == bluetooth.BluetoothState.STATE_BLE_ON
29  || state == bluetooth.BluetoothState.STATE_TURNING_ON || state == bluetooth.BluetoothState.STATE_BLE_TURNING_ON;
30}
31
32export class BluetoothService {
33  mIsStart = false;
34  mListener: BlueupdateStateListener;
35  mIsBluetoothOpen = false;
36
37  constructor() {
38    Log.showDebug(TAG, 'constructor');
39  }
40
41  startService(): void {
42    if (this.mIsStart) {
43      return;
44    }
45    this.mIsStart = true;
46    this.mIsBluetoothOpen = isBluetoothOpen(bluetooth.getState());
47    bluetooth.on('stateChange', (state): void => {
48      let isOpen = isBluetoothOpen(state);
49      if (this.mIsBluetoothOpen != isOpen) {
50        Log.showInfo(TAG, `state change: ${isOpen}`);
51        this.mIsBluetoothOpen = isOpen;
52        this.mListener?.updateState(this.mIsBluetoothOpen);
53      }
54      if(state == bluetooth.BluetoothState.STATE_ON) {
55        bluetooth.setBluetoothScanMode(bluetooth.ScanMode.SCAN_MODE_CONNECTABLE_GENERAL_DISCOVERABLE, 0);
56      }
57    });
58    Log.showInfo(TAG, `startService, mIsBluetoothOpen: ${this.mIsBluetoothOpen}`);
59  }
60
61  stopService(): void {
62    if (!this.mIsStart) {
63      return;
64    }
65    Log.showInfo(TAG, 'stopService');
66    this.mIsStart = false;
67    bluetooth.off('stateChange');
68  }
69
70  registerListener(listener: BlueupdateStateListener): void {
71    Log.showInfo(TAG, `registerListener, listener: ${listener}`);
72    this.mListener = listener;
73    this.mListener.updateState(this.mIsBluetoothOpen);
74  }
75
76  getState(): boolean {
77    return this.mIsBluetoothOpen;
78  }
79
80  enableBluetooth(): boolean{
81    Log.showInfo(TAG, 'enableBluetooth');
82    let result = bluetooth.enableBluetooth();
83    Log.showInfo(TAG, `enableBluetooth, result: ${result}`);
84    return result;
85  }
86
87  disableBluetooth(): boolean{
88    Log.showInfo(TAG, 'disableBluetooth');
89    let result = bluetooth.disableBluetooth();
90    Log.showInfo(TAG, `disableBluetooth, result: ${result}`);
91    return result;
92  }
93}
94
95let sBluetoothService = createOrGet(BluetoothService, TAG);
96
97export default sBluetoothService;