A method and device for a HarmonyOS device to communicate with a Bluetooth device

By presetting the SDK in the APP of the Hongmeng system device, using child threads to process Bluetooth communication instructions and switching between the main thread and the child thread, the asynchronous processing problem of communication between the Hongmeng system device and the Bluetooth device is solved, synchronous processing and stable communication effects are achieved, and development and maintenance costs are reduced.

CN120111465BActive Publication Date: 2025-07-08FEITIAN TECHNOLOGIES CO LTD
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202510405193.7
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2025-04-02
Publication Date
2025-07-08
Estimated Expiration
2045-04-02

AI Technical Summary

Technical Problem

The business logic processing methods of Hongmeng system devices and Bluetooth devices are different, resulting in the APPs in existing Android system devices or iOS system devices being unable to communicate with Bluetooth devices on Hongmeng system devices.

Method used

By presetting the SDK in the APP of the Hongmeng system device, using child threads to process Bluetooth communication instructions, and switching between the main thread and the child thread, synchronous processing is achieved, combining Bluetooth connection status monitoring and data status monitoring to ensure the correct order and stability of communication.

Benefits of technology

It realizes the synchronization processing of communication between Hongmeng system equipment and Bluetooth equipment, supports multi-threaded work, reduces development and maintenance costs, and improves the stability of communication and the correct order of business operations.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120111465B_ABST
    Figure CN120111465B_ABST
Patent Text Reader

Abstract

The present invention discloses a method and device for a HarmonyOS device to communicate with a Bluetooth device. The method includes: when the communication interface is called by an APP, the SDK saves the service parameters passed in by the communication interface, creates a main-thread-safe function, starts a child thread, generates a Bluetooth communication instruction according to the service parameters in the child thread, calls the switching interface with the main-thread-safe function as a parameter to switch the currently running thread from the child thread back to the main thread, performs a locking process on the child thread, calls the sending interface to send the Bluetooth communication instruction to the Bluetooth device, receives the response data returned by the Bluetooth device within a set time, unlocks the child thread, obtains the response result from the response data in the child thread, calls the switching interface with the main-thread-safe function as a parameter to switch the currently running thread from the child thread back to the main thread, and the SDK returns the response result to the APP.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of information security, and in particular to a method and device for a HarmonyOS device to communicate with a Bluetooth device. Background Art

[0002] In the prior art, the business logic programming languages of HarmonyOS devices, Android devices, and iOS devices are different, and the underlying business logic of HarmonyOS requires asynchronous processing, while the business logic of Bluetooth devices (such as Bluetooth Keys) requires synchronous processing. As a result, the SDK (English full name: Software Development Kit, Chinese full name: Software Development Kit) for implementing communication with Bluetooth devices in the APP (application program) set in Android devices or iOS devices cannot be used on HarmonyOS devices. Therefore, there is an urgent need to provide a technical solution that can enable a HarmonyOS device to communicate with a Bluetooth device. Summary of the Invention

[0003] The purpose of the present invention is to overcome the deficiencies of the prior art and provide a method and device for a HarmonyOS device to communicate with a Bluetooth device.

[0004] In a first aspect, an embodiment of the present invention provides a method for a HarmonyOS device to communicate with a Bluetooth device. The HarmonyOS device is installed with an APP, and the SDK is pre-set in the APP. The method includes:

[0005] Step S1: When the communication interface of the SDK is called by the APP, the SDK saves the service parameters passed in by the communication interface and creates a main-thread-safe function;

[0006] Step S2: The SDK starts a child thread and generates a Bluetooth communication instruction according to the saved service parameters in the child thread;

[0007] Step S3: The SDK uses the main-thread-safe function as a parameter to call the switching interface of the HarmonyOS to switch the currently running thread from the child thread back to the main thread and perform a locking process on the child thread;

[0008] Step S4: The SDK calls the sending interface of the HarmonyOS to send the Bluetooth communication instruction to the Bluetooth device;

[0009] Step S5: The SDK waits to receive the response data returned by the Bluetooth device and determines whether the waiting time times out. If so, an error is reported; otherwise, step S6 is executed;

[0010] Step S6: The SDK receives the response data returned by the Bluetooth device and caches it;

[0011] Step S7: The SDK unlocks the sub-thread, obtains the response result from the response data in the sub-thread, and calls the switching interface with the main-thread safe function as a parameter to switch the currently running thread from the sub-thread back to the main-thread;

[0012] Step S8: The SDK returns the response result to the APP.

[0013] In a second aspect, an embodiment of the present invention further provides a device for a HarmonyOS device to communicate with a Bluetooth device. The HarmonyOS device is installed with an APP, and the device is pre-disposed in the APP. The device includes:

[0014] A saving and creating module, configured to save the service parameters passed in by the communication interface and create a main-thread safe function when the communication interface is called by the APP;

[0015] An enabling and generating module, configured to enable a sub-thread and generate a Bluetooth communication instruction in the sub-thread according to the service parameters saved by the saving and creating module;

[0016] A switching and locking module, configured to call the switching interface with the main-thread safe function as a parameter to switch the currently running thread from the sub-thread back to the main-thread and perform a locking process on the sub-thread itself;

[0017] A sending module, configured to call the sending interface of the HarmonyOS to send the Bluetooth communication instruction to the Bluetooth device;

[0018] A first judgment module, configured to wait for receiving the response data returned by the Bluetooth device and judge whether the waiting time times out. If so, an error is reported; otherwise, the receiving buffer module is triggered;

[0019] The receiving buffer module is configured to receive and cache the response data returned by the Bluetooth device;

[0020] An unlocking and switching module, configured to unlock the sub-thread, obtain the response result from the response data in the sub-thread, and call the switching interface with the main-thread safe function as a parameter to switch the currently running thread from the sub-thread to the main-thread;

[0021] A returning module, configured to return the response result to the APP.

[0022] In a third aspect, an embodiment of the present invention further provides an electronic device, characterized in that the electronic device includes at least one processor, a memory, and instructions stored on the memory and executable by the at least one processor. The at least one processor executes the instructions to implement the steps of the method described in any one of the above.

[0023] Fourthly, an embodiment of the present invention further provides a computer-readable storage medium, which includes a computer program. When the computer program runs on an electronic device, the electronic device is caused to execute the steps of any one of the above-mentioned methods.

[0024] Fifthly, an embodiment of the present invention further provides a computer program product, including a computer program / instructions. When the computer program / instructions are executed by a processor, the steps of any one of the above-mentioned methods are implemented.

