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.

  • 相关阅读:
    ycsb
    Tikv docker-compose go client
    Raft 协议
    kubectl 命令
    JAVA判断是否是微信内置浏览器,是否是在微信内打开
    IDEA设置默认maven配置
    JAVA中JDK1.8的LocalDateTime日期类的操作方法
    JAVA在JDK1.8中Stream流的使用
    Linux(Centos)部署Jenkins
    Linux(Centos)安装maven
  • 原文地址:https://www.cnblogs.com/savagemorgan/p/3716864.html
Copyright © 2011-2022 走看看