In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "the method of initializing the Bluetooth adapter by openBluetoothAdapter". In the daily operation, I believe that many people have doubts about the method of initializing the Bluetooth adapter by openBluetoothAdapter. The editor has consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "the method of initializing Bluetooth adapter by openBluetoothAdapter". Next, please follow the editor to study!
Bluetooth adapter interface
The basic library version 1.1.0 is supported, and the lower version needs to be compatible.
IOS Wechat client version 6.5.6 is supported, but not supported by Android client.
The wx.openBluetoothAdapter (OBJECT) base library version 1.1.0 is supported, and the lower version needs to be compatible.
Initialize the Bluetooth adapter
OBJECT parameter description:
The parameter type is required, indicating that successFunction is successful and returns successful initialization information. FailFunction No API call failed callback function completeFunction No API call ends callback function (both successful and failed calls will be executed)
Sample code:
Wx.openBluetoothAdapter ({success: function (res) {console.log (res)}}) Bug & Tip
Tip: due to system problems, Bluetooth debugging is only supported on the mac version of the development tools
Tip: the base library version 1.1.0 is supported, and the lower version needs to be compatible.
The wx.closeBluetoothAdapter (OBJECT) base library version 1.1.0 is supported, and the lower version needs to be compatible.
Turn off the Bluetooth module. Calling this method will break all established links and release system resources
OBJECT parameter description:
The parameter type is required, indicating that successFunction is successful. If the module information is successfully closed, failFunction No API call failed callback function completeFunction No API call ends callback function (both successful and failed calls will be executed)
Sample code:
Wx.closeBluetoothAdapter ({success: function (res) {console.log (res)}}) wx.getBluetoothAdapterState (OBJECT) base library version 1.1.0 is supported, and the lower version needs to be compatible.
Get the status of the native Bluetooth adapter
OBJECT parameter description:
Parameter type required indicates that successFunction returns the status of the native Bluetooth adapter if it is successful. FailFunction No API call failed callback function completeFunction No API call ended callback function (both successful and failed calls will be executed)
Success return parameters:
Parameter type indicates whether discoveringBoolean is searching for device availableBoolean Bluetooth adapter availability errMsgString success: ok, error: details sample code: wx.getBluetoothAdapterState ({success: function (res) {console.log (res)}) wx.onBluetoothAdapterStateChange (CALLBACK) basic library version 1.1.0 is supported, and compatibility processing is required for the lower version.
Listen for Bluetooth adapter status change event
CALLBACK parameter description:
Parameter type indicates whether the availableboolean Bluetooth adapter is available and whether the discoveringboolean Bluetooth adapter is in the search state
Sample code:
Wx.onBluetoothAdapterStateChange (function (res) {console.log (`adapterState changed, now is`, res)}) wx.startBluetoothDevicesDiscovery (OBJECT) base library version 1.1.0 is supported, and the lower version needs to be compatible.
Start searching for nearby Bluetooth peripherals. Note that this operation consumes system resources, please call the stop method to stop the search after searching and connecting to the device.
OBJECT parameter description:
Parameter type is required to indicate whether servicesArray No Bluetooth device Master service uuid list allowDuplicatesKeyboolean allows repeated reporting of the same device. If repeated escalation is allowed, the onDeviceFound method will report the same device many times, but the RSSI value will have the interval between different intervalinteger not reporting the device. The default is 0, which means to find a new device and report it immediately. Otherwise, if the successFunction is reported as successful according to the passed interval, the local Bluetooth adapter status failFunction No API call failed callback function completeFunction No API call end callback function (both successful and failed calls will be executed)
The services parameter states that some Bluetooth devices broadcast the uuid of their own primary service. If the array is passed in here, then only the devices with this primary service are searched according to the uuid list.
Success return parameters:
Parameter type description: errMsgstring successful: ok, error: details isDiscoveringboolean whether the Bluetooth adapter is currently in search state
Sample code:
/ / take the Bluetooth smart lamp of Wechat hardware platform as an example. The UUID of the main service is FEE7. Enter this parameter to search only devices whose main service UUID is FEE7 wx.startBluetoothDevicesDiscovery ({services: ['FEE7'], success: function (res) {console.log (res)}}) wx.stopBluetoothDevicesDiscovery (OBJECT) basic library version 1.1.0 is supported, and the lower version needs to be compatible.
Stop searching for nearby Bluetooth peripherals. Call this method to stop the search after making sure that the device you need to connect to is found.
OBJECT parameter description:
Parameter type required indicates that successFunction returns the status of the native Bluetooth adapter if it is successful. FailFunction No API call failed callback function completeFunction No API call ended callback function (both successful and failed calls will be executed)
Success return parameters:
Parameter type description errMsgstring success: ok, error: details
AdapterState
Bluetooth Adapter status Information
The parameter type indicates whether discoveringboolean is searching for the device availableboolean Bluetooth adapter is available
Sample code:
Wx.stopBluetoothDevicesDiscovery ({success: function (res) {console.log (res)}}) wx.getBluetoothDevices (OBJECT) base library version 1.1.0 is supported, and the lower version needs to be compatible.
Get all discovered Bluetooth devices, including those that are already connected to this computer
OBJECT parameter description:
Parameter type required indicates that successFunction returns the status of the native Bluetooth adapter if it is successful. FailFunction No API call failed callback function completeFunction No API call ended callback function (both successful and failed calls will be executed)
Success return parameters:
Parameter type indicates the list of connected devices corresponding to devicesArrayuuid: errMsgstring success: ok, error: details
Device object
Bluetooth device Information
The parameter type describes the name of the namestring Bluetooth device. Some devices may not have the signal strength of the idRSSIint current Bluetooth device that deviceIdstring uses to distinguish the device. AdvertisDataArrayBuffer the broadcast content of the current Bluetooth device (Note: vConsole cannot print ArrayBuffer type data)
Sample code:
Wx.getBluetoothDevices ({success: function (res) {console.log (res)}}) Bug & Tip
Tip: Mac system may not be able to get advertisData and RSSI. Please use real machine to debug.
Tip: the deviceId obtained from the developer tools and Android is the device MAC address, and the iOS is the device uuid. So deviceId cannot be hard-coded into the code
The wx.getConnectedBluetoothDevices (OBJECT) base library version 1.1.0 is supported, and the lower version needs to be compatible.
Get devices in connected state according to uuid
OBJECT parameter description:
The parameter type is required indicating that servicesArray is the uuid list of the main service of the Bluetooth device. If successFunction is successful, the local Bluetooth adapter status is returned. FailFunction No API call failed callback function completeFunction No API call ended callback function (both successful and failed calls will be executed)
Success return parameters:
Parameter type indicates that the list of devices searched by devicesArray succeeded: ok, error: details
Device object
Bluetooth device Information
The parameter type describes the name of the namestring Bluetooth device. Some devices may not have the id that deviceIdstring uses to distinguish the device.
Sample code:
Wx.getConnectedBluetoothDevices ({success: function (res) {console.log (res)}}) Bug & Tip
Tip: the deviceId obtained from the developer tools and Android is the device MAC address, and the iOS is the device uuid. So deviceId cannot be hard-coded into the code
The wx.onBluetoothDeviceFound (CALLBACK) base library version 1.1.0 is supported, and the lower version needs to be compatible.
Listen for events that find new devices
CALLBACK parameter description:
Parameter type describes the list of devices newly searched by devicesArray
Device object
Parameter type description deviceIdstring Bluetooth device id, refer to device object namestring Bluetooth device name, reference device object RSSIint current Bluetooth device signal strength advertisDataArrayBuffer current Bluetooth device broadcast content (Note: vConsole cannot print ArrayBuffer type data)
Sample code:
Wx.onBluetoothDeviceFound (function (devices) {console.log ('new device list has founded') console.dir (devices)}) Bug & Tip
Tip: Mac system may not be able to get advertisData and RSSI. Please use real machine to debug.
Tip: the deviceId obtained from the developer tools and Android is the device MAC address, and the iOS is the device uuid. So deviceId cannot be hard-coded into the code
Low-power Bluetooth interface wx.createBLEConnection (OBJECT) base library version 1.1.0 is supported, and the lower version needs to be compatible.
Connect low-power Bluetooth devices
OBJECT parameter description:
Parameter type is required to indicate that deviceIdstring is a Bluetooth device id. If the getDevices API successFunction is successful, return the local Bluetooth adapter status failFunction No API call failed callback function completeFunction No API call end callback function (both successful and failed calls will be executed)
Success return parameters:
Parameter type description errMsgstring success: ok, error: details
Sample code:
Wx.createBLEConnection ({/ / the deviceId here needs to obtain deviceId from the above getBluetoothDevices or onBluetoothDeviceFound API: deviceId, success: function (res) {console.log (res)}}) wx.closeBLEConnection (OBJECT) is supported from version 1.1.0, and the lower version needs to be compatible.
Disconnect from a low-power Bluetooth device
OBJECT parameter description:
Parameter type is required to indicate that deviceIdstring is a Bluetooth device id. If the getDevices API successFunction is successful, return the local Bluetooth adapter status failFunction No API call failed callback function completeFunction No API call end callback function (both successful and failed calls will be executed)
Success return parameters:
Parameter type description errMsgstring success: ok, error: details
Sample code:
Wx.closeBLEConnection ({success: function (res) {console.log (res)}}) wx.getBLEDeviceServices (OBJECT) base library version 1.1.0 is supported, and the lower version needs to be compatible.
Get all the service (services) of Bluetooth devices
OBJECT parameter description:
Parameter type is required to indicate that deviceIdstring is a Bluetooth device id. If the getDevices API successFunction is successful, return the local Bluetooth adapter status failFunction No API call failed callback function completeFunction No API call end callback function (both successful and failed calls will be executed)
Success return parameters:
Parameter type description servicesarray device service list errMsgstring success: ok, error: details
Service object
Bluetooth device service (Service) Information
Parameter type indicates whether the uuidisPrimaryboolean of the uuidstring Bluetooth device service is the primary service.
Sample code:
Wx.getBLEDeviceServices ({/ / the deviceId here needs to obtain deviceId from the above getBluetoothDevices or onBluetoothDeviceFound API: deviceId, success: function (res) {console.log ('device services:', res.services)}}) wx.getBLEDeviceCharacteristics (OBJECT) is supported from version 1.1.0, and the lower version needs to be compatible.
Get all characteristic (eigenvalues) of Bluetooth device
OBJECT parameter description:
Parameter type is required to indicate that deviceIdstring is the Bluetooth device id. If the reference device object serviceIdstring is Bluetooth service uuidsuccessFunction, the local Bluetooth adapter status is returned if the uuidsuccessFunction is successful. If the API calls the failed callback function completeFunction, no callback function will be called (both successful and failed calls will be executed)
Success return parameters:
Parameter type description characteristicsarray device eigenvalue list errMsgstring success: ok, error: details
Characteristic object
Bluetooth device characteristic (eigenvalue) information
The parameter type describes the uuidpropertiesobject of the eigenvalue of the uuidstring Bluetooth device the type of operation supported by the eigenvalue
Properties object
Parameter type indicates whether readboolean the eigenvalue supports read operation writeboolean whether the eigenvalue supports write operation notifyboolean whether the eigenvalue supports notify operation indicateboolean whether the eigenvalue supports indicate operation
Sample code:
Wx.getBLEDeviceCharacteristics ({/ / the deviceId here needs to obtain deviceId: deviceId from the above getBluetoothDevices or onBluetoothDeviceFound API, / / the serviceId here needs to obtain serviceId: serviceId from the above getBLEDeviceServices interface, success: function (res) {console.log ('device getBLEDeviceCharacteristics:', res.characteristics)}}) wx.readBLECharacteristicValue (OBJECT) is supported from version 1.1.0, and the lower version needs to be compatible.
Read the binary data value of the eigenvalue of the low-power Bluetooth device. Note: the eigenvalue of the device must support read before it can be successfully called. For more information, please see the properties attribute of characteristic.
OBJECT parameter description:
Parameter type is required to indicate that deviceIdstring is the id of Bluetooth device. Reference device object serviceIdstring is Bluetooth eigenvalue corresponding to the service uuidcharacteristicIdstring is Bluetooth eigenvalue. If uuidsuccessFunction is successful, return local Bluetooth adapter status failFunction No API call failed callback function completeFunction No API call end callback function (both successful and failed calls will be executed)
Success return parameters:
Parameter type description characteristicobject device eigenvalue information errMsgstring success: ok, error: details
Characteristic object
Bluetooth device characteristic (eigenvalue) information
The parameter type indicates that the uuidserviceIdobject Bluetooth device characteristic value of the characteristicIdstring Bluetooth device eigenvalue corresponds to the binary value corresponding to the uuidvalueArrayBuffer Bluetooth device eigenvalue of the service (Note: vConsole cannot print ArrayBuffer type data)
Sample code:
/ / the callback here is required to obtain wx.onBLECharacteristicValueChange (function (characteristic) {console.log ('characteristic value comed:', characteristic)}) wx.readBLECharacteristicValue ({/ / the deviceId here needs to obtain deviceId: deviceId from the above getBluetoothDevices or onBluetoothDeviceFound API, / / the serviceId here needs to obtain serviceId: serviceId from the above getBLEDeviceServices interface, / / the characteristicId here needs to obtain characteristicId: characteristicId from the above getBLEDeviceCharacteristics API) Success: function (res) {console.log ('readBLECharacteristicValue:', res.characteristic.value)}}) Bug & Tip
Tip: there is the possibility of read-write failure when calling the read-write interface multiple times in parallel.
The information read by the tip:read interface needs to be obtained in the callback registered by the onBLECharacteristicValueChange method.
The wx.writeBLECharacteristicValue (OBJECT) base library version 1.1.0 is supported, and the lower version needs to be compatible.
Write binary data to the eigenvalues of the low-power Bluetooth device. Note: the eigenvalue of the device must support write before it can be successfully called. For more information, please see the properties attribute of characteristic.
Tips: there is the possibility of read-write failure when calling the read-write interface multiple times in parallel.
OBJECT parameter description:
Parameter type required indicates that deviceIdstring is a Bluetooth device id, reference device object serviceIdstring is Bluetooth eigenvalue corresponding to service uuidcharacteristicIdstring is bluetooth eigenvalue, uuidvalueArrayBuffer is bluetooth eigenvalue corresponding to bluetooth device eigenvalue (note: vConsole cannot print ArrayBuffer type data) if successFunction is successful, return the status of the native Bluetooth adapter failFunction No API call failed callback function completeFunction No API call end callback function (successful or failed will be executed)
Success return parameters:
Parameter type description errMsgstring success: ok, error: details
Sample code:
/ / the callback here can obtain the eigenvalue change caused by write wx.onBLECharacteristicValueChange (function (characteristic) {console.log ('characteristic value changed:', characteristic)}) / / send a 0x00 hexadecimal data let buffer = new ArrayBuffer (1) let dataView = new DataView (buffer) dataView.setUint8 (0,0) wx.writeBLECharacteristicValue ({/ / the deviceId here needs to obtain deviceId: deviceId in the above getBluetoothDevices or onBluetoothDeviceFound interface / / the serviceId here needs to obtain serviceId: serviceId from the above getBLEDeviceServices API, / / the characteristicId here needs to obtain characteristicId: characteristicId from the above getBLEDeviceCharacteristics API, / / the value here is the ArrayBuffer type value: buffer, success: function (res) {console.log ('writeBLECharacteristicValue success', res.errMsg)}}) wx.notifyBLECharacteristicValueChanged (OBJECT) basic library version 1.1.0 is supported Low version needs to be compatible.
Enable the notify function when the eigenvalues of low-power Bluetooth devices change. Note: the eigenvalue of the device must support notify before it can be successfully called. For more information, please see the properties attribute of characteristic.
In addition, notify must be enabled before you can listen to device characteristicValueChange events
OBJECT parameter description:
Parameter type required indicates that deviceIdstring is a Bluetooth device id, reference device object serviceIdstring is Bluetooth eigenvalue, corresponding service uuidcharacteristicIdstring is Bluetooth eigenvalue, uuidstateboolean is true: enable notify; false: deactivate notifysuccessFunction is successful, return local Bluetooth adapter status failFunction No API call failed callback function completeFunction No API call end callback function (both successful and failed call will be executed)
Success return parameters:
Parameter type description errMsgstring success: ok, error: details
Sample code:
Wx.notifyBLECharacteristicValueChanged ({state: true, / / enable notify feature / / the deviceId here needs to obtain deviceId: deviceId from the above getBluetoothDevices or onBluetoothDeviceFound interface, / / the serviceId here needs to obtain serviceId: serviceId from the above getBLEDeviceServices interface, / / the characteristicId here needs to obtain characteristicId: characteristicId, success: function (res) {console.log ('notifyBLECharacteristicValueChanged success') from the above getBLEDeviceCharacteristics interface Res.errMsg)}}) wx.onBLEConnectionStateChanged (CALLBACK) Base Library version 1.1.0 is supported Low version needs to be compatible.
Listen for low-power Bluetooth connection error events, including device loss, abnormal connection disconnection, and so on.
CALLBACK parameter description:
Parameter type describes the deviceIdstring Bluetooth device id. Refer to the current status of the device object connectedboolean connection.
Sample code:
Wx.onBLEConnectionStateChanged (function (res) {/ / this method callback can be used to handle exceptions such as unexpected disconnection console.log (`device ${res.deviceId} state has changed, connected: ${res.connected} `)) wx.onBLECharacteristicValueChange (CALLBACK) basic library version 1.1.0 is supported, and compatibility is required in the lower version.
Listen for eigenvalue changes of low-power Bluetooth devices. The notify interface must be enabled before you can receive the notification pushed by the device.
CALLBACK parameter description:
Parameter type description: deviceIdstring Bluetooth device id. Refer to device object serviceIdstring eigenvalue the service uuidcharacteristicIdstring eigenvalue the latest eigenvalue of uuidvalueArrayBuffer eigenvalue (Note: vConsole cannot print ArrayBuffer type data)
Sample code:
Wx.onBLECharacteristicValueChange (function (res) {console.log (`characteristic ${res.characteristicId} has changed) Now is ${res.value} `) Bluetooth error code (errCode) list error code description Note 0ok normal 10000not init uninitialized Bluetooth Adapter 10001not available current Bluetooth Adapter unavailable 10002no device did not find specified device 10003connection fail connection failed 10004no service did not find specified service 10005no characteristic did not find specified eigenvalue 10006no connection current connection disconnected 10007property not support current eigenvalue does not support this operation Exceptions reported by the system are unique to 10009system not supportAndroid systems. The system version is lower than 4.3. does not support BLE10010no descriptor did not find the specified descriptor. This is the end of the study on "how to initialize the Bluetooth adapter by openBluetoothAdapter". I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.