[0025] Compared with the prior art, the present invention has the following advantages: The present invention realizes data communication between a HarmonyOS device and a Bluetooth device by creating a sub-thread, supports multi-threaded work of the HarmonyOS device, achieves the effect of communication synchronization processing, and can ensure the correct sequence of business operations; and the code for business operations (i.e., generating communication instructions and obtaining response results) in the existing SDK for communicating with Bluetooth devices in Android devices and iOS devices can be reused, reducing development and maintenance costs. Description of the Drawings

[0026] Figure 1 It is a flowchart of a method for a HarmonyOS device to communicate with a Bluetooth device provided in Embodiment 1 of the present invention;

[0027] Figure 2 It is a flowchart of a connection establishment process between a HarmonyOS device and a Bluetooth device provided in Embodiment 2 of the present invention;

[0028] Figure 3 It is a flowchart of a data communication process in a method for a HarmonyOS device to communicate with a Bluetooth device provided in Embodiment 3 of the present invention. Detailed Embodiments

[0029] The present application proposes a method and device for a HarmonyOS device to communicate with a Bluetooth device. The following will describe the specific embodiments of the present application in detail with reference to the drawings. Examples of the embodiments are shown in the drawings. The embodiments described below with reference to the drawings are exemplary and are only used to explain the present application, and cannot be construed as a limitation of the present application.

[0030] Those skilled in the art of the present technology can understand that unless otherwise defined, all terms (including technical terms and scientific terms) used here have the same meaning as the general understanding of those of ordinary skill in the art to which the present application belongs. It should also be understood that terms such as those defined in a general dictionary should be understood to have a meaning consistent with the meaning in the context of the prior art, and will not be interpreted with an idealized or overly formal meaning unless specifically defined as here.

[0031] To make the objectives, technical solutions and advantages of the present invention more clear, the following further describes the embodiments of the present invention in detail with reference to the accompanying drawings.

[0032] In this embodiment, an APP is installed in a HarmonyOS device (such as a HarmonyOS mobile phone), and an SDK is pre-installed in the APP. When the user operates on the APP, the APP calls the SDK and then starts to execute the method of this embodiment.

[0033] Embodiment 1

[0034] Embodiment 1 of the present invention provides a method for a HarmonyOS device to communicate with a Bluetooth device. As Figure 1 shown, the method of this embodiment includes:

[0035] Step S1: When the communication interface of the SDK is called by the APP, the SDK saves the service parameters passed in by the communication interface and creates a main-thread-safe function;

[0036] In this embodiment, if the communication interface in this embodiment is a PIN verification interface, the service parameter passed in is the PIN code; if the communication interface is a signature interface, the service parameter passed in is the data to be signed; if the communication interface is a device information acquisition interface, the service parameter passed in is the device serial number, etc.;

[0037] Step S2: The SDK starts a sub-thread, and generates a Bluetooth communication instruction in the sub-thread according to the saved service parameters;

[0038] Step S3: The SDK uses the main-thread-safe function as a parameter to call the switching interface of the HarmonyOS to switch the currently running thread from the sub-thread back to the main thread, and locks the sub-thread;

[0039] Step S4: The SDK calls the sending interface of the HarmonyOS to send the Bluetooth communication instruction to the Bluetooth device;

[0040] Optionally, in this embodiment, Step S4 includes: the SDK determines whether the length of the Bluetooth communication instruction is greater than the mtu (full Chinese name: Maximum Transmission Unit, English full name: Maximum Transmission Unit) value. If so, the Bluetooth communication instruction is packetized according to the mtu value, and the sending interface of the HarmonyOS is called in sequence to send each packet of data to the Bluetooth device. Otherwise, the sending interface of the HarmonyOS is called to send the Bluetooth communication instruction to the Bluetooth device;

[0041] After receiving the Bluetooth communication instruction, the Bluetooth device performs corresponding processing, and generates response data according to the processing result. If the length of the response data is greater than the mtu value, the Bluetooth device packetizes the response data and returns each packet of data to the SDK in sequence. Otherwise, the Bluetooth device directly returns the response data to the SDK;

[0042] Step S5: The SDK waits to receive the response data returned by the Bluetooth device and determines whether the waiting time has timed out. If it has, an error is reported; otherwise, step S6 is executed;

[0043] Optionally, between step S4 and step S5, the following may also be included: enabling a data reception timeout timer;

[0044] Correspondingly, determining whether the waiting time has timed out specifically includes: determining whether the waiting time exceeds the set time of the data reception timeout timer. If it does, it has timed out; otherwise, it has not timed out;

[0045] Step S6: The SDK receives the response data returned by the Bluetooth device and caches it;

[0046] Optionally, in this embodiment, step S6 includes: the SDK receives the response data returned by the Bluetooth device and caches it sequentially, verifies the integrity of the cached response data. If it is complete, step S7 is executed; if it is incomplete, continue to wait to receive the response data returned by the Bluetooth device;

[0047] Optionally, between step S6 and step S7, the following is also included: disabling the data reception timeout timer;

[0048] Step S7: The SDK unlocks the child thread, obtains the response result from the response data in the child thread, and calls the switching interface with the main thread security function as a parameter to switch the currently running thread from the child thread back to the main thread;

[0049] Step S8: The SDK returns the response result to the APP.

[0050] In this embodiment, after receiving the response result, the APP prompts a success message.

[0051] Optionally, step S4 in this embodiment includes: the SDK checks the Bluetooth connection status. If the Bluetooth connection status is successful, it sets a data status monitoring callback and calls the sending interface of the HarmonyOS system to send the Bluetooth communication instruction to the Bluetooth device. If the Bluetooth connection status is otherwise, an error is reported;

[0052] Correspondingly, step S6 includes: the SDK receives the response data returned by the Bluetooth device through the data status monitoring callback and caches it sequentially.

[0053] In this embodiment, during the communication between the HarmonyOS device and the Bluetooth device, the user may accidentally turn off the device Bluetooth without knowing it, and the APP does not give any reminder, resulting in the interruption of the communication between the HarmonyOS device and the Bluetooth device. To solve this problem, before step S1 in the method of this embodiment, the following is also included: setting a Bluetooth status monitoring callback when the HarmonyOS device and the Bluetooth device successfully establish a Bluetooth connection;

[0054] The method of this embodiment further includes: when the SDK receives the Bluetooth off state information returned by the Bluetooth state monitoring callback, cancel the data state monitoring callback and the connection state monitoring callback, modify the Bluetooth connection state to unconnected, close the gatt object, and return the device disconnected Bluetooth connection information to the APP;

