Android / Using Bluetooth Low Energy on Android
[
Front page
] [
New
|
List of pages
|
Search
|
Recent changes
]
Start:
[[labs.beatcraft.com]]~
[[Android]]~
#contents
*1. About BLE (Bluetooth Low Energy) [#y7765074]
>
BLE is the abbreviation of Bluetooth Low Energy and a par...
~
Technical Overview:~
- Ultra-low power consumption (being capable of operating...
- No compatibility with the previous version of Bluetooth...
- Communication distance is 2.5m to 10m.~
- A single BLE unit does not have restrictions on the num...
*2. Basic knowledges of BLE communication [#adc02a2b]
>
In the communication of BLE, GATT (Generic Attribute Prof...
A device (server) consists of Profile (GATT), Service, Ch...
~
&ref(./gatt.png);
** Profile [#n3183b5b]
>
GATT (Generic Attribute Profile) is a theoretical design ...
~
GATT consists of three elements, such as Service, Charact...
Profile does have more than one Service.~
** Service [#l6a76675]
>
Service shows the functions of Profile.~
Service consists of several nesting Services and several ...
** Characteristic [#tc5d1590]
>
Characteristic does have a single value, which indicate i...
It consists of own value, property, which defines how to ...
** Descriptor [#f831458a]
>
Descriptor shows the attribute value, which is used when ...
Some Characteristics do NOT contain any Descriptors.~
* 3. BLE on Android [#fb8170f3]
Android 4.3 or above (API Level 18 or greater) supports B...
** Bluetooth Package [#nf593f39]
>
android.bluetooth.*
**BluetoothManager [#fb739711]
>
It manages the functions of Bluetooth.~
It acquires BluetoothAdapter from BluetoothAdapter.~
**BluetoothAdapter [#g5f81df8]
>
It controls Bluetooth set at a mobile device.~
It scans Bluetooth devices.
**BluetoothGatt [#ibcf03c2]
>
It operates GATT profile.~
It manages the search for Services, and the configuration...
**BluetoothDevice [#e336bbd3]
>
It receives the scanning results of BluetoothAdapter.~
It is the information of Bluet device.~
**BluetoothGattService [#f2608ca1]
>
It is the information of (GATT) Services.
It holds Characteristic (BluetoothGattCharacteristic).
**BluetoothGattCharacteristic [#kc93d4f6]
>
It is the information of Characteristic.~
It holds Descriptor (BluetoothGattDescriptor). Some Chara...
**BluetoothGattDescriptor [#dc84db56]
>
The information of Descriptor.
* 4. Implementation of BLE on App [#a44fe1c4]
** Permission [#lc3905b5]
>
Please add these tags listed below to Manifest (AndroidMa...
Configuration for Permission~
<uses-permission android:name="android.permission.BLUETO...
<uses-permission android:name="android.permission.BLUETO...
~
Please add the tag to Manifest for making sure that the A...
<uses-feature android:name="android.hardware.bluetooth_l...
** Initializing [#u1b9a40c]
>
BluetoothManager bluetoothManager = (BluetoothManager)ge...
mBluetoothAdapter = bluetoothManager.getAdapter();
~
**Making BLE effective [#b671573e]
>
As the app is executed, this checks whether Bluetooth fun...
BluetoothManager bluetoothManager = (BluetoothManager)ge...
BluetoothAdapter mBluetoothAdapter = bluetoothMa...
if (mBluetoothAdapter == null) {
return false;
}
// Checking Bluetooth function effective or not....
if (mBluetoothAdapter.isEnabled()) {
// Bluetooth is effective
} else {
// Bluetooth is NOT enabled, and the con...
Intent enableBtIntent = new Intent(Bluet...
activity.startActivityForResult(enableBt...
}
** Scanning [#g85c27f1]
>
// Stop scanning 10 seconds later.
private static final long SCAN_PERIOD = 10000;
...
private void scanLeDevice(final boolean enable) {
if (enable) {
// Stops scanning after a pre-defined sc...
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
mScanning = false;
mBluetoothAdapter.stopLeScan(mLe...
}
}, SCAN_PERIOD);
mScanning = true;
mBluetoothAdapter.startLeScan(mLeScanCal...
} else {
mScanning = false;
mBluetoothAdapter.stopLeScan(mLeScanCall...
}
...
}
private LeDeviceListAdapter mLeDeviceListAdapter;
...
// The callback function after device scanning i...
private BluetoothAdapter.LeScanCallback mLeScanC...
new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(final BluetoothDevice d...
byte[] scanRecord) {
runOnUiThread(new Runnable() {
@Override
public void run() {
String msg = "na...
+ device.getBondState() + ", add...
+ device.getAddress() + ", type"...
+ ", uuid = " + device.getUuid()...
Log.d("Scan", msg);}
});
}
};
** Connecting with a device [#k7158f57]
>
Call BluetoothDevice$connectGatt(), which is returned for...
~
BluetoothDevice:
BluetoothGatt connectGatt(Context context, boole...
** Implementation of BluetoothGattCallback [#j35c7555]
>
private final BluetoothGattCallback callback = ne...
// This is executed when the status...
@Override
public void onConnectionStateChange(Bluetoot...
// It is connected
if (newState == BluetoothProfile.STATE_C...
Log.i(TAG, "Connected to GATT server...
// Searching for Services
Log.i(TAG, "Attempting to start serv...
// It is disconnected.
} else if (newState == BluetoothProfile....
Log.i(TAG, "Disconnected from GATT s...
}
}
@Override
// Return the search results of Services
public void onServicesDiscovered(BluetoothGa...
// Success
if (status == BluetoothGatt.GATT_SUCCESS...
// Acquire the list of Services.
List<BluetoothGattService> gattServi...
for(BluetoothGattService service : g...
UUID uuid = service.getUuid();
// Acquire the list of Character...
List<BluetoothGattCharacteristic...
// Set up Charac...
for(BluetoothGattCharacteristic ...
//
gatt.setCharacteristicNotifi...
}
}
} else {
Log.w(TAG, "onServicesDiscovered rec...
}
}
// Notification for reading
@Override
public void onCharacteristicRead(BluetoothGa...
if (status == BluetoothGatt.GATT_SUCCESS...
// Characteristic is suc...
}
}
// Notification for writing
@Override
public void onCharacteristicWrite(Bluetooth...
if (status == BluetoothGatt.GATT_SUCCESS...
// Characteristic is suc...
}
...
};
** Reading [#c30fa84b]
>
Since reading is asynchronous, the reading process requir...
~
Execute BluetoothGatt#readCharacteristic().~
The results are returned to BluetoothGattCallBack#onChara...
*** Reading request [#e1dcd1a3]
>
mBluetoothGatt.readCharacteristic(characteristic);
*** Reading notification [#p354aad6]
>
public void onCharacteristicRead(BluetoothGatt gatt,Blue...
if (status == BluetoothGatt.GATT_SUCCESS) {
// Characteristic is successfully read.
}
}
** Writing [#w78a8d9a]
>
Since writing is asynchronous, the writing process requir...
~
Execute BluetoothGatt#writeCharacteristic()~
The results are returned to BluetoothGattCallBack#onChara...
*** Writing request [#a68e81f3]
>
mBluetoothGatt.writeCharacteristic(characteristic);
*** Writing notification [#q71f3ab3]
>
public void onCharacteristicWrite(BluetoothGatt...
if (status == BluetoothGatt.GATT_SUCCESS) {
// Characteristic is successfully written.
}
}
** Releasing the connection [#y33a1d42]
>
Execute BluetoothGatt#close().
* Connecting multiple devices [#k0d7c1cf]
>
To connect multiple devices, do not connect them simulta...
If the new connection attempt is started before the curre...
If the new connection attempt is started before the scann...
** Case (Example) [#k352aecc]
>
Connect with Device A, B, and C.
** Processing procedure [#k41fe845]
>
1. Connect to Device A~
BluetoothDevice#connectGatt()~
2. Wait for the results of connection to Device A~
BluetoothGattCallback#onConnectionStateChange()~
3. Connect to Device B~
BluetoothDevice#connectGatt()~
4. Wait for the results of connection to Device B~
BluetoothGattCallback#onConnectionStateChange()~
5. Connect to Device C~
BluetoothDevice#connectGatt()~
6. Wait for the results of connection to Device C~
BluetoothGattCallback#onConnectionStateChange()~
* About demonstration at Embedded Technology 2015 [#h168d...
>
Exhibiting a single Android device is connected to 6 BLE...
***Multiple device connection [#p893dfd2]
>
Multiple BLE devices are connected.~
6 devices are connected one by one.~
#youtube(FCtvmpk3XSE);
*** LED light [#sc24d6fb]
>
There are three LED lights, and each light is capable of ...
The brightness of LED lights is changed individually or c...
#youtube(e8rH0BX88Nc);
***Switch [#j748179f]
>
Employing three relays, turn on/off LED lights. It manage...
#youtube(115ipQ69tik);
***LED color light valve [#sab94637]
>
It controls the color (RGB) of LED color light valve.~
#youtube(u_36gwa3jsE);
* Revision History [#w5a8e891]
>
- 2016-02-19 This article is initially released.
End:
[[labs.beatcraft.com]]~
[[Android]]~
#contents
*1. About BLE (Bluetooth Low Energy) [#y7765074]
>
BLE is the abbreviation of Bluetooth Low Energy and a par...
~
Technical Overview:~
- Ultra-low power consumption (being capable of operating...
- No compatibility with the previous version of Bluetooth...
- Communication distance is 2.5m to 10m.~
- A single BLE unit does not have restrictions on the num...
*2. Basic knowledges of BLE communication [#adc02a2b]
>
In the communication of BLE, GATT (Generic Attribute Prof...
A device (server) consists of Profile (GATT), Service, Ch...
~
&ref(./gatt.png);
** Profile [#n3183b5b]
>
GATT (Generic Attribute Profile) is a theoretical design ...
~
GATT consists of three elements, such as Service, Charact...
Profile does have more than one Service.~
** Service [#l6a76675]
>
Service shows the functions of Profile.~
Service consists of several nesting Services and several ...
** Characteristic [#tc5d1590]
>
Characteristic does have a single value, which indicate i...
It consists of own value, property, which defines how to ...
** Descriptor [#f831458a]
>
Descriptor shows the attribute value, which is used when ...
Some Characteristics do NOT contain any Descriptors.~
* 3. BLE on Android [#fb8170f3]
Android 4.3 or above (API Level 18 or greater) supports B...
** Bluetooth Package [#nf593f39]
>
android.bluetooth.*
**BluetoothManager [#fb739711]
>
It manages the functions of Bluetooth.~
It acquires BluetoothAdapter from BluetoothAdapter.~
**BluetoothAdapter [#g5f81df8]
>
It controls Bluetooth set at a mobile device.~
It scans Bluetooth devices.
**BluetoothGatt [#ibcf03c2]
>
It operates GATT profile.~
It manages the search for Services, and the configuration...
**BluetoothDevice [#e336bbd3]
>
It receives the scanning results of BluetoothAdapter.~
It is the information of Bluet device.~
**BluetoothGattService [#f2608ca1]
>
It is the information of (GATT) Services.
It holds Characteristic (BluetoothGattCharacteristic).
**BluetoothGattCharacteristic [#kc93d4f6]
>
It is the information of Characteristic.~
It holds Descriptor (BluetoothGattDescriptor). Some Chara...
**BluetoothGattDescriptor [#dc84db56]
>
The information of Descriptor.
* 4. Implementation of BLE on App [#a44fe1c4]
** Permission [#lc3905b5]
>
Please add these tags listed below to Manifest (AndroidMa...
Configuration for Permission~
<uses-permission android:name="android.permission.BLUETO...
<uses-permission android:name="android.permission.BLUETO...
~
Please add the tag to Manifest for making sure that the A...
<uses-feature android:name="android.hardware.bluetooth_l...
** Initializing [#u1b9a40c]
>
BluetoothManager bluetoothManager = (BluetoothManager)ge...
mBluetoothAdapter = bluetoothManager.getAdapter();
~
**Making BLE effective [#b671573e]
>
As the app is executed, this checks whether Bluetooth fun...
BluetoothManager bluetoothManager = (BluetoothManager)ge...
BluetoothAdapter mBluetoothAdapter = bluetoothMa...
if (mBluetoothAdapter == null) {
return false;
}
// Checking Bluetooth function effective or not....
if (mBluetoothAdapter.isEnabled()) {
// Bluetooth is effective
} else {
// Bluetooth is NOT enabled, and the con...
Intent enableBtIntent = new Intent(Bluet...
activity.startActivityForResult(enableBt...
}
** Scanning [#g85c27f1]
>
// Stop scanning 10 seconds later.
private static final long SCAN_PERIOD = 10000;
...
private void scanLeDevice(final boolean enable) {
if (enable) {
// Stops scanning after a pre-defined sc...
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
mScanning = false;
mBluetoothAdapter.stopLeScan(mLe...
}
}, SCAN_PERIOD);
mScanning = true;
mBluetoothAdapter.startLeScan(mLeScanCal...
} else {
mScanning = false;
mBluetoothAdapter.stopLeScan(mLeScanCall...
}
...
}
private LeDeviceListAdapter mLeDeviceListAdapter;
...
// The callback function after device scanning i...
private BluetoothAdapter.LeScanCallback mLeScanC...
new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(final BluetoothDevice d...
byte[] scanRecord) {
runOnUiThread(new Runnable() {
@Override
public void run() {
String msg = "na...
+ device.getBondState() + ", add...
+ device.getAddress() + ", type"...
+ ", uuid = " + device.getUuid()...
Log.d("Scan", msg);}
});
}
};
** Connecting with a device [#k7158f57]
>
Call BluetoothDevice$connectGatt(), which is returned for...
~
BluetoothDevice:
BluetoothGatt connectGatt(Context context, boole...
** Implementation of BluetoothGattCallback [#j35c7555]
>
private final BluetoothGattCallback callback = ne...
// This is executed when the status...
@Override
public void onConnectionStateChange(Bluetoot...
// It is connected
if (newState == BluetoothProfile.STATE_C...
Log.i(TAG, "Connected to GATT server...
// Searching for Services
Log.i(TAG, "Attempting to start serv...
// It is disconnected.
} else if (newState == BluetoothProfile....
Log.i(TAG, "Disconnected from GATT s...
}
}
@Override
// Return the search results of Services
public void onServicesDiscovered(BluetoothGa...
// Success
if (status == BluetoothGatt.GATT_SUCCESS...
// Acquire the list of Services.
List<BluetoothGattService> gattServi...
for(BluetoothGattService service : g...
UUID uuid = service.getUuid();
// Acquire the list of Character...
List<BluetoothGattCharacteristic...
// Set up Charac...
for(BluetoothGattCharacteristic ...
//
gatt.setCharacteristicNotifi...
}
}
} else {
Log.w(TAG, "onServicesDiscovered rec...
}
}
// Notification for reading
@Override
public void onCharacteristicRead(BluetoothGa...
if (status == BluetoothGatt.GATT_SUCCESS...
// Characteristic is suc...
}
}
// Notification for writing
@Override
public void onCharacteristicWrite(Bluetooth...
if (status == BluetoothGatt.GATT_SUCCESS...
// Characteristic is suc...
}
...
};
** Reading [#c30fa84b]
>
Since reading is asynchronous, the reading process requir...
~
Execute BluetoothGatt#readCharacteristic().~
The results are returned to BluetoothGattCallBack#onChara...
*** Reading request [#e1dcd1a3]
>
mBluetoothGatt.readCharacteristic(characteristic);
*** Reading notification [#p354aad6]
>
public void onCharacteristicRead(BluetoothGatt gatt,Blue...
if (status == BluetoothGatt.GATT_SUCCESS) {
// Characteristic is successfully read.
}
}
** Writing [#w78a8d9a]
>
Since writing is asynchronous, the writing process requir...
~
Execute BluetoothGatt#writeCharacteristic()~
The results are returned to BluetoothGattCallBack#onChara...
*** Writing request [#a68e81f3]
>
mBluetoothGatt.writeCharacteristic(characteristic);
*** Writing notification [#q71f3ab3]
>
public void onCharacteristicWrite(BluetoothGatt...
if (status == BluetoothGatt.GATT_SUCCESS) {
// Characteristic is successfully written.
}
}
** Releasing the connection [#y33a1d42]
>
Execute BluetoothGatt#close().
* Connecting multiple devices [#k0d7c1cf]
>
To connect multiple devices, do not connect them simulta...
If the new connection attempt is started before the curre...
If the new connection attempt is started before the scann...
** Case (Example) [#k352aecc]
>
Connect with Device A, B, and C.
** Processing procedure [#k41fe845]
>
1. Connect to Device A~
BluetoothDevice#connectGatt()~
2. Wait for the results of connection to Device A~
BluetoothGattCallback#onConnectionStateChange()~
3. Connect to Device B~
BluetoothDevice#connectGatt()~
4. Wait for the results of connection to Device B~
BluetoothGattCallback#onConnectionStateChange()~
5. Connect to Device C~
BluetoothDevice#connectGatt()~
6. Wait for the results of connection to Device C~
BluetoothGattCallback#onConnectionStateChange()~
* About demonstration at Embedded Technology 2015 [#h168d...
>
Exhibiting a single Android device is connected to 6 BLE...
***Multiple device connection [#p893dfd2]
>
Multiple BLE devices are connected.~
6 devices are connected one by one.~
#youtube(FCtvmpk3XSE);
*** LED light [#sc24d6fb]
>
There are three LED lights, and each light is capable of ...
The brightness of LED lights is changed individually or c...
#youtube(e8rH0BX88Nc);
***Switch [#j748179f]
>
Employing three relays, turn on/off LED lights. It manage...
#youtube(115ipQ69tik);
***LED color light valve [#sab94637]
>
It controls the color (RGB) of LED color light valve.~
#youtube(u_36gwa3jsE);
* Revision History [#w5a8e891]
>
- 2016-02-19 This article is initially released.
Page: