Android Open Source - cordova-bluetoothle J S O N Objects






From Project

Back to project page cordova-bluetoothle.

License

The source code is released under:

Apache License

If you think the Android project cordova-bluetoothle 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.mutualmobile.cordova.bluetoothle;
/*ww w  . java 2s. c  o m*/
import java.io.PrintWriter;
import java.io.StringWriter;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattDescriptor;
import android.bluetooth.BluetoothGattService;
import android.bluetooth.BluetoothManager;
import android.bluetooth.BluetoothProfile;
import android.util.Base64;
import android.util.Log;


public class JSONObjects {

  public static JSONObject asAdapter(BluetoothAdapter a) {
    JSONObject result = new JSONObject();
    try {
      result.put("address", a.getAddress());
      result.put("name", a.getName());
      result.put("enabled", a.isEnabled());
      result.put("discovering", a.isDiscovering());
    }
    catch (JSONException e) {
      Log.e("bluetoothle", "JSON Error occurred... so we can't give cordova a response in JSON.", e);
    }
    return result;
  }


  public static JSONObject asDevice(BluetoothDevice device, int rssi, byte[] ad) {
    JSONObject result = new JSONObject();
    try {
      result.put("address", device.getAddress());
      result.put("name", device.getName());
      result.put("rssi", rssi);
      JSONArray uuids = new JSONArray();
      for (UUID u : parseUuids(ad)) {
        uuids.put(u);
      }
      result.put("uuids", uuids);
    }
    catch (JSONException e) {
      Log.e("bluetoothle", "JSON Error occurred... so we can't give cordova a response in JSON.", e);
    }
    return result;
  }


  public static JSONObject asDevice(BluetoothGatt gatt, BluetoothManager manager) {
    JSONObject result = new JSONObject();
    try {
      result.put("address", gatt.getDevice().getAddress());
      result.put("name", gatt.getDevice().getName());

      JSONArray uuids = new JSONArray();
      for (BluetoothGattService service : gatt.getServices()) {
        uuids.put(service.getUuid());
      }
      result.put("uuids", uuids);

      result.put("connected", manager.getConnectionState(gatt.getDevice(), BluetoothProfile.GATT_SERVER) == BluetoothProfile.STATE_CONNECTED);
    }
    catch (JSONException e) {
      Log.e("bluetoothle", "JSON Error occurred... so we can't give cordova a response in JSON.", e);
    }
    return result;
  }


  public static JSONObject asService(BluetoothGattService s, BluetoothDevice d) {
    JSONObject result = new JSONObject();
    try {
      result.put("uuid", s.getUuid().toString());
      result.put("isPrimary", s.getType() == BluetoothGattService.SERVICE_TYPE_PRIMARY);
      result.put("instanceId", String.valueOf(s.getInstanceId()));
      result.put("deviceAddress", d.getAddress());
    }
    catch (JSONException e) {
      Log.e("bluetoothle", "JSON Error occurred... so we can't give cordova a response in JSON.", e);
    }
    return result;
  }


  public static JSONObject asCharacteristic(BluetoothGattCharacteristic c) {
    JSONObject result = new JSONObject();
    try {
      result.put("uuid", c.getUuid().toString());
      result.put("service", c.getService().getUuid().toString());

      JSONArray propArray = new JSONArray();
      int properties = c.getProperties();
      if ((properties & BluetoothGattCharacteristic.PROPERTY_BROADCAST) != 0) {
        propArray.put("broadcast");
      }
      if ((properties & BluetoothGattCharacteristic.PROPERTY_EXTENDED_PROPS) != 0) {
        propArray.put("extendedProperties");
      }
      if ((properties & BluetoothGattCharacteristic.PROPERTY_INDICATE) != 0) {
        propArray.put("indicate");
      }
      if ((properties & BluetoothGattCharacteristic.PROPERTY_NOTIFY) != 0) {
        propArray.put("notify");
      }
      if ((properties & BluetoothGattCharacteristic.PROPERTY_READ) != 0) {
        propArray.put("read");
      }
      if ((properties & BluetoothGattCharacteristic.PROPERTY_SIGNED_WRITE) != 0) {
        propArray.put("authenticatedSignedWrites");
      }
      if ((properties & BluetoothGattCharacteristic.PROPERTY_WRITE) != 0) {
        propArray.put("write");
      }
      if ((properties & BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE) != 0) {
        propArray.put("writeWithoutResponse");
      }
      result.put("properties", propArray);

      result.put("instanceId", c.getInstanceId());
      byte[] value = c.getValue();
      if (value == null) {
        result.put("value", null);
      }
      else {
        result.put("value", Base64.encodeToString(c.getValue(), Base64.NO_WRAP));
      }
    }
    catch (JSONException e) {
      Log.e("bluetoothle", "JSON Error occurred... so we can't give cordova a response in JSON.", e);
    }
    return result;
  }


  public static JSONObject asDescriptor(BluetoothGattDescriptor d) {
    JSONObject result = new JSONObject();
    try {
      result.put("uuid", d.getUuid().toString());
      result.put("characteristic", d.getCharacteristic().getUuid().toString());
      result.put("value", Base64.encodeToString(d.getValue(), Base64.NO_WRAP));
    }
    catch (JSONException e) {
      Log.e("bluetoothle", "JSON Error occurred... so we can't give cordova a response in JSON.", e);
    }
    return result;
  }


  public static JSONObject asError(Exception e) {
    JSONObject result = new JSONObject();
    try {
      result.put("message", e.getMessage());

      StringWriter sw = new StringWriter();
      e.printStackTrace(new PrintWriter(sw));
      result.put("stack", sw.toString());
    }
    catch (JSONException other) {
      Log.e("bluetoothle", "JSON Error occurred... so we can't give cordova a response in JSON.", other);
    }
    return result;
  }


  // http://stackoverflow.com/questions/18019161/startlescan-with-128-bit-uuids-doesnt-work-on-native-android-ble-implementation?noredirect=1#comment27879874_18019161
  private static List<UUID> parseUuids(byte[] advertisedData) {
    List<UUID> uuids = new ArrayList<UUID>();

    ByteBuffer buffer = ByteBuffer.wrap(advertisedData).order(ByteOrder.LITTLE_ENDIAN);
    while (buffer.remaining() > 2) {
      byte length = buffer.get();
      if (length == 0) break;

      byte type = buffer.get();
      switch (type) {
        case 0x02: // Partial list of 16-bit UUIDs
        case 0x03: // Complete list of 16-bit UUIDs
          while (length >= 2) {
            uuids.add(UUID.fromString(String.format(
                    "%08x-0000-1000-8000-00805f9b34fb", buffer.getShort())));
            length -= 2;
          }
          break;

        case 0x06: // Partial list of 128-bit UUIDs
        case 0x07: // Complete list of 128-bit UUIDs
          while (length >= 16) {
            long lsb = buffer.getLong();
            long msb = buffer.getLong();
            uuids.add(new UUID(msb, lsb));
            length -= 16;
          }
          break;

        default:
          buffer.position(buffer.position() + length - 1);
          break;
      }
    }

    return uuids;
  }


}




Java Source Code List