[0055] After the APP receives the device disconnected Bluetooth connection information, it reminds the user to reopen the device Bluetooth and enter the serial number of the Bluetooth device to be connected;

[0056] When the SDK receives the Bluetooth on state information returned by the Bluetooth state monitoring callback, obtain the corresponding cached device object data according to the serial number re-entered by the user, obtain the MAC address of the Bluetooth device from the device object data, create a gatt object according to the MAC address of the Bluetooth device, set the connection state monitoring callback according to the gatt object, call the connection interface of the HarmonyOS system according to the gatt object to start the connection operation with the Bluetooth device, obtain the Bluetooth connection state through the connection state monitoring callback. If the Bluetooth connection state is successful, save the Bluetooth connection state, obtain the Bluetooth service and characteristic values from the HarmonyOS system, set to receive Bluetooth characteristic value notifications, set the mtu value, and return the connection success information to the APP. Otherwise, return the connection failure information to the APP.

[0057] In this embodiment, before step S1, it further includes the process of establishing a connection between the HarmonyOS device and the Bluetooth device, and this establishment connection process includes:

[0058] Step A1: When the Bluetooth connection interface of the SDK is called by the APP, the SDK obtains the serial number input by the user and sets the screen of the HarmonyOS device to be always on;

[0059] Step A2: The SDK determines whether the HarmonyOS device has Bluetooth function and whether the APP has Bluetooth permission. If so, execute step A3. Otherwise, return an error message to the APP;

[0060] In this embodiment, between step A2 and step A3, it further includes:

[0061] Step B1: The SDK determines whether the HarmonyOS device has already connected a Bluetooth device. If so, execute step B2. Otherwise, execute step A3;

[0062] Specifically, step B1 includes: The SDK determines whether there is a Bluetooth connection object and a gatt object in the HarmonyOS device. If so, it determines that the HarmonyOS device has already connected a Bluetooth device and executes step B2. Otherwise, it determines that the HarmonyOS device has not connected a Bluetooth device and executes step A3;

[0063] Step B2: The SDK determines whether the serial number of the already connected Bluetooth device is the same as the obtained serial number. If they are the same, the SDK returns the information of the connected Bluetooth device to the APP. If they are different, the SDK disconnects the connected Bluetooth device and executes Step A3;

[0064] Optionally, before executing Step A3 in Step B2, it further includes: The SDK queries whether there is device object data in the device cache list according to the obtained serial number. If so, it obtains the MAC address of the Bluetooth device from the device object data and executes Step A7. Otherwise, it executes Step A3;

[0065] Step A3: The SDK sets the Bluetooth scan parameter configuration and the Bluetooth scan filtering policy, and sets the Bluetooth scan result listening callback;

[0066] Specifically, in this embodiment, the SDK setting the Bluetooth scan parameter configuration includes: The SDK configures the high-power mode or the balanced mode in the scan parameter mode, and sets the scan result reporting time interval to 0;

[0067] Step A4: The SDK starts Bluetooth scanning and obtains the scanned Bluetooth device broadcast data through the Bluetooth scan result listening callback;

[0068] Step A5: The SDK parses the scanned Bluetooth device broadcast data and finds the MAC address of the Bluetooth device from the parsing result according to the obtained serial number;

[0069] Step A6: The SDK stops Bluetooth scanning, cancels the Bluetooth scan result listening callback, forms device object data with the MAC address and serial number of the Bluetooth device and caches it;

[0070] Optionally, in this embodiment, Step A4 includes: The SDK creates a first timer, starts Bluetooth scanning, and determines whether the scanning times out according to the first timer. If so, it returns a connection failure message to the APP. Otherwise, it obtains the scanned Bluetooth device broadcast data through the Bluetooth scan result listening callback;

[0071] Correspondingly, Step A6 further includes: Closing the first timer;

[0072] Step A7: The SDK creates a gatt object according to the MAC address of the Bluetooth device, sets the connection status listening callback according to the gatt object, calls the connection interface of the HarmonyOS to start the connection operation with the Bluetooth device according to the gatt object, obtains the Bluetooth connection status through the connection status listening callback. If the Bluetooth connection status is successful, it saves the Bluetooth connection status and executes Step A8. Otherwise, it returns a connection failure message to the APP;

[0073] Step A8: The SDK obtains the Bluetooth service and characteristic values from the HarmonyOS, sets up receiving Bluetooth characteristic value notifications, sets the mtu value, and returns a connection success message to the APP.

[0074] The method of this embodiment realizes data communication between the HarmonyOS device and the Bluetooth device by creating a child thread, supports multi-threaded work of the HarmonyOS device, achieves the effect of communication synchronization processing, and can ensure the correct order of business operations; moreover, the code for business operations (i.e., generating communication instructions and obtaining response results) in the existing SDK for communicating with Bluetooth devices in Android system devices and iOS system devices can be reused, reducing development and maintenance costs.

[0075] Embodiment Two

[0076] Embodiment Two of the present invention provides a process for establishing a connection between a HarmonyOS device and a Bluetooth device, as Figure 2 shown. The connection establishment process includes:

[0077] When the user performs a Bluetooth connection operation on the APP in the HarmonyOS device, the APP prompts the user to enter a serial number and calls the Bluetooth connection interface of the SDK in the HarmonyOS device, and the SDK starts to execute Step 101;

[0078] Step 101: The SDK obtains the serial number entered by the user;

[0079] Step 102: The SDK sets the screen of the HarmonyOS device to be always on;

[0080] In this embodiment, the SDK calls the first system interface to set the screen of the HarmonyOS device to be always on. Specifically, the first system interface is: setWindowKeepScreenOn(true);

[0081] In this embodiment, by setting the screen to be always on, it prevents the interruption of Bluetooth use when the screen times out;

[0082] Step 103: The SDK determines whether the HarmonyOS device has Bluetooth functionality and whether the APP has Bluetooth permissions. If so, it executes Step 104; otherwise, it returns a connection failure message to the APP;

[0083] In this embodiment, the SDK calls the second system interface to determine whether the HarmonyOS device has Bluetooth functionality. Specifically, the second system interface is: accessBluetooth;

[0084] Call the third system interface to determine whether the current APP has Bluetooth permission. Specifically, the third system interface is: atManager.checkAccessTokenSync(tokenId, “ohos.permission.ACCESS_BLUETOOTH”);

[0085] Step 104: The SDK determines whether a Bluetooth device is connected to the HarmonyOS device. If so, execute Step 105; otherwise, execute Step 106;

