"Go" bcsphere Getting Started tutorial 01:immediate alert--good

Source: Internet
Author: User

Original URL: http://www.ituring.com.cn/article/117570

Write in front

Smart hardware development starts with smart hardware, and in each chapter of this tutorial, you first list the service that your Bluetooth smart hardware needs to support, make sure that the hardware reads and writes the eigenvalues correctly, and confirms that the hardware is functioning properly. You can use Bcsphere-core-dev or lightblue to test the functionality of your hardware.

Minimum hardware support

Service: Immediate Alert
UUID: 00001802-0000-1000-8000-00805F9B34FB

characteristic: Immediate Alert
UUID: 00002A26-0000-1000-8000-00805F9B34FB
Property : Write | | Writewithoutresponse
Function: Write hex value 0, the device stops making sound, write hex value 1, the device makes a sound;

By studying in this chapter, you can:

    • Learn how to use Bc.js to connect a device
    • Learn how to use the Bc.js interface to manipulate the GATT for data writing
    • Manipulate a feature value with a button that is displayed with HTML

Immediate Alert is the simplest intelligent hardware service defined by Bluetooth4.0 and uses only one characteristic. Hardware If this service is supported, then a value is written to the characteristic contained therein, so the device should be able to make a corresponding noise.

Device Object

The device object is the base object for all GATT operations and is used to connect, find service, and so on. Device objects include many properties, such as equipment broadcast packets, equipment manufacturer information, and so on, can refer to here.
To create a device object, you can use the following statement:

var device = new BC.Device({deviceAddress:"20:CD:39:AD:65:20",type:"BLE"});

When you create a new device, its deviceaddress and type must be passed in, and in Bcsphere, when a device is selected to enter your WebApp, it will bring in two ' environment variables ' respectively DEVICEADDRESS . DEVICETYPE Represents the selected device address and the corresponding device type, or you can create a new device that is selected on the Bcsphere Scan page:

var device = new BC.Device({deviceAddress:DEVICEADDRESS,type:DEVICETYPE});

The debugging environment for the example in the tutorial is Android, so you can get the MAC address of the device. iOS assigns a UUID to each device, and the UUID assigned to the same tracker on a different iOS device is not the same, so for iOS devices, the device object should be generated by the Bc.js scan interface, but in order to maintain the clarity of the tutorial, this chapter does not cover the introduction of the scanning interface.
Note: Of course, this is not to say that in the operation of the device must know its specific Bluetooth address and device type, through the bc.js provided by the scanning interface can also automatically generate an array of device objects, the use of scanning interface will be introduced in the third chapter.

After a new device object is created, we can use it to connect to the device, and any operation on the device must be made after the device is connected:

device.connect(function(){ alert("device is already connected well!"); },function(){ alert("connect error!");});

After the connection, two functions are passed as parameters, which are the callback functions that handle the success and error of the device connection respectively. The device object also has two events, which are connection establishment and disconnection events, which can be used to listen for these two events after the device object is established, and when the connection state of connected devices changes (such as when the device is too far away), The callback function that is registered on the corresponding event is invoked for processing.

Device.AddEventListener("Deviceconnected",function(Device){Alert("Device:" +Sdeviceaddress +  "is connected successfully !" device. ( "devicedisconnected" , (device alert  ( "device:"  + device< Span class= "pun". deviceaddress +  "is connected successfully !" });           
Service object:

Once connected to the device, the device's discoverservices interface can be used to find the services included in the appliance, and the structure in the specific service object is shown here:

Device.Connect(function(){Device.Discoverservices(function(){ VarService=Device.Getservicebyuuid "1802" 0]; Alert ( "the Service UUID is"  +  Service. },function () { alert< Span class= "pun" > ( "discoverservices error!"  }); },function () { alert< Span class= "pun" > ( "Connect error!" });              

It is important to note that if a service is a standard service, its UUID can be abbreviated to a 32-bit UUID. Because the service of the same UUID may appear on the device, the interface returns an array of objects with the UUID as a getServiceByUUID("1802") 00001802-0000-1000-8000-00805f9b34fb service, because the hardware in this chapter has only one service with a UUID of 1802, So we take one of the first service to operate getServiceByUUID("1802")[0] .
After the call to the Discoverservices interface succeeds, a field is populated in the device object, and you can iterate over the device.services array to get all the service in the device. (There is an array of characteristic in the same service, an array of descriptor in the characteristic, which is not described later).

Characteristic object

The eigenvalue object is the focus object of the operation, and the service includes an Discovercharacteristics interface that gets to the corresponding characteristic object and can call its method to characteristic ' write/ Read/subscribe ' operation, the specific interface can be see here.

    VarDevice= NewBc.Device({DeviceAddress:"20:cd:39:ad:65:20",Type:"BLE"});Device.Connect(function(){Device.Discoverservices(function(){ VarService=Device.Getservicebyuuid("1802")[0];Service.Discovercharacteristics(function(){ VarCharacter=Service.Getcharacteristicbyuuid("2a26")[0];Character.Write("Hex","01",function(Data){Alert(Json.Stringify(Data)); },function(){Alert("Write error!" });  },function () { alert< Span class= "pun" > ( "Discovercharacteristics error!"  });  },function () { alert< Span class= "pun" > ( "discoverservices error!"  });  },function () { alert< Span class= "pun" > ( "Connnect error!"  });             

Of course, if you want to get to the characteristic object and manipulate it faster, You can use the prepare interface provided by the device class in Bc.js, and after the successful invocation of this interface, all the service structures in the hardware device are populated into the device object, but for more heavily-serviced hardware, invoking the prepare interface can cause delays in the operation, as appropriate during development:

    VarDevice= NewBc.Device({DeviceAddress:"20:cd:39:ad:65:20",Type:"BLE"});Device.Connect(function(){Device.Prepare(function(){ VarService=Device.Getservicebyuuid("1802")[0]; VarCharacter=Service.Getcharacteristicbyuuid("2a26")[0];Character.Write("Hex","01",function(Data){Alert(Json.Stringify(data));  },function() { alert("Write error!" );  });  },function() { alert("Device Prepare error!" );  });  },function() { alert("Connnect error!" );  }); 
Summarize

At this point, you can already use Bcsphere to write the eigenvalues of a device that you want to manipulate. For specific examples of combining web pages, see the source code provided in this chapter. The next chapter will focus on the operation of the characteristic.

"Go" bcsphere Getting Started tutorial 01:immediate alert--good

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.