Android Open Source - MovisensGattSensorExample Ble Utils






From Project

Back to project page MovisensGattSensorExample.

License

The source code is released under:

GNU General Public License

If you think the Android project MovisensGattSensorExample listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.movisens.gattsensorexample.utils;
//from w w w . j av  a 2  s  .c o  m
import java.util.List;
import java.util.UUID;

import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattService;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;
import android.util.Log;

import com.movisens.gattsensorexample.services.BluetoothLeService;

public class BleUtils {

  private final static String TAG = BleUtils.class.getSimpleName();

  public static boolean hasBluetoothLE(Context context) {
    return (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2 && context
        .getPackageManager().hasSystemFeature(
            PackageManager.FEATURE_BLUETOOTH_LE));
  }

  public static void enableCharacteristicNotification(UUID enableUuid,
      String name, List<BluetoothGattCharacteristic> gattCharacteristics,
      BluetoothLeService mBluetoothLeService) {
    BluetoothGattCharacteristic gattCharacteristic = findCharacteristic(
        enableUuid, gattCharacteristics);
    if (gattCharacteristic != null) {
      Log.d(TAG, "Enable " + name);
      mBluetoothLeService.setCharacteristicNotification(
          gattCharacteristic, true);
    }
  }

  public static void readCharacteristic(UUID enableUuid, String name,
      List<BluetoothGattCharacteristic> gattCharacteristics,
      BluetoothLeService mBluetoothLeService) {
    BluetoothGattCharacteristic gattCharacteristic = findCharacteristic(
        enableUuid, gattCharacteristics);
    if (gattCharacteristic != null) {
      Log.d(TAG, "Read " + name);
      mBluetoothLeService.readCharacteristic(gattCharacteristic);
    }
  }

  public static void writeCharacteristic(UUID enableUuid, String name,
      List<BluetoothGattCharacteristic> gattCharacteristics,
      BluetoothLeService mBluetoothLeService, byte[] value) {
    BluetoothGattCharacteristic gattCharacteristic = findCharacteristic(
        enableUuid, gattCharacteristics);
    if (gattCharacteristic != null) {
      Log.d(TAG, "Write " + name);
      gattCharacteristic.setValue(value);
      gattCharacteristic
          .setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT);
      mBluetoothLeService.writeCharacteristic(gattCharacteristic);
    }
  }

  public static BluetoothGattCharacteristic findCharacteristic(UUID uuid,
      List<BluetoothGattCharacteristic> gattCharacteristics) {
    if (gattCharacteristics != null && gattCharacteristics.size() > 0) {
      for (BluetoothGattCharacteristic gattCharacteristic : gattCharacteristics) {
        if (gattCharacteristic != null) {
          if (gattCharacteristic.getUuid().equals(uuid)) {
            return gattCharacteristic;
          }
        }
      }
    }
    return null;
  }

  public static BluetoothGattService findService(UUID uuid,
      List<BluetoothGattService> gattServices) {
    if (gattServices == null)
      return null;
    for (BluetoothGattService gattService : gattServices) {
      if (uuid.equals(gattService.getUuid())) {
        return gattService;
      }
    }
    return null;
  }

  final protected static char[] hexArray = "0123456789ABCDEF".toCharArray();

  public static String bytesToHex(byte[] bytes) {
    char[] hexChars = new char[bytes.length * 2];
    for (int j = 0; j < bytes.length; j++) {
      int v = bytes[j] & 0xFF;
      hexChars[j * 2] = hexArray[v >>> 4];
      hexChars[j * 2 + 1] = hexArray[v & 0x0F];
    }
    return new String(hexChars);
  }
}




Java Source Code List

com.movisens.gattsensorexample.activities.DeviceScanActivity.java
com.movisens.gattsensorexample.activities.Preferences.java
com.movisens.gattsensorexample.activities.SensorConnected.java
com.movisens.gattsensorexample.activities.SensorDisconnected.java
com.movisens.gattsensorexample.activities.StartActivity.java
com.movisens.gattsensorexample.activities.TimePreference.java
com.movisens.gattsensorexample.application.App.java
com.movisens.gattsensorexample.events.BLEEvent.java
com.movisens.gattsensorexample.events.MeasurementStatus.java
com.movisens.gattsensorexample.events.SensorStatusEvent.java
com.movisens.gattsensorexample.model.CurrentSensorData.java
com.movisens.gattsensorexample.receivers.SystemStateReceiver.java
com.movisens.gattsensorexample.receivers.UpdateAppReceiver.java
com.movisens.gattsensorexample.sensors.MovisensSensor.java
com.movisens.gattsensorexample.services.BluetoothLeService.java
com.movisens.gattsensorexample.services.SamplingService.java
com.movisens.gattsensorexample.utils.BleQueue.java
com.movisens.gattsensorexample.utils.BleUtils.java