[0086] Specifically, Step 104 in this embodiment includes: The SDK determines whether there are Bluetooth connection objects and gatt objects in the HarmonyOS device. If so, it determines that the HarmonyOS device has connected to a Bluetooth device and executes Step 105; otherwise, it determines that the HarmonyOS device is not connected to a Bluetooth device and executes Step 106;

[0087] Step 105: The SDK determines whether the serial number of the already connected device is the same as the obtained serial number. If they are the same, it returns the information of the connected Bluetooth device to the APP; if they are different, it disconnects the connected Bluetooth device and executes Step 106;

[0088] In this embodiment, after the application receives the success information, it displays a prompt to the user;

[0089] Step 106: The SDK queries whether there is device object data in the device cache list according to the obtained serial number. If so, it obtains the MAC address of the Bluetooth device from the device object data and executes Step 112; otherwise, it executes Step 107;

[0090] Step 107: The SDK sets the Bluetooth scan parameter configuration and the Bluetooth scan filtering policy, and sets the Bluetooth scan result listening callback;

[0091] Preferably, in this embodiment, setting the Bluetooth scan parameter configuration includes: configuring the high-power mode or the balanced mode in the scan parameter mode, setting the scan result reporting time interval to 0. By setting the high-power mode or the balanced mode, the Bluetooth scan frequency can be increased, and setting the scan result reporting time interval to 0 can speed up the return speed of the Bluetooth scan result, thereby improving the Bluetooth scan efficiency;

[0092] In this embodiment, the SDK sets the Bluetooth scan parameter configuration through the first object of the HarmonyOS. Specifically, the first object is: ble.ScanOptions;

[0093] The SDK sets the Bluetooth scan filtering policy through the first system method of the HarmonyOS. Specifically, the first system method is: ble.startBLEScan;

[0094] In this embodiment, the SDK setting the Bluetooth scan result monitoring callback specifically includes: binding the callback function bleDeviceFindCallBack to the BLEDeviceFind event through the binding interface of the HarmonyOS system;

[0095] When the HarmonyOS system monitors the occurrence of the BLEDeviceFind event, the HarmonyOS system notifies the SDK that a Bluetooth device is connected to the HarmonyOS device through the callback function bleDeviceFindCallBack; the callback function bleDeviceFindCallBack is used to handle the logic after discovering the Bluetooth device, such as displaying the device list, connecting to the device, etc.;

[0096] The binding interface is ble.on(“BLEDeviceFind”, bleDeviceFindCallBack);

[0097] Step 108: The SDK creates a first timer and starts Bluetooth scanning;

[0098] Step 109: The SDK determines whether the scan times out according to the first timer. If so, it returns a connection failure message to the APP. Otherwise, it obtains the broadcast data of the scanned Bluetooth device through the Bluetooth scan result monitoring callback and executes Step 110;

[0099] The first timer in this embodiment is used for Bluetooth scan timeout processing;

[0100] Step 110: The SDK parses the broadcast data of the scanned Bluetooth device and finds the MAC address of the Bluetooth device from the parsing result according to the obtained serial number;

[0101] Step 111: The SDK stops Bluetooth scanning, cancels the Bluetooth scan result monitoring callback, closes the first timer, forms device object data with the MAC address and serial number of the Bluetooth device and caches it, and executes Step 112;

[0102] In this embodiment, the Bluetooth scan is stopped through the fourth system interface of the HarmonyOS system. Specifically, the fourth system interface is: ble.stopBLEScan();

[0103] The Bluetooth scan result monitoring callback is cancelled through the fifth system interface of the HarmonyOS system. Specifically, the fifth system interface is: ble.off(“BLEDeviceFind”, bleDeviceFindCallBack);

[0104] In this embodiment, the device object data is used for the next connection;

[0105] Step 112: The SDK creates a gatt object based on the MAC address of the Bluetooth device, sets a connection state listening callback according to the gatt object, calls the connection interface of the HarmonyOS based on the gatt object to start the connection operation with the Bluetooth device, obtains the Bluetooth connection state through the connection state listening callback. If the Bluetooth connection state is successful, save the Bluetooth connection state and execute Step 113. Otherwise, return a connection failure message to the APP;

[0106] In this embodiment, the SDK creates a gatt object through the sixth system interface based on the MAC address of the Bluetooth device. Specifically, the sixth system interface is: ble.createGattClientDevice( deviceId );

[0107] In this embodiment, the SDK sets a connection state listening callback through the seventh system interface of the HarmonyOS according to the gatt object. Specifically, the seventh system interface is: gattDevice.on( “BLEConnectionStateChange”,bleConnectionChangeCallBack); The connection interface is gattDevice.connect();

[0108] In this embodiment, the device connection state includes success or failure;

[0109] Step 113: The SDK obtains the Bluetooth service and characteristic values from the HarmonyOS, sets to receive Bluetooth characteristic value notifications, and sets the mtu value;

[0110] In this embodiment, Step 113 includes: The SDK obtains the Bluetooth service and characteristic values from the HarmonyOS through the eighth system interface of the HarmonyOS, sets to receive Bluetooth characteristic value notifications through the ninth system interface of the HarmonyOS, and sets the mtu value through the tenth system interface of the HarmonyOS;

[0111] Specifically, the eighth system interface is: gattDevice.getServices(), the ninth system interface is: gattDevice.setCharacteristicChangeNotification(), and the tenth system interface is: gettDevice.setBLEMtuSize();

[0112] The mtu value in this embodiment is used for the Bluetooth communication packet splitting length, and receiving Bluetooth characteristic value notifications is used to receive Bluetooth data listening notifications during the communication process; The Bluetooth service and characteristic values are used as parameters when calling the sending interface to send Bluetooth data;

[0113] Step 114: The SDK sets a Bluetooth state listening callback and returns a connection success message to the APP;

[0114] In this embodiment, the SDK sets the Bluetooth status monitoring callback through the eleventh system interface of the HarmonyOS. Specifically, the eleventh system interface is: access.on( “stateChange”, bleStateChangeCallBack).

[0115] Embodiment 3

[0116] Embodiment 3 of the present invention provides a method for a HarmonyOS device to communicate with a Bluetooth device. The HarmonyOS device is installed with an APP, and the SDK is pre-installed in the APP. Specifically, taking the example of obtaining the device information of the connected Bluetooth device, as Figure 3 shown, the method of this embodiment includes:

