A ble and BT differences
In fact, I know many programmers do not like to read in addition to the code of the document, because sometimes these too lengthy documents for programming and no more benefits, with the Protocol, interface, demo almost a lot of people can write very good quality code. But in fact, deeper into the programming is less reading, reading documents can solve many of the difficulties encountered in programming, such as large data traffic situation, a lot of normal programs will show unreliable side, this is not enough programming can be solved, hardware configuration, server bandwidth, the use of the database, The interface that is called can be a bottleneck. such as BLE, the same Bluetooth, but there are essential differences, a performance is not all Bluetooth devices are supported BLE, programming if not consider this problem, the program error will be confused. Another example is connected to the BLE or Bluetooth devices are limited, if you do not know this limit, when the connection is not on, will also restless.
Ble in the smart home and mobile device applications in the ascendant, do in-depth research to extend to more applications, or meaningful.
1 History of Bluetooth: The founding company of Bluetooth is Ericsson. 1994 Ericsson began research and development of small-scale wireless communication technology, in 1997, Ericsson's research stimulated the interest of other companies, so in February 1998, some multinational companies including Nokia, Apple, Samsung, a special interest group (SIG), Their common goal is to create a global, small-scale wireless communications technology that is Bluetooth.
2.BLE is the abbreviation of Bluetooth low energy, also known as Bluetooth 4.0, differs from Bluetooth 3.0 and previous technology. BLE, formerly known as the Wibree technology developed by Nokia, is mainly used for the continuous connection between mobile intelligent terminals and peripheral accessories, is a very low-power short-range wireless communication technology, and the effective transmission distance is raised to more than 100 meters, and only need a button battery can work for several years.
3. BLE is developed on the basis of Bluetooth technology, both with Bluetooth and from traditional Bluetooth. BLE device sub-mode and two-mode, dual-mode abbreviation BR, the trademark is Bluetooth smart ready, single-mode short-ble or LE, the trademark is Bluetooth Smart. Android does not support ble after 4.3, which can explain that not all Bluetooth phones support ble, and Bluetooth phones that support ble are generally dual-mode.
4. Dual-mode compatible with traditional Bluetooth, can communicate with traditional Bluetooth, can also communicate with BLE, commonly used on mobile phones, android4.3 and IOS4.0 after the release of support BR, that is, dual-mode equipment. Single mode can only communicate with the BR and Singlemode devices, and can not communicate with traditional Bluetooth, due to low power consumption, to the captain, so commonly used in the smart device of the bracelet. This can explain the difference between ble on a mobile phone and a device such as a hand ring.
5 Not all phones support ble, because BLE does not rely solely on software implementation, but also requires hardware support, so there are many mobile phones can not join the smart bracelet and other devices. Android4.3 mobile phones are equipped with dual-mode BR, so compatible with Bluetooth 3.0 technology before, can not only communicate with the BLE device, but also with the traditional Bluetooth communication, compared to power consumption, can be as high-speed transmission as traditional equipment. Most smart bracelet uses a single-work ble, does not support traditional Bluetooth, can not connect and communicate with it, low-power lower-rate devices.
two. Use of BLE (Low power Bluetooth)
Application features: Packet transfer, mobile phone expansion, low power consumption
☆2.4G Bluetooth Low power system
☆ Consumer Electronic Products
☆ Displaced Phone Peripheral expansion device
☆ Transport and Leisure equipment
☆ Health Medical Supplies (Sphygmomanometer, thermometer ...)
☆ Automotive Electronic Equipment
☆ Human-Machine Interface Device (keyboard, mouse, remote control ...)
☆usb Dongle
three How Bluetooth works
1 the master-slave relationship of Bluetooth communication
Bluetooth technology requires that each pair of devices for Bluetooth communication, must be a primary role, and the other is from the role, in order to communicate, communication, must be by the main side to find, initiate pairing, after the success of the chain, the two sides can send and receive data. Theoretically, a Bluetooth master device can communicate with 7 Bluetooth slave devices at the same time. A Bluetooth communication device, can switch between two characters, usually work in the slave mode, waiting for other main devices to connect, when needed, switch to the main mode, to other devices to initiate the call. When a Bluetooth device initiates a call in the main mode, it needs to know the other person's Bluetooth address, pairing password and other information, and after pairing is complete, the call can be initiated directly. This can explain why Bluetooth is sometimes not available and there may be too many Bluetooth devices connected.
2 The call process for Bluetooth
The Bluetooth master device initiates the call, starting with a lookup to find the Bluetooth device that is located around the found. After the primary device locates the slave Bluetooth device, it is paired with the slave Bluetooth device, and the pin code of the slave device is required, and the device does not need to enter the PIN code. When pairing is complete, the slave Bluetooth device records the trust information for the primary device, where the master can initiate a call to the slave device, and the paired device no longer needs to be paired again at the next call. Paired devices, as the slave-side Bluetooth headset can also initiate a chain request, but the Bluetooth module that does the data communication generally does not initiate a call. When the link is established successfully, two-way data or voice communication can be done between the master and slave ends. In the communication state, both the primary and the slave devices can initiate a broken chain and disconnect the Bluetooth link.
3 Bluetooth one-to-one serial data transmission application
Bluetooth data transmission applications, a pair of one-port communication is one of the most common applications, Bluetooth equipment in the factory is set up in advance of the pairing information between two Bluetooth devices, the main side pre-stored from the terminal device PIN code, address, etc., the two ends of the device power-on-build chain, transparent serial transmission, without peripheral circuit intervention. The slave device can be set to two types, one is silent state, that can only communicate with the specified master, not be found by other Bluetooth devices, and the development status can be found either by the designated master or by other Bluetooth devices.
Four Android with BLE
1. Key Concepts:
Generic Attribute profile (GATT)
Through BLE connection, read and write the profile General specification of the attribute class small data. All BLE application profiles are now based on GATT.
Attribute Protocol (ATT)
GATT is based on the Attprotocol. The ATT has been specifically optimized for BLE devices by using as little data as possible during the transfer process. Each property has a unique UUID, and the properties are transferred as characteristics and services.
Characteristic
Characteristic can be understood as a data type that includes a value and a description of 0 to multiple pairs of secondary value (descriptor).
Descriptor
A description of the characteristic, such as the range, unit of measurement, etc.
Service
A collection of characteristic. For example, a service called "heart rate Monitor" may contain multiple characteristics, which may contain a characteristic called "Heart Ratemeasurement". A bracelet May
2. Roles and Responsibilities:
Android devices interact with BLE devices in two sets of roles:
Center and peripheral devices (central vs. peripheral);
GATT Server vs. GATT client.
Central vs. Peripheral:
The concept of central devices and peripherals is for the BLE connection itself. The central role is responsible for scan advertisement. And the peripheral role is responsible for make advertisement.
GATT Server vs. GATT Client:
Both roles depend on how the two devices communicate with each other after the BLE connection succeeds.
To illustrate:
There is an active tracking ble device and an Android device that supports BLE. The Android device supports the central role while the BLE device supports the peripheral role. Creating a BLE connection requires both of these roles to be present, only the central role is supported or only the peripheral role is supported, and the connection cannot be established.
When a connection is established, it is necessary to transfer GATT data between them. Who does the server, who does the client, depends on the specific data transfer situation. For example, if an activity-traced BLE device needs to transmit sensor data to an Android device, the activity tracker naturally becomes the server side, and if the activity tracker needs to get updated information from an Android device, the Android device might be more appropriate as the server side.
3 Permissions and Feature:
Like classic Bluetooth, apps use Bluetooth, you need to declare Bluetooth permissions, and if you need to scan your device or operate Bluetooth settings, you'll also need Bluetooth_admin permissions:
<uses-permissionandroid:name= "Android.permission.BLUETOOTH"/>
<uses-permission android:name= "Android.permission.BLUETOOTH_ADMIN"/>
In addition to Bluetooth permissions, if you need ble feature you also need to declare uses-feature:
<uses-featureandroid:name= "Android.hardware.bluetooth_le" android:required= "true"/>
On time required is true, the app can only be installed on an Android device that supports BLE, and when required is false, Android devices are installed and run, and you need to determine if your device supports BLE feature when your code is running:
Use this check to determine whether BLE is Supportedon the device. Then
You can selectively disable ble-related features.
if (!getpackagemanager (). Hassystemfeature (Packagemanager.feature_bluetooth_le)) {
Toast.maketext (this, R.string.ble_not_supported,toast.length_short). Show ();
Finish ();
}
4. Activate Bluetooth:
Before using Bluetooth ble, you need to confirm that your Android device supports BLE feature (required is false), and you need to confirm that Bluetooth is turned on.
If you find that BLE is not supported, you cannot use BLE-related features. If BLE is supported, but Bluetooth is not turned on, you need to turn on Bluetooth.
To turn on Bluetooth:
1. Get Bluetoothadapter
Bluetoothadapter is required for all Bluetooth operations in the Android system, it corresponds to the Bluetooth module of the local Android device, Bluetoothadapter is a singleton in the whole system. When you get an example of it, you can do the relevant Bluetooth operation.
Get the Bluetoothadapter code example as follows:
Initializes Bluetooth adapter.
Final Bluetoothmanager Bluetoothmanager =
(Bluetoothmanager) Getsystemservice (Context.bluetooth_service);
Mbluetoothadapter = Bluetoothmanager.getadapter ();
Note: Get Bluetoothmanager through Getsystemservice and get bluetoothadapter through Bluetoothmanager. Bluetoothmanager above Android4.3 support (API level 18).
2. Determine if Bluetooth is supported and turn on Bluetooth
After you get to Bluetoothadapter, you also need to determine whether Bluetooth is supported, and whether Bluetooth is turned on.
If not, you need to have the user turn on Bluetooth:
Private Bluetoothadapter Mbluetoothadapter;
...
Ensures Bluetooth is available on the device and it is enabled. If not,
Displays a dialog requesting user permission to enable Bluetooth.
if (Mbluetoothadapter = = NULL | |!mbluetoothadapter.isenabled ()) {
Intent enablebtintent = newintent (bluetoothadapter.action_request_enable);
Startactivityforresult (Enablebtintent, REQUEST_ENABLE_BT);
}
5. Search for BLE devices:
Search the BLE device by calling Bluetoothadapter's startlescan () . You need to pass in the bluetoothadapter.lescancallback parameter when calling this method.
So you need to implement the Bluetoothadapter.lescancallback interface, the BLE device search results will be returned through this callback.
Because the search needs to minimize power consumption, you need to be aware of the actual use:
1. When the corresponding equipment is found, stop scanning immediately;
2, do not cycle search devices, set the appropriate time limit for each search. Avoid constant scanning and power consumption when the device is not in the available range.
The sample code for the search is as follows:
/** * Activity for scanning and displaying available BLE devices. */public class Devicescanactivity extends Listactivity {private Bluetoothadapter mbluetoothadapter; Private Boolean mscanning; Private Handler Mhandler; Stops scanning after ten seconds. Private static final Long scan_period = 10000; ... private void Scanledevice (final Boolean enable) {if (enable) {//Stops scanning after a pre-def ined Scan period. Mhandler.postdelayed (New Runnable () {@Override public void run () {MScan ning = false; Mbluetoothadapter.stoplescan (Mlescancallback); }}, Scan_period); Mscanning = true; Mbluetoothadapter.startlescan (Mlescancallback); } else {mscanning = false; Mbluetoothadapter.stoplescan (Mlescancallback); } }}
If you only need to search the peripheral for the specified UUID, you can call the Startlescan (uuid[], Bluetoothadapter.lescancallback) method.
Where the UUID array specifies the UUID of the GATT services supported by your application.
Examples of implementations of Bluetoothadapter.lescancallback are as follows:
Private Ledevicelistadapter mledevicelistadapter;...//Device scan callback.private bluetoothadapter.lescancallback Mlescancallback = New Bluetoothadapter.lescancallback () { @Override public void Onlescan (final Bluetoothdevice device, int rssi, byte[] scanrecord) { runonuithread (new Runnable () { @Override public void Run () { mledevicelistadapter.adddevice (device); Mledevicelistadapter.notifydatasetchanged ();}});} ;
Note: When searching, you can only search for traditional Bluetooth devices or BLE devices, which are completely independent and not searchable at the same time.
6. Connection Gattserver:
The two devices communicate via BLE, and the GATT connection needs to be established first. Here we are talking about the Android device as the client side, connected to the GATT Server.
To connect to the GATT Server, you need to call Bluetoothdevice's Connectgatt () method. This function takes three parameters: Context, AutoConnect (Boolean), and Bluetoothgattcallback object. Invocation Example:
Mbluetoothgatt = Device.connectgatt (this, false,mgattcallback);
The function succeeds and returns the Bluetoothgatt object, which is the encapsulation of the GATT profile. With this object, we can perform the relevant operations on the client side of the GATT. Bluetoothgattcallback is used to pass some connection states and results.
Bluetoothgatt Several examples of operations that are used in general:
Connect (): Connects to the remote device.
Discoverservices (): Search for the service supported by the connected device.
Disconnect (): Disconnects the GATT connection to the remote device.
Close (): Closes the gattclient end.
Readcharacteristic (characteristic): reads the specified characteristic.
Setcharacteristicnotification (characteristic, enabled): Sets a notification when the specified characteristic value changes.
GetServices (): Gets the services supported by the remote device.
Wait a minute.
Note:
1. There is a succession relationship between some function calls. For example, you need connect to discoverservices first.
2, some function calls are asynchronous, the required value will not be returned immediately, but will be returned in the Bluetoothgattcallback callback function. For example, Discoverservices and onservicesdiscovered callbacks, Readcharacteristic and Oncharacteristicread callbacks, Setcharacteristicnotification and Oncharacteristicchanged callback and so on.
Http://www.blogjava.net/zh-weir/archive/2013/12/09/407373.html
http://blog.csdn.net/mov2012/article/details/16368441
Baidu Documentation:
BLE Introductory tutorial. pdf
Ble_ and classic Bluetooth Android programming instructions. pdf
ble introduction and Android ble programming