Android ble Bluetooth 4.0 Summary 1: androidble

Source: Internet
Author: User

Android ble Bluetooth 4.0 Summary 1: androidble

This article introduces Android ble Bluetooth 4.0, that is, API level> = 18, which can only be used by mobile phones that support Bluetooth 4.0. If the mobile phone system version API level is less than 18, Bluetooth 4.0 cannot be used.

First, send an official demo. If you are interested, visit http://developer.android.com/guide/topics/connectivity/~th-le.html. Android 4.3 or above, mobile phone supports Bluetooth 4.0, with search, pairing, connection, service and feature value discovery, disconnection and other functions,: http://download.csdn.net/detail/lqw770737185/8116019.

1. Understand APIs and concepts

1.1 BluetoothGatt

Inherits the descrithprofile and connects to the device (connect) through descrithgatt to discover the Service (discoverServices), and returns the corresponding property to descrithgattcallback.

1.2 faster thgattcharacteristic

It is equivalent to a data type, which includes a value and 0 ~ Description of n values (descrithgattdescriptor)

1.3 define thgattdescriptor

The description of Characteristic, including the range and unit of measurement.

1.4 define thgattservice

A set of Characteristic services.

1.5 define thprofile

A general specification that allows you to send and receive data.

1.6 define thmanager

Use the thmanager to obtain the thadapter

BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);

1.7 define thadapter

An Android system has only one javasthadapter, which is obtained through javasthmanager.

BluetoothAdapter mBluetoothAdapter = bluetoothManager.getAdapter();

1.8 define thgattcallback

The result returned after some operations on the device. Here, you must note that only those connected to the device can be returned. If not, check whether the device is connected.

Private synchronized thgattcallback GattCallback = new BluetoothGattCallback () {// There are nine methods to be implemented here. Depending on the situation, the public void onConnectionStateChange (implements thgatt gatt, int status, int newState) {}; public void onCharacteristicWrite (descrithgatt gatt, descrithgattcharacteristic characteristic, int status) {};}; descrithdevice device = mbluw.thadapter. getRemoteDevice (address); gatethgatt gatt = device. connectGatt (this, false, mGattCallback );

1.8.1: corresponding to notificationOnCharacteristicChanged

gatt.setCharacteristicNotification(characteristic, true);

1.8.2:ReadCharacteristic correspondenceOnCharacteristicRead;

gatt.readCharacteristic(characteristic);

1.8.3:WriteCharacteristic correspondenceOnCharacteristicWrite;

gatt.wirteCharacteristic(mCurrentcharacteristic);

1.8.4: connect to or disconnect the corresponding onConnectionStateChange;

1.8.5:ReadDescriptorOnDescriptorRead;

1.8.6: writeDescriptor corresponds to onDescriptorWrite;

gatt.writeDescriptor(descriptor); 

1.8.7: readRemoteRssi corresponds to onReadRemoteRssi;

gatt.readRemoteRssi()

1.8.8: executeReliableWrite corresponds to onReliableWriteCompleted;

1.8.9: discoverServices corresponds to onServicesDiscovered.

gatt.discoverServices()

1.9 define thdevice

After scanning, you can find connected devices and obtain connected devices.

2. Enable the Bluetooth permission

<uses-permission android:name="android.permission.BLUETOOTH"/><uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/><uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/>

If android. hardware. javasth_le is set to false, it can be installed on an unsupported device. You can use the following code to determine whether Bluetooth 4.0 is supported.

If (! GetPackageManager (). hasSystemFeature (PackageManager. feature_effecth_le) {Toast. makeText (this, "the device does not support Bluetooth 4.0", Toast. LENGTH_SHORT). show (); finish ();}

3. Enable and disable Bluetooth

1. Enable the Bluetooth dialog box by default.

if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled()) {    Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);    startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);}

2. Enable Bluetooth in the background without any prompts. You can also use custom settings to open the Bluetooth dialog box.

mBluetoothAdapter.enable();

3. Disable Bluetooth in the background

mBluetoothAdapter.disable();

4. Scan the device, connect to the device, obtain device information, disconnect the device, and view the official demo on your own. The demo is clear.


What is the difference between samsung ble Bluetooth 40 and moto ble Bluetooth 40 ??

The two APIs are different in development.
 
Bluetooth 40 BLE support

Hi!
Hahaha what rom can I fl my 2 S to 41 million pixels ??
Bluetooth in meters 2 is 4.0
Xiaomi enterprise Platform [Official Certification]

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.