[0117] The user performs an operation to obtain device information on the APP. When the APP receives the trigger information from the user, it prompts the user to input the serial number of the Bluetooth device to be connected. When the APP receives the serial number input by the user, it uses the serial number as a parameter to call the device information acquisition interface of the SDK;

[0118] Step 201: When the device information acquisition interface is called, the SDK saves the serial number passed into the device information acquisition interface and creates a main thread-safe function;

[0119] In this embodiment, a main thread-safe function is created through the fifteenth system interface of the HarmonyOS. Specifically, the fifteenth system interface is: napi_create_threadsafe_function(env, func,...), where the parameter func is the main thread-safe function;

[0120] Step 202: The SDK starts a child thread and generates a device information acquisition instruction in the child thread according to the saved serial number;

[0121] Step 203: The SDK uses the main thread-safe function as a parameter to call the switching interface of the HarmonyOS to switch the currently running thread from the child thread back to the main thread and perform a locking process on the child thread;

[0122] Specifically, the switching interface is: napi_acquire_threadsafe_function( func ), where the parameter func is the main thread-safe function;

[0123] Step 204: The SDK calls the sending interface of the HarmonyOS to send the device information acquisition instruction to the connected Bluetooth device and starts a data reception timeout timer;

[0124] The sending interface is: napi_call_threadsafe_function(func, data, napi_tsfn_blocking), where the parameter func is the safe function, data is the instruction to obtain device information, and napi_tsfn_blocking is the blocking communication mode (non-essential parameter);

[0125] Specifically, in this embodiment, the SDK calls the sending interface of the HarmonyOS to send the instruction to obtain device information to the connected Bluetooth device, including: the SDK judges the saved Bluetooth connection status. If the Bluetooth connection status is successful, it sets the data status monitoring callback and sends the instruction to obtain device information to the Bluetooth device. If the Bluetooth connection status is other, it reports an error;

[0126] If the length of the instruction to obtain device information in this embodiment is greater than the mtu value, packet splitting processing is also required. Then the SDK calls the sending interface of the HarmonyOS to send the instruction to obtain device information to the connected Bluetooth device, including: the SDK judges whether the length of the instruction to obtain device information is greater than the mtu value. If so, it performs packet splitting processing on the instruction to obtain device information according to the mtu value and sequentially calls the sending interface of the HarmonyOS to send each packet of data to the Bluetooth device. Otherwise, it directly sends the instruction to obtain device information to the Bluetooth device. If it is other, it reports an error;

[0127] Specifically, the sending interface is: gattDevice.writeCharacteristicValue(characteristic, ble.GattWriteType.WRITE_NO_RESPONSE), where the parameter ble.GattWriteType.WRITE_NO_RESPONSE is the sending mode of Bluetooth without response, and characteristic is an object containing the characteristic value, Bluetooth service, and data information to be sent;

[0128] Step 205: The SDK waits to receive the response data returned by the Bluetooth device, judges whether the waiting time exceeds the set time of the data reception timeout timer. If so, it reports an error. Otherwise, it executes step 206;

[0129] In this embodiment, the SDK registers the data status monitoring callback to the system through the registration interface of the HarmonyOS and receives the response data returned by the Bluetooth device through the data status monitoring callback. Specifically, the registration interface is: gattDevice.on(“BLECharacteristicChange”, bleCharacteristicChangeCallBack), where the parameter bleCharacteristicChangeCallBack is the data status monitoring callback;

[0130] After the Bluetooth device receives the complete device information acquisition instruction, it determines whether the device serial number in the device information acquisition instruction is the same as the serial number it stores itself. If they are the same, it returns its own device information to the SDK; if the length of the device information is greater than the mtu value, it performs packet splitting on the device information and returns each packet of data to the SDK in sequence.

[0131] Step 206: When the SDK receives the response data returned by the Bluetooth device through the data status monitoring callback, it caches the response data in sequence, verifies the integrity of the cached response data. If it is complete, it executes Step 207; if it is incomplete, it returns to Step 205.

[0132] Step 207: The SDK closes the data reception timeout timer, unlocks the child thread, obtains the response result from the response data in the child thread, and calls the switching interface with the main thread security function as a parameter to switch the currently running thread from the child thread back to the main thread.

[0133] Step 208: The SDK returns the response result to the APP.

[0134] In this embodiment, after the APP receives the response result, it prompts that the device information has been successfully obtained.

[0135] Optionally, in this embodiment, during the communication between the HarmonyOS device and the Bluetooth device, it further includes:

[0136] When the user manually disconnects the Bluetooth of the HarmonyOS device, the Bluetooth status monitoring callback is triggered. When the SDK receives the Bluetooth off state information returned by the Bluetooth status monitoring callback, it cancels the data status monitoring callback and the connection status monitoring callback, modifies the Bluetooth connection state to unconnected, closes the gatt object, and returns the Bluetooth device disconnection information to the APP.

[0137] In this embodiment, the data status monitoring callback is cancelled through the twelfth system interface of the HarmonyOS. Specifically, the twelfth system interface is: gattDevice.off(“BLECharacteristicChange”);

[0138] The connection status monitoring callback is cancelled through the thirteenth system interface of the HarmonyOS. Specifically, the thirteenth system interface is: gattDevice.off(“BLEConnectionStateChange”);

[0139] The gatt object is closed through the fourteenth system interface of the HarmonyOS. Specifically, the fourteenth system interface is: gattDevice.close();

[0140] After the APP receives the disconnection information of the Bluetooth device, it reminds the user to reopen the Bluetooth of the device and enter the serial number of the Bluetooth device to be connected.

[0141] When the user manually reopens the Bluetooth of the HarmonyOS device, the Bluetooth status monitoring callback is triggered again. When the SDK receives the Bluetooth on status information returned by the Bluetooth status monitoring callback, it obtains the corresponding cached device object data according to the serial number re-entered by the user, obtains the MAC address of the Bluetooth device from the device object data, creates a gatt object according to the MAC address of the Bluetooth device, sets the connection status monitoring callback according to the gatt object, calls the connection interface of the HarmonyOS according to the gatt object to start the connection operation with the Bluetooth device, obtains the Bluetooth connection status through the connection status monitoring callback. If the Bluetooth connection status is successful, it saves the Bluetooth connection status, obtains the Bluetooth service and characteristic values from the HarmonyOS, sets the reception of Bluetooth characteristic value notifications, sets the mtu value, and returns a connection success message to the APP. Otherwise, it returns a connection failure message to the APP.

