Skip to content

Commit

Permalink
Simplifying API. Added protobuf to offer some data structure across p…
Browse files Browse the repository at this point in the history
…latform. Scanning reimplemented for Android.
  • Loading branch information
pauldemarco committed Aug 30, 2017
1 parent 480b3fc commit a4f7d1b
Show file tree
Hide file tree
Showing 45 changed files with 798 additions and 225 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ A new flutter plugin project.
For help getting started with Flutter, view our online
[documentation](http://flutter.io/).

For help on editing plugin code, view the [documentation](https://flutter.io/platform-plugins/#edit-code).
For help on editing plugin code, view the [documentation](https://flutter.io/platform-plugins/#edit-code).
58 changes: 48 additions & 10 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,44 +4,82 @@ version '1.0-SNAPSHOT'
buildscript {
repositories {
jcenter()
maven {
url "https://maven.google.com"
}
}

dependencies {
classpath 'com.android.tools.build:gradle:2.3.0'
classpath 'me.tatarka:gradle-retrolambda:3.6.1'
classpath 'com.android.tools.build:gradle:2.3.3'
classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.3'
}
}

allprojects {
rootProject.allprojects {
repositories {
jcenter()
maven {
url "https://maven.google.com"
}
}
}

apply plugin: 'com.android.library'
apply plugin: 'me.tatarka.retrolambda'
apply plugin: 'com.google.protobuf'

android {
compileSdkVersion 25
buildToolsVersion '25.0.3'

defaultConfig {
minSdkVersion 18
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
lintOptions {
disable 'InvalidPackage'
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
dependencies {
// Required for local unit tests (JUnit 4 framework)
testCompile 'junit:junit:4.12'

compile "com.polidea.rxandroidble:rxandroidble:1.3.3"
//compile files('/home/paul/flutter/bin/cache/artifacts/engine/android-arm/flutter.jar')
}
sourceSets {
main {
proto {
srcDir '../protos'
}
java {
exclude '**/old/'
}
}
}
}

protobuf {
// Configure the protoc executable
protoc {
// Download from repositories
artifact = 'com.google.protobuf:protoc:3.0.0'
}
plugins {
javalite {
// The codegen for lite comes as a separate artifact
artifact = 'com.google.protobuf:protoc-gen-javalite:3.0.0'
}
}
generateProtoTasks {
all().each { task ->
task.plugins {
javalite { }
}
}
}
}

dependencies {
compile 'com.google.protobuf:protobuf-lite:3.0.0'
compile group: 'com.google.guava', name: 'guava', version: '22.0-android'
}
8 changes: 3 additions & 5 deletions android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.pauldemarco.flutterblue"
android:versionCode="1"
android:versionName="0.0.1">

<uses-sdk android:minSdkVersion="19" android:targetSdkVersion="21" />
package="com.pauldemarco.flutterblue">
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
// Copyright 2017, the Flutter project authors. All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following
// disclaimer in the documentation and/or other materials provided
// with the distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

package com.pauldemarco.flutterblue;

import com.google.protobuf.ByteString;
import com.pauldemarco.flutterblue.Protos.AdvertisementData;

import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.UUID;

/**
* Parser of Bluetooth Advertisement packets.
*/
class AdvertisementParser {

/**
* Parses packet data into {@link AdvertisementData} structure.
*
* @param rawData The scan record data.
* @return An AdvertisementData proto object.
* @throws ArrayIndexOutOfBoundsException if the input is truncated.
*/
static AdvertisementData parse(byte[] rawData) {
ByteBuffer data = ByteBuffer.wrap(rawData).asReadOnlyBuffer().order(ByteOrder.LITTLE_ENDIAN);
AdvertisementData.Builder ret = AdvertisementData.newBuilder();
boolean seenLongLocalName = false;
do {
int length = data.get() & 0xFF;
if (length == 0) {
break;
}
if (length > data.remaining()) {
throw new ArrayIndexOutOfBoundsException("Not enough data.");
}

int type = data.get() & 0xFF;
length--;

switch (type) {
case 0x08: // Short local name.
case 0x09: { // Long local name.
if (seenLongLocalName) {
// Prefer the long name over the short.
break;
}
byte[] name = new byte[length];
data.get(name);
try {
ret.setLocalName(new String(name, "UTF-8"));
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
if (type == 0x09) {
seenLongLocalName = true;
}
break;
}
case 0x0A: { // Power level.
ret.setTxPowerLevel(data.get());
break;
}
case 0x16: // Service Data with 16 bit UUID.
case 0x20: // Service Data with 32 bit UUID.
case 0x21: { // Service Data with 128 bit UUID.
UUID uuid;
int remainingDataLength = 0;
if (type == 0x16 || type == 0x20) {
long uuidValue;
if (type == 0x16) {
uuidValue = data.getShort() & 0xFFFF;
remainingDataLength = length - 2;
} else {
uuidValue = data.getInt() & 0xFFFFFFFF;
remainingDataLength = length - 4;
}
uuid = UUID.fromString(String.format("%08x-0000-1000-8000-00805f9b34fb", uuidValue));
} else {
long msb = data.getLong();
long lsb = data.getLong();
uuid = new UUID(msb, lsb);
remainingDataLength = length - 16;
}
byte[] remainingData = new byte[remainingDataLength];
data.get(remainingData);
ret.putServiceData(uuid.toString(), ByteString.copyFrom(remainingData));
break;
}
case 0xFF: {// Manufacturer specific data.
byte[] msd = new byte[length];
data.get(msd);
ret.setManufacturerData(ByteString.copyFrom(msd));
break;
}
default: {
data.position(data.position() + length);
break;
}
}
} while (true);
return ret.build();
}
}
Loading

0 comments on commit a4f7d1b

Please sign in to comment.