Bluetooth scanning method, electronic device, storage medium and program product
By introducing a scheduling mechanism for the first and second running tasks into the Bluetooth scanning method, the Bluetooth scanning process is simplified, the complexity and timing errors in the prior art are solved, and flexible periodic scanning and resource optimization are achieved.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- VOYAH AUTOMOBILE TECH CO LTD
- Filing Date
- 2026-02-03
- Publication Date
- 2026-06-05
Smart Images

Figure CN122160744A_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of wireless communication technology, and in particular to a Bluetooth scanning method, electronic device, storage medium, and program product. Background Technology
[0002] In Bluetooth communication scenarios for smart cars, IoT devices, and mobile terminals, rapid discovery and connection between devices is a core requirement. In some scenarios, periodic scanning is required to discover surrounding devices.
[0003] In related technologies, starting or stopping a Bluetooth scanning operation involves a multi-layered architecture and a complex calling process. It requires writing a lot of code to implement complex calls. To achieve periodic scanning, the above complex calling process needs to be repeated, which increases the complexity of the code, results in a high error rate, and makes it difficult to achieve accurate periodic scanning. Summary of the Invention
[0004] This application provides a Bluetooth scanning method, electronic device, storage medium, and program product to simplify the implementation of periodic Bluetooth scanning and improve the accuracy of periodic Bluetooth scanning.
[0005] In a first aspect, embodiments of this application provide a Bluetooth scanning method, including:
[0006] In response to a Bluetooth scan request, the Bluetooth startup callback method is invoked;
[0007] Inside the Bluetooth startup callback method, the first running task that was stopped after startup is scheduled to execute after the first delay.
[0008] When the first running task calls the start scan callback function, the second running task starts after the scheduler stops, and the second running task executes after the second delay.
[0009] When the second running task calls the stop scanning callback function, the first running task is scheduled and executed after the first delay.
[0010] In one possible implementation, in response to a Bluetooth scan request, a Bluetooth startup callback method is invoked, including: in response to the Bluetooth scan request, accessing a singleton instance of the scanner; determining, through the singleton instance, whether the global running flag satisfies a globally unique condition; and if the globally unique condition is satisfied, invoking the Bluetooth startup callback method based on the Bluetooth hardware's scan flag.
[0011] In one possible implementation, determining whether the global running flag satisfies the global uniqueness condition through a singleton instance includes: viewing the global running flag through the singleton instance; if the global running flag is running, then the uniqueness condition is not met; if the global running flag is not running, then the uniqueness condition is met.
[0012] In one possible implementation, if the globally unique condition is met, the Bluetooth startup callback method is invoked based on the Bluetooth hardware's scanning flag, including: if the globally unique condition is met, setting the global running flag to running via a singleton instance; checking the Bluetooth hardware's scanning flag via the scheduler; if the scanning flag is scanning, stopping the current scanning task and setting the scanning flag to not scanning; after stopping the current scanning task for a preset duration, if the scanning flag is not scanning, invoking the Bluetooth startup callback method; and setting the scanning flag to scanning.
[0013] In one possible implementation, the method further includes: in response to a stop scanning request, setting a global run flag to not run via a singleton instance; setting a scan flag to not run; and calling a callback method for Bluetooth stop.
[0014] In one possible implementation, the method further includes: in response to a Bluetooth scanning request, obtaining a scene identifier of the vehicle; the scene identifier is determined by the vehicle state and the environment in which the vehicle is located; and obtaining a first delay and a second delay corresponding to the scene identifier.
[0015] Secondly, embodiments of this application provide a Bluetooth scanning device, the device comprising:
[0016] The startup module is used to call the Bluetooth startup callback method in response to a Bluetooth scan request;
[0017] The first scheduling module is used inside the Bluetooth startup callback method to schedule the first running task that stops after startup. The first running task is executed after a first delay.
[0018] The second scheduling module is used to start the second running task after the scheduling stops when the first running task calls the start scanning callback function. The second running task is executed after the second delay.
[0019] The third scheduling module is used to schedule the first running task when the second running task calls the stop scanning callback function. The first running task is executed after the first delay.
[0020] Thirdly, embodiments of this application provide an electronic device, including: a memory and a processor; the memory stores computer-executable instructions; the processor executes the computer-executable instructions stored in the memory, causing the processor to perform the first aspect and / or various possible implementations of the first aspect as described above.
[0021] Fourthly, embodiments of this application provide a computer-readable storage medium storing computer-executable instructions, which, when executed by a processor, are used to implement the first aspect and / or various possible implementations of the first aspect.
[0022] Fifthly, embodiments of this application provide a computer program product, including a computer program that, when executed by a processor, implements the first aspect and / or various possible implementations of the first aspect.
[0023] The Bluetooth scanning method, electronic device, storage medium, and program product provided in this application, in response to a Bluetooth scanning request, call a Bluetooth startup callback method. Within this callback method, a first running task is scheduled, and the first running task executes after a first delay. When the first running task is executed, a start scanning callback function is called, and a second running task is scheduled. The second running task executes after a second delay. When the second running task is executed, a stop scanning callback function is called, and the first running task is scheduled, and the first running task executes after the first delay. In related technologies, complex calls are implemented through a large amount of code. Periodic Bluetooth scanning requires rereading and rewriting the same calling code, resulting in code redundancy and complex implementation methods, which are prone to timing issues. Errors and status judgment errors can cause periodic Bluetooth scanning to malfunction. In this embodiment, periodic Bluetooth scanning is achieved by scheduling the first and second running tasks together and setting delays for executing the first and second running tasks after scheduling. This eliminates the need for redundant calling code and complex function implementations, making the code for periodic Bluetooth scanning more concise and easier to maintain. Furthermore, by calling each other between runnable tasks, the complex process control problem is transformed into a clear task management and scheduling problem, effectively improving the timing and logical accuracy of periodic Bluetooth scanning. In addition, the first and second delays can be customized to form a configurable scanning cycle, allowing the scanning cycle to flexibly adapt to different scenario requirements. Attached Figure Description
[0024] The accompanying drawings, which are incorporated in and form part of this specification, illustrate embodiments consistent with this application and, together with the description, serve to explain the principles of this application.
[0025] Figure 1 A flowchart illustrating the Bluetooth scanning method provided in this application;
[0026] Figure 2 A schematic diagram illustrating the mutual scheduling of the first and second running tasks provided for this application;
[0027] Figure 3 A flowchart illustrating the process of invoking the Bluetooth startup callback method provided in this application;
[0028] Figure 4 This is a schematic diagram of the Bluetooth scanning device provided in this application;
[0029] Figure 5 A schematic diagram of the structure of the electronic device provided in this application.
[0030] The accompanying drawings illustrate specific embodiments of this application, which will be described in more detail below. These drawings and descriptions are not intended to limit the scope of the concept in any way, but rather to illustrate the concept of this application to those skilled in the art through reference to particular embodiments. Detailed Implementation
[0031] Exemplary embodiments will now be described in detail, examples of which are illustrated in the accompanying drawings. When the following description relates to the drawings, unless otherwise indicated, the same numbers in different drawings denote the same or similar elements. The embodiments described in the following exemplary embodiments do not represent all embodiments consistent with this application. Rather, they are merely examples of apparatuses and methods consistent with some aspects of this application as detailed in the appended claims.
[0032] The technical solution of this application and how the technical solution of this application solves the above-mentioned technical problems are described in detail below with specific embodiments. These specific embodiments can be combined with each other, and the same or similar concepts or processes may not be described again in some embodiments. The embodiments of this application will now be described with reference to the accompanying drawings.
[0033] Figure 1 This is a flowchart illustrating the Bluetooth scanning method provided in this application. The Bluetooth scanning method can be applied to electronic devices, such as vehicle-mounted devices; Figure 1 As shown, the Bluetooth scanning method includes:
[0034] S101. In response to the Bluetooth scanning request, call the Bluetooth startup callback method.
[0035] The Bluetooth scan request can be triggered by user operation or by a specific event; for example, a user presses a micro switch on the door handle to initiate a Bluetooth scan request to scan for paired keys or mobile devices in the vicinity; another example is that a moving object is detected in the rear area of the vehicle, triggering a Bluetooth scan request.
[0036] The callback method for Bluetooth startup is called to perform periodic Bluetooth scanning.
[0037] Specifically, when an electronic device receives a Bluetooth scan request, it needs to start scanning for Bluetooth devices. The Bluetooth Helper takes over the processing of the Bluetooth scan request. The Bluetooth Helper obtains the ScannerWorker and uses the ScannerWorker to determine whether the scanning conditions are met. If they are met, the ScannerWorker calls back the Bluetooth startup callback method of the Bluetooth Helper.
[0038] S102. Inside the Bluetooth startup callback method, schedule the first running task that was stopped after startup, and the first running task will be executed after the first delay.
[0039] The first running task that stops after starting refers to the first running task used to start scanning and then scheduled to stop scanning. In practical applications, the first running task is implemented through a runnable task, for example, the first running task is startThenStopScanRunnable.
[0040] During periodic Bluetooth scanning, the first delay is the duration for which scanning stops. The specific duration of the first delay can be set according to actual needs or determined based on the current scene identifier of the vehicle. For example, the first delay can be 2 seconds.
[0041] Specifically, inside the Bluetooth startup callback method, startThenStopScanRunnable is scheduled and set to execute after the first delay.
[0042] S103. When the first running task calls the start scan callback function, the second running task, which was started after the scheduler stopped, is executed after the second delay.
[0043] The start scan callback function is used to initiate the scan; in practical applications, the start scan callback function can be onStartScan.run().
[0044] The second running task that starts after stopping refers to the second running task used to stop scanning and then schedule the start of scanning. In practical applications, the first running task is implemented through a Runnable task. For example, the second running task is StopThenstartScanRunnable.
[0045] During periodic Bluetooth scanning, the second delay is the duration of the scan. The specific duration of the second delay can be set according to actual needs or determined according to the current scene identifier of the vehicle. For example, the second delay can be 550 milliseconds.
[0046] Specifically, when the first running task (startThenStopScanRunnable) is executed, the start scan callback function is called to perform Bluetooth scanning, and the second running task (StopThenstartScanRunnable) is scheduled to execute after the second delay.
[0047] It should be noted that when the first running task is executed, the start scan callback function is called and the second running task is scheduled. The second running task is executed after the second delay, not immediately. Therefore, during the second delay period, Bluetooth scanning is performed through the start scan callback function. Thus, the duration of Bluetooth scanning is the second delay.
[0048] S104. When the second running task calls the stop scanning callback function, the first running task is scheduled and executed after the first delay.
[0049] The stop scan callback function is used to pause the scan; in practical applications, the stop scan callback function can be onStopScan.run().
[0050] Specifically, when the second running task (StopThenstartScanRunnable) is executed, the stop scanning callback function is called to perform Bluetooth scanning, and the first running task (startThenStopScanRunnable) is scheduled to execute after the first delay.
[0051] It should be noted that when the second running task is executed, the stop scanning callback function is called and the first running task is scheduled. The first running task is executed after the first delay, not immediately. Therefore, during the first delay period, scanning is paused by the stop scanning callback function. Thus, the duration of Bluetooth scanning pause is the first delay.
[0052] In addition, the first running task is scheduled by the second running task, and the first running task is executed after the first delay, realizing periodic Bluetooth scanning; when the first running task is executed, the start scanning callback function is called, that is, the next cycle of Bluetooth scanning begins.
[0053] For example, such as Figure 2As shown, inside the Bluetooth startup callback method, a first running task is scheduled and set to execute after a first delay. During the Bluetooth scan cycle T1, when the first running task is executed, onStartScan.run() is called, and a second running task is scheduled and set to execute after a second delay. During the second delay, Bluetooth scanning is performed through onStartScan.run(). If the Bluetooth scan duration reaches the second delay, the second running task is executed. When the second running task is executed, onStopScan.run() is called, and the first running task is scheduled and set to execute after a first delay. During the first delay, Bluetooth scanning is paused through onStopScan.run(). If the pause duration reaches the first delay, the first running task is executed, entering the second Bluetooth scan cycle, thus achieving a loop and implementing periodic Bluetooth scanning.
[0054] The Bluetooth scanning method provided in this application, in response to a Bluetooth scanning request, calls a Bluetooth startup callback method. Within this callback method, a first running task is scheduled, and the first running task executes after a first delay. When the first running task is executed, a start scanning callback function is called, and a second running task is scheduled. The second running task executes after a second delay. When the second running task is executed, a stop scanning callback function is called, and the first running task is scheduled, and the first running task executes after the first delay. In related technologies, complex calls are implemented through a large amount of code. Periodic Bluetooth scanning requires rereading and rewriting the same call code, resulting in code redundancy. Furthermore, the implementation is complex and prone to timing errors and incorrect state judgments. Errors can cause periodic Bluetooth scanning anomalies. In this embodiment, periodic Bluetooth scanning is achieved by scheduling the first and second running tasks together and setting delays for executing the first and second running tasks after scheduling. This eliminates the need for redundant calling code and complex function implementations, making the code for periodic Bluetooth scanning more concise and easier to maintain. Moreover, by calling each other between runnable tasks, the complex process control problem is transformed into a clear task management and scheduling problem, effectively improving the timing and logical accuracy of periodic Bluetooth scanning. Furthermore, the first and second delays can be customized to form a configurable scanning cycle, allowing the scanning cycle to flexibly adapt to different scenario requirements.
[0055] In some embodiments, such as Figure 3 As shown, in response to a Bluetooth scan request, the Bluetooth startup callback method is invoked, including: S301, in response to a Bluetooth scan request, accessing a singleton instance of the scanner; S302, determining whether the global running flag satisfies the globally unique condition through the singleton instance; S303, if the globally unique condition is satisfied, invoking the Bluetooth startup callback method based on the Bluetooth hardware's scan flag.
[0056] The singleton instance of the scan worker refers to a single scan worker instance globally, which accesses the same scan worker object regardless of how it is triggered.
[0057] The global run flag is a global lock, triggered by a Bluetooth scan request, used to prevent multiple external requests from occurring concurrently and causing scan conflicts; in practical applications, the global run flag can be isRunning.
[0058] The scan flag is a physical lock used to manage the scanning status of Bluetooth hardware; in practical applications, the scan flag can be isScanning.
[0059] Specifically, in response to a Bluetooth scan request, the Bluetooth Helper obtains a singleton instance of the ScannerWorker. The ScannerWorker singleton instance checks isRunning and determines whether it meets the globally unique condition. If isRunning meets the globally unique condition, it checks isScanning and calls the Bluetooth startup callback method based on isScanning.
[0060] Optionally, if the global run flag does not meet the globally unique condition, return directly and refuse to start repeatedly.
[0061] In the above embodiments, by using a single instance of the scanning worker, it is ensured that only one Bluetooth scanning process exists at the same time, avoiding repeated creation and startup caused by multiple threads or events. It checks whether the global running flag meets the globally unique condition. If it does, it calls the Bluetooth startup callback method according to the Bluetooth hardware's scanning flag, preventing multiple external requests from occurring concurrently and causing confusion in the timing of startup and shutdown.
[0062] In some embodiments, determining whether the global running flag satisfies the global uniqueness condition through a singleton instance includes: viewing the global running flag through the singleton instance; if the global running flag is running, then the uniqueness condition is not met; if the global running flag is not running, then the uniqueness condition is met.
[0063] The global running flag (isRunning) records the execution status of the Bluetooth scanning task using a boolean value (true / false).
[0064] Specifically, when checking the singleton instance of ScannerWorker, if isRunning is false, meaning the global running flag is not running, it indicates that Bluetooth scanning is idle, satisfying the uniqueness condition; if isRunning is true, meaning the global running flag is running, it indicates that a scanning process is already being executed, and the Bluetooth scanning request is a duplicate request, not satisfying the uniqueness condition.
[0065] In the above embodiments, the Bluetooth scanning status is determined by a global running flag, ensuring that only one Bluetooth scanning process exists at the same time, and avoiding duplicate creation and startup caused by multiple threads or events.
[0066] In some embodiments, if the globally unique condition is met, the Bluetooth startup callback method is invoked based on the Bluetooth hardware's scanning flag, including: if the globally unique condition is met, setting the global running flag to running through a singleton instance; checking the Bluetooth hardware's scanning flag through the scheduler; if the scanning flag is scanning, stopping the current scanning task and setting the scanning flag to not scanning; after stopping the current scanning task for a preset duration, invoking the Bluetooth startup callback method; and setting the scanning flag to scanning.
[0067] The scan flag (isScanning) records the execution status of the Bluetooth hardware using a boolean value (true / false).
[0068] Optionally, if the scan flag is not scanned, the Bluetooth startup callback method is invoked; the scan flag is then set to scan in progress.
[0069] Specifically, if the singleton instance of ScannerWorker determines that isRunning is false, then the subsequent process is executed, setting isRunning to true (setting the global run flag to running), and checking the Bluetooth hardware's scan flag (isScanning) through the scheduler (Handler).
[0070] If the Handler checks if isScanning is true, it means the Bluetooth hardware is scanning. Stopping the current scanning task via the Handler automatically sets isScanning to false. Also, by executing a delay via the Handler, the Bluetooth startup callback method is called when the delay reaches the preset duration, and isScanning is automatically set to true. Calling the Bluetooth startup callback method after the Bluetooth hardware has stopped scanning for the preset duration is to give the Bluetooth hardware enough time to clean up and reset.
[0071] In practical applications, the Handler calls the Bluetooth hardware's stop scanning method to stop the current scanning task. The Bluetooth hardware's stop scanning method can be stopScan(). The preset duration can be set according to actual needs, for example, the preset duration can be set to 50ms.
[0072] Optionally, if the Handler checks isScanning and finds it to be false, it indicates that the Bluetooth hardware is idle, and the Bluetooth startup callback method is called; isScanning is automatically set to true.
[0073] In the above embodiments, the global run flag is a global lock, and the scan flag is a physical lock of the Bluetooth hardware. When both the global lock and the physical lock meet the requirements for a unique scan, the actual hardware operation is triggered by a callback. Through the dual-state lock mechanism, a rigorous and orderly global unique scan is formed, preventing multiple external requests from occurring concurrently and causing confusion in the timing of start-up and stop. Each task execution starts from a clean initial context and executes a predefined complete process, reducing unpredictable behavior caused by residual states from previous operations or improper handling of interruptions, and improving the accuracy of periodic Bluetooth scans.
[0074] In some embodiments, the method further includes: in response to a stop scanning request, setting a global run flag to not run via a singleton instance; setting a scan flag to not run; and calling a callback method for Bluetooth stop.
[0075] The stop scan request can be triggered by the user or by a specific event.
[0076] For example, if a Bluetooth scanning request is triggered by detecting movement of an object in the rear area of the vehicle, and no paired key or mobile terminal is detected in the vicinity within a certain period of time, a stop scanning request can be automatically triggered.
[0077] The callback method for stopping Bluetooth is called to stop the periodic Bluetooth scan.
[0078] Specifically, in response to a stop scanning request, the electronic device calls the Bluetooth stop callback method through a singleton instance of the scanning worker to stop periodic Bluetooth scanning. The scanning worker sets isRunning to false to release global scanning control and sets isScanning to false to achieve state synchronization.
[0079] Optionally, the callback methods for Bluetooth startup and Bluetooth shutdown are pre-registered.
[0080] Specifically, Bluetooth start callback methods and Bluetooth stop callback methods are pre-registered in BluetoothHelper; in response to a Bluetooth scan request, BluetoothHelper obtains a scanner, and the scanner calls the Bluetooth start callback method to start performing periodic Bluetooth scans; in response to a stop scan request, the scanner calls the Bluetooth stop callback method to stop performing periodic Bluetooth scans.
[0081] In the above embodiment, the global running flag is set to not running, the scanning flag is set to not running, and then the Bluetooth stop callback method is called to realize the synchronization of global lock release, hardware lock release, and stopping periodic Bluetooth scanning, thereby achieving closed-loop control of periodic Bluetooth scanning.
[0082] In some embodiments, the method further includes: in response to a Bluetooth scanning request, obtaining a scene identifier of the vehicle; the scene identifier is determined by the vehicle state and the environment in which the vehicle is located; and obtaining a first delay and a second delay corresponding to the scene identifier.
[0083] Specifically, the vehicle terminal determines the scene identifier in real time based on the vehicle status and the environment in which the vehicle is located, and obtains the first delay and the second delay corresponding to the scene identifier. The scanning worker obtains the first delay and the second delay corresponding to the scene identifier. When scheduling the first running task, the first running task is set to be executed after the first delay. When scheduling the second running task, the second running task is set to be executed after the second delay.
[0084] Optionally, the vehicle status includes: driving status, parking status, locked status, or charging status; the vehicle environment includes: road environment, parking lot environment, private garage environment, or charging environment; a scene identifier is determined based on the vehicle status and the vehicle environment, and the first delay and second delay corresponding to the scene identifier are obtained.
[0085] The first delay is the duration for stopping scanning, and the second delay is the duration for starting scanning. The first and second delays corresponding to the scene identifier can be set according to actual needs.
[0086] For example, the vehicle state is driving, the vehicle environment is road environment, and the scene identifier is safe driving identifier. The first delay and the second delay corresponding to the safe driving identifier are obtained; for example, the first delay is 30s and the second delay is 100ms, that is, the scanning cycle is: scan for 100ms and stop for 30s.
[0087] The vehicle is in a parked state, the vehicle is in a parking lot environment, and the scene identifier is a public standby identifier. Obtain the first and second delays corresponding to the public standby identifier; for example, the first delay is 3s and the second delay is 200ms, that is, the scanning cycle is: scan for 200ms and stop for 3s.
[0088] The vehicle is in a parked state, and the environment in which the vehicle is located is a private garage environment. The scene identifier is a private standby identifier. Obtain the first delay and the second delay corresponding to the private standby identifier; for example, the first delay is 5s and the second delay is 100ms, that is, the scanning cycle is: scan for 100ms and stop for 5s.
[0089] The vehicle is locked. The vehicle's environment is either a road or a parking lot. The scene identifier is a security monitoring identifier. The system obtains the first and second delays corresponding to the security monitoring identifier. For example, the first delay is 10 seconds and the second delay is 200 milliseconds, meaning the scanning cycle is 200 milliseconds for scanning and 10 seconds for stopping.
[0090] The vehicle is locked and in a private garage environment. The scene identifier is a sleep identifier. The first and second delays corresponding to the sleep identifier are obtained. For example, the first delay is 20 seconds and the second delay is 100 milliseconds, which means the scanning cycle is: 100 milliseconds for scanning and 20 seconds for stopping.
[0091] The vehicle is in a charging state, the vehicle is in a charging environment, and the scene identifier is a charging service identifier. The first delay and the second delay corresponding to the charging service identifier are obtained. For example, the first delay is 2 seconds and the second delay is 100 milliseconds, that is, the scanning cycle is: scan for 100 milliseconds and stop for 2 seconds.
[0092] In the above embodiments, the current scene identifier is determined by the vehicle status and the environment in which the vehicle is located, and the first delay and the second delay are determined based on the current scene identifier. That is, the Bluetooth scanning cycle is adaptively determined by environmental perception. The dynamic adjustment of the first delay and the second delay avoids the waste of resources or insufficient detection efficiency caused by a fixed Bluetooth cycle. By dynamically adjusting the delay parameters, the scanning cycle can be flexibly adapted to different scene requirements.
[0093] The Bluetooth scanning method provided in this application, in response to a Bluetooth scanning request, calls a Bluetooth startup callback method. Within this callback method, a first running task is scheduled, and the first running task executes after a first delay. During the execution of the first running task, a start scanning callback function is called, and a second running task is scheduled. The second running task executes after a second delay. During the execution of the second running task, a stop scanning callback function is called, and the first running task is scheduled, and the first running task executes after the first delay. In related technologies, complex calls are implemented through a large amount of code. Periodic Bluetooth scanning requires rereading and rewriting the same calling code, resulting in code redundancy. Furthermore, the implementation is complex and prone to timing errors and status judgment errors, leading to abnormal periodic Bluetooth scanning. In this application embodiment... By scheduling the first and second running tasks together and setting delays for their execution after scheduling, periodic Bluetooth scanning is achieved. This eliminates the need for redundant calling code and complex function implementations, making the code for periodic Bluetooth scanning more concise and easier to maintain. Furthermore, the mutual calls between runnable tasks transform complex process control issues into clear task management and scheduling problems, effectively improving the timing and logical accuracy of periodic Bluetooth scanning. In addition, the first and second delays can be customized to form a configurable scanning cycle, allowing the scanning cycle to flexibly adapt to different scenario requirements. Moreover, periodic Bluetooth scanning avoids the resource waste of continuous Bluetooth scanning and optimizes the balance between scanning efficiency and system energy efficiency.
[0094] It should be understood that although the steps in the flowcharts of the above embodiments are shown sequentially according to the arrows, these steps are not necessarily executed in the order indicated by the arrows. Unless explicitly stated herein, there is no strict order restriction on the execution of these steps, and they can be executed in other orders. Moreover, at least some steps in the flowcharts of the above embodiments may include multiple steps or multiple stages. These steps or stages are not necessarily completed at the same time, but can be executed at different times. The execution order of these steps or stages is not necessarily sequential, but can be performed alternately or in turn with other steps or at least some of the steps or stages of other steps.
[0095] Figure 4 A schematic diagram of the Bluetooth scanning device provided in this application is shown below. Figure 4 As shown, the Bluetooth scanning device 40 provided in this embodiment includes:
[0096] The startup module 401 is used to call the Bluetooth startup callback method in response to a Bluetooth scanning request;
[0097] The first scheduling module 402 is used inside the Bluetooth startup callback method to schedule the first running task that stops after startup, and the first running task is executed after a first delay.
[0098] The second scheduling module 403 is used to start the second running task after the scheduling stops when the first running task calls the start scanning callback function. The second running task is executed after the second delay.
[0099] The third scheduling module 404 is used to schedule the first running task when the second running task calls the stop scanning callback function. The first running task is executed after a first delay.
[0100] In some embodiments, the startup module is further configured to, in response to a Bluetooth scanning request, access a singleton instance of the scanning worker; determine, through the singleton instance, whether the global running flag satisfies the globally unique condition; and, if the globally unique condition is satisfied, invoke the Bluetooth startup callback method based on the scanning flag of the Bluetooth hardware.
[0101] In some embodiments, the startup module is further configured to view the global running flag through a singleton instance; if the global running flag is running, the uniqueness condition is not met; if the global running flag is not running, the uniqueness condition is met.
[0102] In some embodiments, the startup module is further configured to, under the condition of global uniqueness, set the global running flag to running through a singleton instance; view the scanning flag of the Bluetooth hardware through the scheduler; stop the current scanning task and set the scanning flag to not scan when the scanning flag is scanning; after the current scanning task has been stopped for a preset time, call the Bluetooth startup callback method when the scanning flag is not scanned; and set the scanning flag to scanning.
[0103] In some embodiments, the Bluetooth scanning device further includes: a stop module, configured to, in response to a stop scanning request, set a global run flag to not run via a singleton instance; set a scan flag to not run; and call back the Bluetooth stop callback method.
[0104] In some embodiments, the Bluetooth scanning device further includes: a delay determination module, configured to, in response to a Bluetooth scanning request, acquire a scene identifier of the vehicle; the scene identifier is determined by the vehicle state and the environment in which the vehicle is located; and acquire a first delay and a second delay corresponding to the scene identifier.
[0105] The Bluetooth scanning device provided in this embodiment can execute the Bluetooth scanning method provided in the above method embodiment. Its implementation principle and technical effect are similar, and will not be described in detail here.
[0106] The Bluetooth scanning device provided in this application embodiment, in response to a Bluetooth scanning request, calls a Bluetooth startup callback method. Within this callback method, a first running task is scheduled, and the first running task executes after a first delay. During the execution of the first running task, a start scanning callback function is called, and a second running task is scheduled. The second running task executes after a second delay. During the execution of the second running task, a stop scanning callback function is called, and the first running task is scheduled, executing after the first delay. In related technologies, complex calls are implemented through a large amount of code. Periodic Bluetooth scanning requires rereading and rewriting the same call code, resulting in code redundancy. Furthermore, the implementation is complex and prone to timing errors and incorrect state judgments. Errors can cause periodic Bluetooth scanning anomalies. In this embodiment, periodic Bluetooth scanning is achieved by scheduling the first and second running tasks together and setting delays for executing the first and second running tasks after scheduling. This eliminates the need for redundant calling code and complex function implementations, making the code for periodic Bluetooth scanning more concise and easier to maintain. Moreover, by calling each other between runnable tasks, the complex process control problem is transformed into a clear task management and scheduling problem, effectively improving the timing and logical accuracy of periodic Bluetooth scanning. Furthermore, the first and second delays can be customized to form a configurable scanning cycle, allowing the scanning cycle to flexibly adapt to different scenario requirements.
[0107] Figure 5 A schematic diagram of the structure of the electronic device provided in this application. Figure 5 As shown, the electronic device 50 provided in this embodiment includes at least one processor 501 and a memory 502. Optionally, the device 50 further includes a communication component 503. The processor 501, memory 502, and communication component 503 are connected via a bus.
[0108] In a specific implementation, at least one processor 501 executes computer execution instructions stored in memory 502, causing at least one processor 501 to perform the above-described method.
[0109] The specific implementation process of processor 501 can be found in the above method embodiments, and its implementation principle and technical effect are similar. It will not be repeated here.
[0110] In the above embodiments, it should be understood that the processor can be a Central Processing Unit (CPU), or other general-purpose processors, digital signal processors (DSPs), application-specific integrated circuits (ASICs), etc. The general-purpose processor can be a microprocessor or any conventional processor. The steps of the method disclosed in this invention can be directly implemented by a hardware processor, or implemented by a combination of hardware and software modules within the processor.
[0111] The memory may include random access memory (RAM) and may also include non-volatile memory (NVM), such as at least one disk storage device.
[0112] The bus can be an Industry Standard Architecture (ISA) bus, a Peripheral Component Interconnect (PCI) bus, or an Extended Industry Standard Architecture (EISA) bus, etc. Buses can be categorized as address buses, data buses, control buses, etc. For ease of illustration, the buses shown in the accompanying drawings are not limited to a single bus or a single type of bus.
[0113] This application also provides a computer program product, including a computer program that, when executed by a processor, implements the above-described method.
[0114] This application also provides a computer-readable storage medium storing computer-executable instructions, which, when executed by a processor, implement the above-described method.
[0115] The aforementioned readable storage medium can be implemented by any type of volatile or non-volatile storage device or a combination thereof, such as static random access memory (SRAM), electrically erasable programmable read-only memory (EEPROM), erasable programmable read-only memory (EPROM), programmable read-only memory (PROM), read-only memory (ROM), magnetic storage, flash memory, magnetic disk, or optical disk. The readable storage medium can be any available medium accessible to a general-purpose or special-purpose computer.
[0116] An exemplary readable storage medium is coupled to a processor, enabling the processor to read information from and write information to the readable storage medium. Of course, the readable storage medium can also be a component of the processor. The processor and the readable storage medium can reside in an Application Specific Integrated Circuit (ASIC). Alternatively, the processor and the readable storage medium can exist as discrete components in the device.
[0117] The division of units is merely a logical functional division; in actual implementation, there may be other division methods. For example, multiple units or components may be combined or integrated into another system, or some features may be ignored or not executed. Furthermore, the coupling or direct coupling or communication connection shown or discussed may be indirect coupling or communication connection through some interfaces, devices, or units, and may be electrical, mechanical, or other forms.
[0118] The units described as separate components may or may not be physically separate. The components shown as units may or may not be physical units; that is, they may be located in one place or distributed across multiple network units. Some or all of the units can be selected to achieve the purpose of this embodiment according to actual needs.
[0119] In addition, the functional units in the various embodiments of the present invention can be integrated into one processing unit, or each unit can exist physically separately, or two or more units can be integrated into one unit.
[0120] If a function is implemented as a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of this invention, or the part that contributes to the prior art, or a part of the technical solution, can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute all or part of the steps of the methods of the various embodiments of this invention. The aforementioned storage medium includes various media capable of storing program code, such as USB flash drives, portable hard drives, read-only memory (ROM), random access memory (RAM), magnetic disks, or optical disks.
[0121] Those skilled in the art will understand that all or part of the steps of the above-described method embodiments can be implemented by hardware related to program instructions. The aforementioned program can be stored in a computer-readable storage medium. When executed, the program performs the steps of the above-described method embodiments; and the aforementioned storage medium includes various media capable of storing program code, such as ROM, RAM, magnetic disks, or optical disks.
[0122] Finally, it should be noted that other embodiments of the invention will readily occur to those skilled in the art upon consideration of the specification and practice of the invention disclosed herein. This invention is intended to cover any variations, uses, or adaptations of the invention that follow the general principles of the invention and include common knowledge or customary techniques in the art not disclosed herein, and is not limited to the precise structures described above and shown in the accompanying drawings, and various modifications and changes can be made without departing from its scope. The scope of the invention is limited only by the appended claims.
Claims
1. A Bluetooth scanning method, characterized in that, include: In response to a Bluetooth scan request, the Bluetooth startup callback method is invoked; Inside the Bluetooth startup callback method, a first running task that was stopped after startup is scheduled, and the first running task is executed after a first delay; When the first running task calls the start scan callback function, the second running task, which was started after the scheduling stopped, is executed after the second running task has a second delay. When the second running task calls the stop scanning callback function, the first running task is scheduled and executed after the first delay.
2. The method according to claim 1, characterized in that, The callback method for invoking Bluetooth startup in response to a Bluetooth scanning request includes: In response to a Bluetooth scan request, access the singleton instance of the scan worker; The singleton instance is used to determine whether the global runtime flag satisfies the condition of global uniqueness. If the condition of global uniqueness is met, the Bluetooth startup callback method is invoked based on the scanning flag of the Bluetooth hardware.
3. The method according to claim 2, characterized in that, The step of determining whether the global runtime flag satisfies the globally unique condition through the singleton instance includes: View the global runtime flags through the singleton instance; If the global running flag is currently running, then the uniqueness condition is not met; If the global running flag is not running, then the uniqueness condition is satisfied.
4. The method according to claim 2, characterized in that, Under the condition of global uniqueness, the Bluetooth startup callback method is invoked based on the scanning flag of the Bluetooth hardware, including: Under the condition of global uniqueness, the global running flag is set to running through the singleton instance; View the scanning flags of Bluetooth hardware through the scheduler; If the scan flag indicates that scanning is in progress, the current scan task is stopped, and the scan flag is set to "not scanned". After the current scanning task has been stopped for a preset duration, the Bluetooth startup callback method is invoked; Set the scan flag to scan in progress.
5. The method according to claim 2, characterized in that, The method further includes: In response to a stop scanning request, the global run flag is set to not running via the singleton instance; Set the scan flag to not running; Call the callback method for when Bluetooth is stopped.
6. The method according to any one of claims 1 to 5, characterized in that, The method further includes: In response to a Bluetooth scanning request, the scene identifier of the vehicle is obtained; the scene identifier is determined by the vehicle status and the environment in which the vehicle is located. Obtain the first delay and the second delay corresponding to the scene identifier.
7. A Bluetooth scanning device, characterized in that, The device includes: The startup module is used to call the Bluetooth startup callback method in response to a Bluetooth scan request; The first scheduling module is used within the Bluetooth startup callback method to schedule a first running task that stops after startup, and the first running task is executed after a first delay. The second scheduling module is used to start the second running task after the scheduling stops when the first running task calls the start scanning callback function, and the second running task is executed after a second delay. The third scheduling module is used to schedule the first running task when the second running task calls the stop scanning callback function, and the first running task is executed after the first delay.
8. An electronic device, characterized in that, include: A processor, and a memory communicatively connected to the processor; The memory stores computer-executed instructions; The processor executes computer execution instructions stored in the memory to implement the method as described in any one of claims 1 to 6.
9. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores computer-executable instructions, which, when executed by a processor, are used to implement the method as described in any one of claims 1 to 6.
10. A computer program product, characterized in that, Includes computer execution instructions, which, when executed by a processor, implement the method as described in any one of claims 1 to 6.