com.mutualmobile.cordova.BluetoothleTests.java
com.mutualmobile.cordova.bluetoothle.BluetoothLePlugin.java
com.mutualmobile.cordova.bluetoothle.JSONObjects.java
com.squareup.okhttp.Address.java
com.squareup.okhttp.ConnectionPool.java
com.squareup.okhttp.Connection.java
com.squareup.okhttp.Dispatcher.java
com.squareup.okhttp.Failure.java
com.squareup.okhttp.HttpResponseCache.java
com.squareup.okhttp.Job.java
com.squareup.okhttp.MediaType.java
com.squareup.okhttp.OkAuthenticator.java
com.squareup.okhttp.OkHttpClient.java
com.squareup.okhttp.OkResponseCache.java
com.squareup.okhttp.Request.java
com.squareup.okhttp.ResponseSource.java
com.squareup.okhttp.Response.java
com.squareup.okhttp.RouteDatabase.java
com.squareup.okhttp.Route.java
com.squareup.okhttp.TunnelRequest.java
com.squareup.okhttp.internal.AbstractOutputStream.java
com.squareup.okhttp.internal.Base64.java
com.squareup.okhttp.internal.DiskLruCache.java
com.squareup.okhttp.internal.Dns.java
com.squareup.okhttp.internal.FaultRecoveringOutputStream.java
com.squareup.okhttp.internal.NamedRunnable.java
com.squareup.okhttp.internal.Platform.java
com.squareup.okhttp.internal.StrictLineReader.java
com.squareup.okhttp.internal.Util.java
com.squareup.okhttp.internal.http.AbstractHttpInputStream.java
com.squareup.okhttp.internal.http.HeaderParser.java
com.squareup.okhttp.internal.http.HttpAuthenticator.java
com.squareup.okhttp.internal.http.HttpDate.java
com.squareup.okhttp.internal.http.HttpEngine.java
com.squareup.okhttp.internal.http.HttpTransport.java
com.squareup.okhttp.internal.http.HttpURLConnectionImpl.java
com.squareup.okhttp.internal.http.HttpsEngine.java
com.squareup.okhttp.internal.http.HttpsURLConnectionImpl.java
com.squareup.okhttp.internal.http.OkResponseCacheAdapter.java
com.squareup.okhttp.internal.http.Policy.java
com.squareup.okhttp.internal.http.RawHeaders.java
com.squareup.okhttp.internal.http.RequestHeaders.java
com.squareup.okhttp.internal.http.ResponseHeaders.java
com.squareup.okhttp.internal.http.RetryableOutputStream.java
com.squareup.okhttp.internal.http.RouteSelector.java
com.squareup.okhttp.internal.http.SpdyTransport.java
com.squareup.okhttp.internal.http.Transport.java
com.squareup.okhttp.internal.http.UnknownLengthHttpInputStream.java
com.squareup.okhttp.internal.spdy.ErrorCode.java
com.squareup.okhttp.internal.spdy.FrameReader.java
com.squareup.okhttp.internal.spdy.FrameWriter.java
com.squareup.okhttp.internal.spdy.HeadersMode.java
com.squareup.okhttp.internal.spdy.Hpack.java
com.squareup.okhttp.internal.spdy.Http20Draft06.java
com.squareup.okhttp.internal.spdy.IncomingStreamHandler.java
com.squareup.okhttp.internal.spdy.NameValueBlockReader.java
com.squareup.okhttp.internal.spdy.Ping.java
com.squareup.okhttp.internal.spdy.Settings.java
com.squareup.okhttp.internal.spdy.Spdy3.java
com.squareup.okhttp.internal.spdy.SpdyConnection.java
com.squareup.okhttp.internal.spdy.SpdyStream.java
com.squareup.okhttp.internal.spdy.Variant.java
com.squareup.okhttp.internal.tls.DistinguishedNameParser.java
com.squareup.okhttp.internal.tls.OkHostnameVerifier.java
org.apache.cordova.App.java
org.apache.cordova.AuthenticationToken.java
org.apache.cordova.CallbackContext.java
org.apache.cordova.Config.java
org.apache.cordova.CordovaActivity.java
org.apache.cordova.CordovaArgs.java
org.apache.cordova.CordovaChromeClient.java
org.apache.cordova.CordovaInterface.java
org.apache.cordova.CordovaPlugin.java
org.apache.cordova.CordovaResourceApi.java
org.apache.cordova.CordovaUriHelper.java
org.apache.cordova.CordovaWebViewClient.java
org.apache.cordova.CordovaWebView.java
org.apache.cordova.DirectoryManager.java
org.apache.cordova.DroidGap.java
org.apache.cordova.ExifHelper.java
org.apache.cordova.ExposedJsApi.java
org.apache.cordova.FileHelper.java
org.apache.cordova.IceCreamCordovaWebViewClient.java
org.apache.cordova.JSONUtils.java
org.apache.cordova.LOG.java
org.apache.cordova.LinearLayoutSoftKeyboardDetect.java
org.apache.cordova.NativeToJsMessageQueue.java
org.apache.cordova.PluginEntry.java
org.apache.cordova.PluginManager.java
org.apache.cordova.PluginResult.java
org.apache.cordova.ScrollEvent.java
org.apache.cordova.Whitelist.java