--- fast application using Bluetooth

1, initialization Bluetooth module

      In order to use Bluetooth, you need to initialize the Bluetooth module. In addition to setting status monitoring, all Bluetooth interfaces are required to work properly after the initialization is complete;

      // initialize the Bluetooth module

      bluetooth.openAdapter({

           // whether to open the Bluetooth switch system, default false

           operateAdapter: true,

           success:function(){

                 console.log('success');

           },

           fail:function(data,code){

                console.log('handling fail,code = ${code}');

                if(code===10001) {

                    // Bluetooth is not turned on, the user is prompted to turn on Bluetooth

                }

           },

           complete: function(){

                console.log('complete');

           }

      })

2, search around the equipment

      You can search operation found around low-power Bluetooth devices. Before performing a scan, you need to register a callback device discovery and upon receiving search results

       // register before scanning device discovery callback

       bluetooth.ondevicefound = function(data){

            console.log('new device list has founded');

            data.devices.forEach(device=>{

                    // find the necessary equipment stops scanning

                    bluetooth.stopDevicesDiscovery();

                    _this.deviceId = device.deviceId;

                    console.log('handling finding new device: ${JSON.stringify(device)}');

                    console.log('handling advertisData = ${_this.ab2hex(device.advertisData)}');

                    for(let key in device.serviceData){

                          console.log('handling serviceData:uuid = ${key},serviceData=${_this.ab2hex(device.serviceData[key])}')

                    }

            })

       };

       // start scanning

        bluetooth.startDevicesDiscovery({

             // Specify device uuid, supports 16-bit, 32-bit, 128-bit uuid, do not fill all around the scanning device

             services:['1105'],

              // whether to allow duplicate device report, if you do not listen to broadcast packets of data, this recommendation is not set, the default value is false

              allowDuplicatesKey: false,

              // reporting interval, in milliseconds, i.e. immediately reported to 0, the default value is 0

              interval:1000,

              success:function(){

                    console.log('success');

              }

        })

      3, the connection

       Before operating the device connected to the device, the connecting operation can be performed directly by deviceId, the scanning operation is not necessary. Before connecting, you can register to set the connection state of the interface monitoring device disconnection state;

       // Register the device connection status monitor

       bluetooth.onbleconnectionstatechange = function(data){

              console.log('handling device state change:deviceId = ${data.deviceId},connected =${data.connected}');

              // update the device connection status

               _this.connected = data.connected;

              if(data.connected){

                   // After the target device is connected, access the list of services

                    _this.getServices();

              }else{

                   // do the relevant disconnected operation, such as reconnection or rescan

              }

       }

       // connected devices

        bluetooth.createBLEConnection({

              deviceId: _this.deviceId,

              success:function(){

               },

              fail:function(data,code){

              },

              complete:function(){}

       })

       4, access to equipment service information

        bluetooth.getBLEDeviceServices({

              deviceId: _this.deviceId,

              success: function(data){

                  data.services.forEach(service = >{ 

                         // get the services they need, according to the definition can uuid screening equipment

                        if(service.isPrimary){

                             _this.serviceId = service.uuid;

                             // Get a list of feature values

                             _this.getCharacteristices();

                        }

                  })

              },

             fail:function(data,code){},

             complete(){}

        })

Guess you like

Origin www.cnblogs.com/sunqq/p/11237179.html
Recommended