[0142] In the method of this embodiment, by adding exception handling for the user to manually disconnect the Bluetooth, system problems can be avoided, and the stability of connection and communication can be improved; by creating a child thread to implement data communication between the HarmonyOS device and the Bluetooth device, supporting the HarmonyOS device to work in multiple threads, and achieving the effect of communication synchronization processing, the correct order of business operations can be guaranteed; and the code of the existing business operations (i.e., generating instructions for obtaining device information and obtaining response results) in the SDK for implementing communication with the Bluetooth device in Android system devices and iOS system devices can be reused, reducing development and maintenance costs.

[0143] Embodiment Four

[0144] Embodiment Four of the present invention provides a device for a HarmonyOS device to communicate with a Bluetooth device. The HarmonyOS device is installed with an APP. The device of this embodiment is pre-installed in the APP. The device includes:

[0145] A save and create module, configured to save the service parameters passed in by the communication interface and create a main thread safe function when the communication interface is called by the APP;

[0146] An enable and generate module, configured to start a child thread and generate Bluetooth communication instructions in the child thread according to the service parameters saved by the save and create module;

[0147] A switch and lock module, configured to call the switch interface with the main thread safe function as a parameter to switch the currently running thread from the child thread back to the main thread and perform a locking process on the child thread itself;

[0148] A send module, configured to call the send interface of the HarmonyOS to send the Bluetooth communication instructions to the Bluetooth device;

[0149] Optionally, the sending module in this embodiment is specifically configured to determine whether the length of the Bluetooth communication instruction is greater than the mtu value. If so, the Bluetooth communication instruction is packetized according to the mtu value, and the sending interface of the HarmonyOS is called in sequence to send each packet of data to the Bluetooth device. Otherwise, the sending interface of the HarmonyOS is called to send the Bluetooth communication instruction to the Bluetooth device;

[0150] Optionally, the sending module in this embodiment is specifically configured to check the saved Bluetooth connection status. If the Bluetooth connection status is successful, a data status monitoring callback is set, and the sending interface of the HarmonyOS is called to send the Bluetooth communication instruction to the Bluetooth device. If the Bluetooth connection status is otherwise, an error is reported;

[0151] The first judgment module is configured to wait for the response data returned by the Bluetooth device and determine whether the waiting time times out. If so, an error is reported. Otherwise, the receiving buffer module is triggered;

[0152] Among them, the first judgment module in this embodiment is specifically configured to start a data reception timeout timer, wait for the response data returned by the Bluetooth device, and determine whether the waiting time exceeds the set time of the data reception timeout timer. If so, an error is reported. Otherwise, the receiving buffer module is triggered;

[0153] The receiving buffer module is configured to receive the response data returned by the Bluetooth device and cache it;

[0154] In this embodiment, the receiving buffer module is further configured to turn off the data reception timeout timer;

[0155] Optionally, the receiving buffer module in this embodiment is specifically configured to receive the response data returned by the Bluetooth device and cache it in sequence, verify the integrity of the cached response data. If it is complete, the data reception timeout timer is turned off, and the unlocking and switching module is triggered. If it is incomplete, continue to wait for the response data returned by the Bluetooth device;

[0156] Optionally, the receiving buffer module in this embodiment is specifically configured to receive the response data returned by the Bluetooth device through the data status monitoring callback and cache it in sequence;

[0157] The unlocking and switching module is configured to unlock the child thread, obtain the response result from the response data in the child thread, and call the switching interface with the main thread security function as a parameter to switch the currently running thread from the child thread to the main thread;

[0158] The return module is configured to return the response result to the APP.

[0159] The device in this embodiment further includes:

[0160] The setting module is configured to set a Bluetooth status monitoring callback when the HarmonyOS device successfully establishes a Bluetooth connection with the Bluetooth device;

[0161] The cancellation and shutdown module is used to cancel the data status monitoring callback and the connection status monitoring callback when receiving the Bluetooth shutdown status information returned by the Bluetooth status monitoring callback, modify the Bluetooth connection status to unconnected, close the gatt object, and return the device disconnection Bluetooth connection information to the APP;

[0162] The reconnection module is used to, when receiving the Bluetooth turned-on status information returned by the Bluetooth status monitoring callback, obtain the corresponding cached device object data according to the serial number re-entered by the user, obtain the MAC address of the Bluetooth device from the device object data, create a gatt object according to the MAC address of the Bluetooth device, set the connection status monitoring callback according to the gatt object, call the connection interface of the HarmonyOS to start the connection operation with the Bluetooth device according to the gatt object, obtain the Bluetooth connection status through the connection status monitoring callback, save the Bluetooth connection status if the Bluetooth connection status is successful, obtain the Bluetooth service and characteristic values from the HarmonyOS, set to receive Bluetooth characteristic value notifications, set the mtu value, set the Bluetooth status monitoring callback, and return the connection success information to the APP, otherwise return the connection failure information to the APP.

[0163] The device in this embodiment further includes:

[0164] The acquisition and setting module is used to obtain the serial number input by the user and set the screen of the HarmonyOS device to be always on when the Bluetooth connection interface in the device is called by the APP;

[0165] The second judgment module is used to judge whether the HarmonyOS device has Bluetooth function and whether the APP has Bluetooth permission. If so, execute step A3, otherwise return an error message to the APP;

[0166] The first setting module is used to set the Bluetooth scan parameter configuration and the Bluetooth scan filtering policy, and set the Bluetooth scan result monitoring callback;

[0167] In this embodiment, the first setting module is specifically used to configure the high-power consumption mode or the balanced mode in the scan parameter mode, set the scan result reporting time interval to 0, set the Bluetooth scan filtering policy, and set the Bluetooth scan result monitoring callback;

[0168] The scan and acquisition module is used to perform Bluetooth scanning and obtain the scanned Bluetooth device broadcast data through the Bluetooth scan result monitoring callback;

[0169] Optionally, in this embodiment, the scan and acquisition module is specifically used to create a first timer, start Bluetooth scanning, judge whether the scanning times out according to the first timer. If so, return the connection failure information to the APP, otherwise obtain the scanned Bluetooth device broadcast data through the Bluetooth scan result monitoring callback;

[0170] A parsing and obtaining module, which is used to parse the broadcast data of the scanned Bluetooth device and find the MAC address of the Bluetooth device from the parsing result according to the obtained serial number;

[0171] A cancellation composition module, which is used to stop Bluetooth scanning, cancel the Bluetooth scanning result listening callback, form device object data with the MAC address and serial number of the Bluetooth device, and cache it;

[0172] Correspondingly, the cancellation composition module in this embodiment is also used to close the first timer;

