• 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.

  • 相关阅读:
    JS-记住用户名【cookie封装引申】
    JS-cookie封装
    JS-比较函数中嵌套函数,可以排序【对象数组】
    JS-随机div颜色
    JS-过滤敏感词【RegExp】
    JS-提取字符串—>>普通方法VS正则表达式
    CSS- ie6,ie7,ie8 兼容性写法,CSS hack写法
    JS-【同页面多次调用】轮播特效封装-json传多个参数
    JS-【同页面多次调用】tab选项卡封装
    Redis主从同步
  • 原文地址:https://www.cnblogs.com/savagemorgan/p/3716864.html
Copyright © 2020-2023  润新知