How to Implement Bluetooth Low Energy (BLE) in Ice Cream Sandwich
2014-05-08 19:35
609 查看
ShareThis
- By Vikas Verma
Bluetooth low energy (BLE) is a feature of Bluetooth 4.0 wireless radio technology, aimed at new, principally low-power and low-latency, applications for wireless devices within a short range. As I discussed in my previous blog about BLE in Android, it has been acknowledged that using this technology in devices will lead to very less power consumption and increase in performance. BLE being a latest technology and in the development mode, is not being used by many devices as of now but this is poised to change soon. It will soon become an essential part of devices that use Bluetooth.
As the Bluetooth Low Energy stack is available with some third party
vendors, they have their own set of APIs for BLE programming. You just
need to include their API add-ons into your Android apps to access the
BLE features.
As we know that in Android, there is no generic API for BLE. Different vendors give their own API's for Android App development.
Here we are going to discuss about Motorola API. The profile
specification allows performing the read and writing operations only
when the Bluetooth Low Energy API's allow connecting with the
remote device.
Some developments have been done with BLE to support the latest Android version ICS (Ice Cream Sandwich) and I have tried to summarize some useful information for you to implement with your latest Motorola device having ICS.
Use this link to download the Add-on for ICS (Ice Cream Sandwich) operating system.
Two libraries, BluetoothGattService.jar and BluetoothGatt.jar are used in Android project.
You need to create two files:
i) android.bluetooth.IBluetoothGattProfile; //This file needs to be created
interface IBluetoothGattProfile {
void onDiscoverCharacteristicsResult(in String path, in boolean result);
void onSetCharacteristicValueResult(in String path, in boolean result);
void onSetCharacteristicCliConfResult(in String path, in boolean result);
void onUpdateCharacteristicValueResult(in String path, in boolean result);
void onValueChanged(in String path, in String value);
}
ii)com.motorola.bluetooth.bluetoothle.IBluetoothGattCallback; //This file needs to be created
/**
* System private API for Bluetooth GATT Service
*
*
*/
oneway interface IBluetoothGattCallback
{
void indicationGattCb (in BluetoothDevice device, String uuid, String char_handle, in String[] data);
void notificationGattCb (in BluetoothDevice device , String uuid, String char_handle, in byte[] data);
}
Now in main activity, initially you have to scan for bluetooth devices and then check for the BLE device by calling function getBluetoothDeviceType(). If it returns LE that means the device you scanned for is a BLE device
After that you need to call getGattServices(uuid, device) for searching the primary device.
private boolean getGattServices(ParcelUuid uuid, BluetoothDevice btDevice)
{
Log.d(TAG, "Calling btDevice.getGattServices");
return btDevice.getGattServices(uuid.getUuid());
}
Parameters
Device: Device address of the GATT server being connected to.
Uuid: UUID of the primary service to which your profile is connecting.
Then you get the broadcast with the action BluetoothDevice.ACTION_GATT and then call the function
getBluetoothGattService(selectedServiceObjPath, uuid);
private void getBluetoothGattService(String objPath, ParcelUuid uuid)
{
if (mDevice != null)
{
BluetoothGattService gattService = new BluetoothGattService(mDevice,uid,objPath,btGattCallback);
if (gattService != null)
{
uuidGattSrvMap.put(uuid, gattService);
return;
}
else
{
Log.e(TAG, "Gatt service is null for UUID");
}
}
else
{
Log.e(TAG, mDevice is null");
}
mLeState = DISCONNECTED;
Toast.makeText(mContext,"Connection Failed", Toast.LENGTH_SHORT).show();
}
Parameters in BluetoothGattService
device: device address of the GATT server being connected to.
Uuid : UUID of the primary service to which your profile is connecting.
Callback: IBluetoothGattProfile.Stub objects that receives user data from the connected service.
Methods used by Bluetooth LE profiles
i) How to read the value of a service
gattService.updateCharacteristicValue(objPath)
objPath is a mapped path for the service you want to read.
ii) How to write a value
gattService.writeCharacteristicRaw(objPath, data, true);
objPath is a mapped path for the service you want to write.
Data is a byte array which you want to write
iii) How to disconnect
gattService.close();
NOTE: You can find the sample inside the Motorola
Add-ons. The above mentioned API is still in development mode and you
might come across with some development issues (bugs) as it is a BETA
Release.
- By Vikas Verma
Bluetooth low energy (BLE) is a feature of Bluetooth 4.0 wireless radio technology, aimed at new, principally low-power and low-latency, applications for wireless devices within a short range. As I discussed in my previous blog about BLE in Android, it has been acknowledged that using this technology in devices will lead to very less power consumption and increase in performance. BLE being a latest technology and in the development mode, is not being used by many devices as of now but this is poised to change soon. It will soon become an essential part of devices that use Bluetooth.
As the Bluetooth Low Energy stack is available with some third party
vendors, they have their own set of APIs for BLE programming. You just
need to include their API add-ons into your Android apps to access the
BLE features.
As we know that in Android, there is no generic API for BLE. Different vendors give their own API's for Android App development.
Here we are going to discuss about Motorola API. The profile
specification allows performing the read and writing operations only
when the Bluetooth Low Energy API's allow connecting with the
remote device.
Some developments have been done with BLE to support the latest Android version ICS (Ice Cream Sandwich) and I have tried to summarize some useful information for you to implement with your latest Motorola device having ICS.
Use this link to download the Add-on for ICS (Ice Cream Sandwich) operating system.
Two libraries, BluetoothGattService.jar and BluetoothGatt.jar are used in Android project.
You need to create two files:
i) android.bluetooth.IBluetoothGattProfile; //This file needs to be created
interface IBluetoothGattProfile {
void onDiscoverCharacteristicsResult(in String path, in boolean result);
void onSetCharacteristicValueResult(in String path, in boolean result);
void onSetCharacteristicCliConfResult(in String path, in boolean result);
void onUpdateCharacteristicValueResult(in String path, in boolean result);
void onValueChanged(in String path, in String value);
}
ii)com.motorola.bluetooth.bluetoothle.IBluetoothGattCallback; //This file needs to be created
/**
* System private API for Bluetooth GATT Service
*
*
*/
oneway interface IBluetoothGattCallback
{
void indicationGattCb (in BluetoothDevice device, String uuid, String char_handle, in String[] data);
void notificationGattCb (in BluetoothDevice device , String uuid, String char_handle, in byte[] data);
}
Now in main activity, initially you have to scan for bluetooth devices and then check for the BLE device by calling function getBluetoothDeviceType(). If it returns LE that means the device you scanned for is a BLE device
After that you need to call getGattServices(uuid, device) for searching the primary device.
private boolean getGattServices(ParcelUuid uuid, BluetoothDevice btDevice)
{
Log.d(TAG, "Calling btDevice.getGattServices");
return btDevice.getGattServices(uuid.getUuid());
}
Parameters
Device: Device address of the GATT server being connected to.
Uuid: UUID of the primary service to which your profile is connecting.
Then you get the broadcast with the action BluetoothDevice.ACTION_GATT and then call the function
getBluetoothGattService(selectedServiceObjPath, uuid);
private void getBluetoothGattService(String objPath, ParcelUuid uuid)
{
if (mDevice != null)
{
BluetoothGattService gattService = new BluetoothGattService(mDevice,uid,objPath,btGattCallback);
if (gattService != null)
{
uuidGattSrvMap.put(uuid, gattService);
return;
}
else
{
Log.e(TAG, "Gatt service is null for UUID");
}
}
else
{
Log.e(TAG, mDevice is null");
}
mLeState = DISCONNECTED;
Toast.makeText(mContext,"Connection Failed", Toast.LENGTH_SHORT).show();
}
Parameters in BluetoothGattService
device: device address of the GATT server being connected to.
Uuid : UUID of the primary service to which your profile is connecting.
Callback: IBluetoothGattProfile.Stub objects that receives user data from the connected service.
Methods used by Bluetooth LE profiles
i) How to read the value of a service
gattService.updateCharacteristicValue(objPath)
objPath is a mapped path for the service you want to read.
ii) How to write a value
gattService.writeCharacteristicRaw(objPath, data, true);
objPath is a mapped path for the service you want to write.
Data is a byte array which you want to write
iii) How to disconnect
gattService.close();
NOTE: You can find the sample inside the Motorola
Add-ons. The above mentioned API is still in development mode and you
might come across with some development issues (bugs) as it is a BETA
Release.
相关文章推荐
- How to Choose a Bluetooth Smart / Low Energy Development Kit
- What is EPOLL? EPOLL vs Select call? And How to implement UDP server in Linux using EPOLL?
- How to implement an ArrayList structure in Java - Tutorial
- Yii 2.0: How to implement cron in Yii 2
- Interview Series: How To Implement Singleton In Concurrent Environment?
- How to implement AOP in .net
- How to Implement an Automatic Sliding Window in a Partitioned Table on SQL Server 2005
- How to implement switch statement in python
- 低功耗蓝牙Bluetooth Low Energy(BLE)
- How to Implement Ajax in WordPress Themes
- How To Use GSM Compression in Low-level Wave Recording.
- How to implement inheritance in JavaScript
- How can I connect Unity to an SQL database in order to implement an MMO?
- Android5.0(Lollipop) 蓝牙BLE(Bluetooth Low Energy)
- How to implement growable memory-mapped files (GMMFs) in NTFS
- How To Implement IObjectSafety in Visual Basic Controls
- What is EPOLL? Epoll vs Poll vs Select call ? And How to implement UDP server in Linux using EPOLL?
- how to implement C# ref in Java
- How to define a template class in a .h file and implement it in a .cpp file
- How to implement log function in daemon system