[0173] A connection creation module, which is used to create a gatt object according to the MAC address of the Bluetooth device, set a connection status listening callback according to the gatt object, call the connection interface of the HarmonyOS to start the connection operation with the Bluetooth device according to the gatt object, obtain the Bluetooth connection status through the connection status listening callback, save the Bluetooth connection status if the Bluetooth connection status is successful, trigger the setting and return module, otherwise return a connection failure message to the APP;

[0174] A setting and return module, which is used to obtain Bluetooth services and characteristic values from the HarmonyOS, set the reception of Bluetooth characteristic value notifications, set the mtu value, and return a connection success message to the APP.

[0175] The device in this embodiment further includes:

[0176] A third judgment module, which is used to judge whether the HarmonyOS device has connected to the Bluetooth device when the second judgment module judges yes. If yes, it triggers the fourth judgment module, otherwise it triggers the first setting module;

[0177] Specifically, the third judgment module is used to judge whether there are Bluetooth connection objects and gatt objects in the HarmonyOS device when the second judgment module judges yes. If yes, it triggers the fourth judgment module, otherwise it triggers the first setting module;

[0178] A fourth judgment module, which is used to judge whether the serial number of the already connected Bluetooth device is consistent with the obtained serial number. If it is consistent, it returns information about the already connected Bluetooth device to the APP. If it is inconsistent, it disconnects the already connected Bluetooth device and triggers the first setting module;

[0179] Optionally, the device in this embodiment further includes: a query module, which is used to query whether there is device object data in the device cache list according to the obtained serial number when the fourth judgment module judges inconsistent. If yes, it obtains the MAC address of the Bluetooth device from the device object data, triggers the connection creation module, otherwise it triggers the first setting module.

[0180] Optionally, an embodiment of the present application further provides an electronic device, which includes at least one processor, a memory, and instructions stored on the memory and executable by the at least one processor. The at least one processor executes the instructions to implement the method for a HarmonyOS device to communicate with a Bluetooth device in the above embodiments.

[0181] An embodiment of the present invention provides a computer program product, including a computer program / instructions, wherein when the computer program / instructions are executed by a processor, the method for a HarmonyOS device to communicate with a Bluetooth device in the above embodiments is implemented.

[0182] In the above embodiments, it can be implemented in whole or in part by software, hardware, firmware, or any combination thereof. When implemented using a software program, it can be implemented in whole or in part in the form of a computer program product. The computer program product includes one or more computer programs. When the computer program is loaded and executed on an electronic device, the processes or functions described in the embodiments of the present application are generated in whole or in part. The computer program can be stored in a computer-readable storage medium, or transmitted from one computer-readable storage medium to another computer-readable storage medium. For example, the computer instructions can be transmitted from a base station, an electronic device, a server, or a data center to another base station, an electronic device, a server, or a data center in a wired manner (such as coaxial cable, optical fiber, digital subscriber line (DSL)) or a wireless manner (such as infrared, wireless, microwave, etc.). The computer-readable storage medium can be any available medium that can be accessed by the electronic device or a data storage device such as a server or a data center that includes one or more integrated media. The available medium can be a magnetic medium (such as a floppy disk, a hard disk, a magnetic tape), an optical medium (such as a DVD), or a semiconductor medium (such as a solid state disk (SSD)). In the embodiments of the present application, the electronic device can include the device described above.

[0183] Although the present application has been described in conjunction with various embodiments herein, however, in the process of implementing the claimed present application, those skilled in the art can understand and achieve other variations of the disclosed embodiments by viewing the accompanying drawings, the disclosure content, and the appended claims. In the claims, the word "comprising" does not exclude other components or steps, and "a" or "one" does not exclude a plurality. A single processor or other unit can implement several functions recited in the claims. Certain measures are recited in mutually different dependent claims, but this does not mean that these measures cannot be combined to produce good results.

[0184] Although the present application has been described in connection with specific features and their embodiments, it will be apparent that various modifications and combinations can be made without departing from the spirit and scope of the present application. Accordingly, the present specification and the drawings are merely exemplary illustrations of the present application as defined by the appended claims, and are considered to cover any and all modifications, variations, combinations, or equivalents within the scope of the present application. Obviously, those skilled in the art can make various changes and modifications to the present application without departing from the spirit and scope of the present application. Thus, if these modifications and variations of the present application fall within the scope of the claims of the present application and their equivalent technologies, the present application also intends to include these changes and modifications.

Claims

1. A method for a HarmonyOS device to communicate with a Bluetooth device, characterized in that, The HarmonyOS device is installed with an APP, and an SDK is pre - installed in the APP. The method includes: Step S1: When the communication interface of the SDK is called by the APP, the SDK saves the service parameters passed in by the communication interface and creates a main - thread - safe function; Step S2: The SDK starts a child thread and generates a Bluetooth communication instruction in the child thread according to the saved service parameters; Step S3: The SDK uses the main - thread - safe function as a parameter to call the switching interface of the HarmonyOS to switch the currently running thread from the child thread back to the main thread and locks the child thread; Step S4: The SDK calls the sending interface of the HarmonyOS to send the Bluetooth communication instruction to the Bluetooth device; Step S5: The SDK waits to receive the response data returned by the Bluetooth device and determines whether the waiting time times out. If so, it reports an error; otherwise, it executes Step S6; Step S6: The SDK receives the response data returned by the Bluetooth device and caches it; Step S7: The SDK unlocks the child thread, obtains the response result from the response data in the child thread, and uses the main - thread - safe function as a parameter to call the switching interface to switch the currently running thread from the child thread back to the main thread; Step S8: The SDK returns the response result to the APP.

2. The method according to claim 1, wherein Step S4 includes: The SDK checks the Bluetooth connection status. If the Bluetooth connection status is successful, it sets a data - status monitoring callback and calls the sending interface of the HarmonyOS to send the Bluetooth communication instruction to the Bluetooth device. If the Bluetooth connection status is other, it reports an error; Step S6 includes: The SDK receives the response data returned by the Bluetooth device through the data - status monitoring callback and caches it in sequence.

