Android BLE bluetooth Low power tutorial, Central Bluetoothgatt and the implementation of the peripheral bluetoothgattserver
Source: Internet
Author: User
<span id="Label3"></p><p><p><span style="font-size: 18px;">Android4.3 has standardized the BLE api, but until now 4.4, some features are not perfect.</span></p></p><p><p><span style="font-size: 18px;">In the BLE protocol, there are two roles, the perimeter (periphery) and the center (central), the periphery is the data provider, the central is the data use/processor; in the iOS sdk, you can use an iOS device as a perimeter or as a central; Inside the sdk, until now the newest android4.4.2,android mobile phone can only use and process data as the center; where does the data come from? From BLE devices, Many wearable devices now use BLE to provide Data.</span></p></p><p><p><span style="font-size: 18px;">A central can be connected to multiple peripherals at the same time, but only one central area is connected at a Time.</span></p></p><p><p><span style="font-size: 18px;">If you know the concept, take a look at the four key classes of the Android BLE SDK (class):</span></p></p><p><p><span style="font-size: 18px;">A)<strong> Bluetoothgattserver</strong> as a peripheral to provide data;<strong>bluetoothgattservercallback</strong> returns to the surrounding State.</span></p></p><p><p><span style="font-size: 18px;">B) <strong>Bluetoothgatt</strong> uses and processes data as a central part;<strong>bluetoothgattcallback</strong> returns to the central State and data provided by the Perimeter.</span></p></p><p><p><span style="font-size: 18px;">Because we are talking about the android ble SDK, all of the following bluetoothgattserver represent the perimeter, Bluetoothgatt represents the Central.</span></p></p><p><p><span style="font-size: 18px;"></span></p></p><p><p><span style="font-size: 18px;"><strong>I. Create a perimeter</strong> (although the current perimeter API does not work on Android phones, but still look)</span></p></p><p><p><span style="font-size: 18px;">A) first look at the surrounding class, the blue Oval</span></p></p><p><p><span style="font-size: 18px;"></span></p></p><p><p><span style="font-size: 18px;">B) description:</span></p></p><p><p><span style="font-size: 18px;">Each of the perimeter bluetoothgattserver contains multiple service services, each of which contains multiple feature characteristic.</span></p></p><p><p><span style="font-size: 18px;">1.new a feature: character = new Bluetoothgattcharacteristic (</span><br><span style="font-size: 18px;">Uuid.fromstring (characteristicuuid),</span><br><span style="font-size: 18px;">bluetoothgattcharacteristic.property_notify,</span><br><span style="font-size: 18px;">bluetoothgattcharacteristic.permission_read);</span></p></p><p><p><span style="font-size: 18px;">2.new services: service = new Bluetoothgattservice (uuid.fromstring (serviceuuid),</span><br><span style="font-size: 18px;">bluetoothgattservice.service_type_primary);</span></p></p><p><p><span style="font-size: 18px;">3. Adding features to the service: service.addcharacteristic (character);</span></p></p><p><p><span style="font-size: 18px;">4. Get Bluetoothmanager:manager = (bluetoothmanager) Getsystemservice (context.bluetooth_service);</span></p></p><p><p><span style="font-size: 18px;">5. Get/open perimeter: bluetoothgattserver Server = Manager.opengattserver (this,</span><br><span style="font-size: 18px;">New Bluetoothgattservercallback () {...});</span></p></p><p><p><span style="font-size: 18px;">6. Add the service to the Perimeter: Server.addservice (service);</span></p></p><p><p><span style="font-size: 18px;">7. Start Broadcasting Service:google not yet broadcast service api, wait!!!!! So for now we can't have an Android phone as a perimeter to provide Data.</span></p></p><p><p><span style="font-size: 18px;"></span></p></p><p><p><span style="font-size: 18px;"><strong>two. Create a central</strong> (this time will not let you down, can be successfully created and connected to the Surrounding)</span></p></p><p><p><span style="font-size: 18px;">A) first look at the class used in the center, the blue Oval</span></p></p><p><p><span style="font-size: 18px;"></span></p></p><p><p><span style="font-size: 18px;"></span></p></p><p><p><span style="font-size: 18px;">B) description:</span></p></p><p><p><span style="font-size: 18px;">To get the central bluetoothgatt, you have to Shanshan 18 bends:</span></p></p><p><p><span style="font-size: 18px;">1. First Get Bluetoothmanager:bluetoothmanager = (bluetoothmanager) Getsystemservice (context.bluetooth_service);</span></p></p><p><p><span style="font-size: 18px;">2. Get bluetoothadapt:btadapter = Bluetoothmanager.getadapter ();</span></p></p><p><p><span style="font-size: 18px;">3. Start scanning: Btadapter.startlescan (bluetoothadapter.lescancallback);</span></p></p><p><p><span style="font-size: 18px;">4. Get bluetoothdevice:public void onlescan (bluetoothdevice device, int rssi, byte[] scanrecord) from Lescancallback {...}</span></p></p><p><p><span style="font-size: 18px;">5. Use Bluetoothdevice to get Bluetoothgatt:gatt = Device.connectgatt (this, true, gattcallback);</span></p></p><p><p><span style="font-size: 18px;">Finally get the central bluetoothgatt, it has a bunch of methods (check API bar), Call these methods, you can through the Bluetoothgattcallback and the surrounding Bluetoothgattserver Interaction.</span></p></p><p><p><span style="font-size: 18px;"></span></p></p><p><p><span style="font-size: 18px;">Demo Project: http://download.csdn.net/detail/jimoduwu/7072515</span></p></p><p><p>Android BLE bluetooth Low power tutorial, Central Bluetoothgatt and the implementation of the peripheral bluetoothgattserver</p></p></span>
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