All Products
Search
Document Center

Bluetooth API overview

Last Updated: Feb 04, 2021

Version requirement

Bluetooth mPaaS version Android/iOS
Bluetooth Low Energy (BLE) mPaaS 10.1.60 or later versions
  • Android: 5.0 and later versions
  • iOS: No requirement
Traditional Bluetooth mPaaS 10.1.60 or later versions -

Process flow

BLE

BLE

Traditional Bluetooth

traditional

Bluetooth API

BLE

API Description
my.connectBLEDevice Connect to low energy bluetooth devices.
my.disconnectBLEDevice Disconnect to low energy bluetooth devices.
my.getBLEDeviceCharacteristics Get the characteristics of low energy Bluetooth devices.
my.getBLEDeviceServices Get all the low energy Bluetooth devices that are discovered, including the connected devices.
my.notifyBLECharacteristicValueChange Enable the function to notify changes to the characteristic value.
my.offBLECharacteristicValueChange Disable the function to notify changes to the characteristic value.
my.offBLEConnectionStateChanged Disable the listener for connection status change events.
my.onBLECharacteristicValueChange Enable the listener for characteristic value change events.
my.onBLEConnectionStateChanged Enable the listener for connection status change events, such as device lost and device disconnected.
my.readBLECharacteristicValue Read the characteristic value.
my.writeBLECharacteristicValue Write data to the characteristic value.

Traditional Bluetooth

API Description
my.closeBluetoothAdapter Close the Bluetooth module in the mini program.
my.getBluetoothAdapterState Get the Bluetooth adapter status in the mini program.
my.getBluetoothDevices Get all the low energy Bluetooth devices that are discovered, including the connected devices.
my.getConnectedBluetoothDevices Get the Bluetooth devices that are connected.
my.offBluetoothAdapterStateChange Remove the listener for connection status change events.
my.offBluetoothDeviceFound Remove the listener for new Bluetooth device discovery events.
my.onBluetoothDeviceFound Trigger the event when a new Bluetooth device is found.
my.onBluetoothAdapterStateChange Monitor the Bluetooth status change events.
my.openBluetoothAdapter Initialize the Bluetooth module in the mini program.
my.startBluetoothDevicesDiscovery Start discovering Bluetooth devices nearby.
my.stopBluetoothDevicesDiscovery Stop discovering Bluetooth devices nearby.

Sample code

 
  1. //Initialize
  2. my.openBluetoothAdapter({
  3. success: (res) => {
  4. console.log(res);
  5. }
  6. });
  7. //Register discovery event
  8. my.onBluetoothDeviceFound({
  9. success: (res) => {
  10. let device = res.devices[0];
  11. //Connect the discovered device
  12. my.connectBLEDevice({
  13. deviceId: deviceId,
  14. success: (res) => {
  15. console.log(res)
  16. },
  17. fail:(res) => {
  18. },
  19. complete: (res)=>{
  20. }
  21. });
  22. //Stop discovering
  23. my.stopBluetoothDevicesDiscovery({
  24. success: (res) => {
  25. console.log(res)
  26. },
  27. fail:(res) => {
  28. },
  29. complete: (res)=>{
  30. }
  31. });
  32. }
  33. });
  34. //Register connection event
  35. my.onBLEConnectionStateChanged({
  36. success: (res) => {
  37. console.log(res);
  38. if (res.connected) {
  39. //Start reading and writing notify and other operations
  40. my.notifyBLECharacteristicValueChange({
  41. deviceId: deviceId,
  42. serviceId: serviceId,
  43. characteristicId: characteristicId,
  44. success: (res) => {
  45. console.log(res)
  46. },
  47. fail:(res) => {
  48. },
  49. complete: (res)=>{
  50. }
  51. });
  52. }
  53. }
  54. });
  55. //Register the read or notify data reception events
  56. my.onBLECharacteristicValueChange({
  57. success: (res) => {
  58. console.log(res);
  59. }
  60. });
  61. //Start discovering
  62. my.startBluetoothDevicesDiscovery({
  63. services: ['fff0'],
  64. success: (res) => {
  65. console.log(res)
  66. },
  67. fail:(res) => {
  68. },
  69. complete: (res)=>{
  70. }
  71. });
  72. //Disconnect
  73. my.disconnectBLEDevice({
  74. deviceId: deviceId,
  75. success: (res) => {
  76. console.log(res)
  77. },
  78. fail:(res) => {
  79. },
  80. complete: (res)=>{
  81. }
  82. });
  83. // Unregister events
  84. my.offBluetoothDeviceFound();
  85. my.offBLEConnectionStateChanged();
  86. my.offBLECharacteristicValueChange();
  87. //Exit bluetooth module
  88. my.closeBluetoothAdapter({
  89. success: (res) => {
  90. },
  91. fail:(res) => {
  92. },
  93. complete: (res)=>{
  94. }
  95. });