How to Implement Bluetooth Low Energy (BLE) in Ice Cream Sandwich

ShareThis

Android Developer - 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.

posted on 2014-05-08 19:35 罗斯摩根 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/savagemorgan/p/3716864.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值