3. The method according to claim 2, characterized in that, Before Step S1, it further includes: When the HarmonyOS device successfully establishes a Bluetooth connection with the Bluetooth device, it sets a Bluetooth - status monitoring callback; The method further includes: When the SDK receives the Bluetooth - off - state information returned by the Bluetooth - status monitoring callback, it cancels the data - status monitoring callback and the connection - status monitoring callback, modifies the Bluetooth connection status to unconnected, closes the gatt object, and returns device - disconnected - Bluetooth - connection information to the APP; When the SDK receives the Bluetooth on-state information returned by the Bluetooth state monitoring callback, it obtains the corresponding cached device object data according to the serial number re-entered by the user, obtains the MAC address of the Bluetooth device from the device object data, creates the gatt object according to the MAC address of the Bluetooth device, sets the connection state monitoring callback according to the gatt object, calls the connection interface of the HarmonyOS according to the gatt object to start the connection operation with the Bluetooth device, obtains the Bluetooth connection state through the connection state monitoring callback, saves the Bluetooth connection state if the Bluetooth connection state is successful, obtains the Bluetooth service and characteristic values from the HarmonyOS, sets the reception of Bluetooth characteristic value notifications, sets the mtu value, and returns a connection success message to the APP; otherwise, returns a connection failure message to the APP.

4. The method according to claim 1, characterized in that The step S4 includes: the SDK determines whether the length of the Bluetooth communication instruction is greater than the mtu value. If so, it performs packet splitting on the Bluetooth communication instruction according to the mtu value and sequentially calls the sending interface of the HarmonyOS to send each packet of data to the Bluetooth device; otherwise, it calls the sending interface of the HarmonyOS to send the Bluetooth communication instruction to the Bluetooth device. The step S6 includes: the SDK receives the response data returned by the Bluetooth device and caches it sequentially, checks the integrity of the cached response data. If it is complete, it executes step S7; if it is incomplete, it continues to wait for the response data returned by the Bluetooth device.

5. The method according to claim 1, characterized in that Before the step S1, it further includes: Step A1: When the Bluetooth connection interface of the SDK is called by the APP, the SDK obtains the serial number input by the user and sets the screen of the HarmonyOS device to be always on. Step A2: The SDK determines whether the HarmonyOS device has Bluetooth functionality and whether the APP has Bluetooth permissions. If so, it executes step A3; otherwise, it returns an error message to the APP. Step A3: The SDK sets the Bluetooth scan parameter configuration and Bluetooth scan filtering policy, and sets the Bluetooth scan result monitoring callback. Step A4: The SDK starts Bluetooth scanning and obtains the broadcast data of the scanned Bluetooth device through the Bluetooth scan result monitoring callback. Step A5: The SDK parses the broadcast data of the scanned Bluetooth device and finds the MAC address of the Bluetooth device from the parsing result according to the obtained serial number. Step A6: The SDK stops Bluetooth scanning, cancels the Bluetooth scan result monitoring callback, and caches the device object data composed of the MAC address of the Bluetooth device and the serial number. Step A7: The SDK creates a gatt object according to the MAC address of the Bluetooth device, sets the connection state monitoring callback according to the gatt object, calls the connection interface of the HarmonyOS according to the gatt object to start the connection operation with the Bluetooth device, obtains the Bluetooth connection state through the connection state monitoring callback, saves the Bluetooth connection state if the Bluetooth connection state is successful, and executes step A8; otherwise, it returns a connection failure message to the APP. Step A8: The SDK obtains the Bluetooth service and characteristic values from the HarmonyOS, sets up to receive Bluetooth characteristic value notifications, sets the mtu value, and returns a connection success message to the APP.

6. The method according to claim 5, wherein Between step A2 and step A3, the following steps are also included: Step B1: The SDK determines whether the HarmonyOS device has connected a Bluetooth device. If so, it proceeds to step B2; otherwise, it proceeds to step A3. Step B2: The SDK determines whether the serial number of the already connected Bluetooth device is the same as the obtained serial number. If they are the same, it returns the information of the connected Bluetooth device to the APP. If they are different, it disconnects the connected Bluetooth device and proceeds to step A3.

7. The method according to claim 6, wherein Before executing step A3 in step B2, the following steps are also included: The SDK queries the device cache list based on the obtained serial number to check if there is device object data. If so, it obtains the MAC address of the Bluetooth device from the device object data and proceeds to step A7; otherwise, it proceeds to step A3.

8. The method according to claim 6, characterized in that Step B1 includes: The SDK determines whether there are Bluetooth connection objects and gatt objects in the HarmonyOS device. If so, it determines that the HarmonyOS device has connected a Bluetooth device and proceeds to step B2; otherwise, it determines that the HarmonyOS device has not connected a Bluetooth device and proceeds to step A3.

9. The method according to claim 5, wherein The SDK's configuration of Bluetooth scanning parameters includes: The SDK configures a high-power consumption mode or a balanced mode in the scanning parameter mode and sets the scanning result reporting time interval to 0.

10. A device for a HarmonyOS device to communicate with a Bluetooth device, characterized in that, The HarmonyOS device is installed with an APP, and the device is pre-positioned in the APP. The device includes: A save and creation module, which is used to save the service parameters passed in by the communication interface and create a main-thread-safe function when the communication interface is called by the APP. An opening and generation module, which is used to start a child thread and generate Bluetooth communication instructions in the child thread based on the service parameters saved by the save and creation module. A switching and locking module, which is used to call the switching interface with the main-thread-safe function as a parameter to switch the currently running thread from the child thread back to the main thread and perform a locking process on the child thread itself. A sending module, which is used to call the sending interface of the HarmonyOS to send the Bluetooth communication instructions to the Bluetooth device. A first judgment module, which is used to wait for the response data returned by the Bluetooth device and judge whether the waiting time has timed out. If so, it reports an error; otherwise, it triggers the receiving cache module. The receiving cache module is used to receive and cache the response data returned by the Bluetooth device. An unlocking and switching module, which is used to unlock the child thread, obtain the response result from the response data in the child thread, and call the switching interface with the main-thread-safe function as a parameter to switch the currently running thread from the child thread to the main thread. A return module, which is used to return the response result to the APP.

11. An electronic device, characterized in that, The electronic device includes at least one processor, a memory, and instructions stored on the memory and executable by the at least one processor. The at least one processor executes the instructions to implement the steps of the method according to any one of claims 1 to 9.

12. A computer-readable storage medium, characterized in that, The computer-readable storage medium includes a computer program, which, when running on an electronic device, causes the electronic device to perform the steps of the method according to any one of claims 1 to 9.

13. A computer program product comprising computer programs / instructions, characterized in that, When the computer program / instructions are executed by a processor, the steps of the method according to any one of claims 1 to 9 are implemented.

Citation Information

Patent Citations

  • Service pack installation method and device

    CN114253558A

  • Method for carrying out HDC debugging by utilizing Bluetooth communication based on swan monk system

    CN114691509A