zoukankan      html  css  js  c++  java
  • 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.

  • 相关阅读:
    clientcontainerThrift Types
    测试项目测试计划
    执行delete触发器及示例演示
    互联网平台再谈互联网平台化糗百成功案例
    问题错误功能测试报告
    方法结构Oracle查看表结构的几种方法
    内容选择android控件之Spinner(动态生成下拉内容)
    混合服务VMware混合云–IaaS三国演义?
    数据schemaAvro简介
    按钮数据测试用例
  • 原文地址:https://www.cnblogs.com/savagemorgan/p/3716864.html
Copyright © 2011-2022 走看看