Skip to content

Commit

Permalink
Merge pull request pauldemarco#12 from pauldemarco/simplify
Browse files Browse the repository at this point in the history
Simplified plugin substantially
pauldemarco authored Sep 1, 2017
2 parents 480b3fc + 40f3c39 commit 2cc68ad
Showing 107 changed files with 3,728 additions and 5,156 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
## [0.0.1] - TODO: Add release date.
## [0.0.1] - September 1st, 2017

* TODO: Describe initial release.
* Initial Release.
4 changes: 2 additions & 2 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2017 Your Company. All rights reserved.
// Copyright 2017 Paul DeMarco. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
@@ -10,7 +10,7 @@
// 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 Your Company nor the names of its
// * Neither the name of Truckable Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
42 changes: 36 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,40 @@
# flutter_blue
<br>
<p align="center">
<img alt="FlutterBlue" src="site/flutterblue.png" />
</p>
<br><br>

A new flutter plugin project.
## Introduction

## Getting Started
FlutterBlue is a bluetooth plugin for [Flutter](http://www.flutter.io), a new mobile SDK to help developers build modern apps for iOS and Android.

For help getting started with Flutter, view our online
[documentation](http://flutter.io/).
## Cross-Platform Bluetooth LE
FlutterBlue aims to offer the most from both platforms (iOS and Android).

For help on editing plugin code, view the [documentation](https://flutter.io/platform-plugins/#edit-code).
Using the FlutterBlue instance, you can scan for and connect to nearby devices ([BluetoothDevice](#bluetoothdevice-api)).
Once connected to a device, the BluetoothDevice object can discover services ([BluetoothService](lib/src/bluetooth_service.dart)), characteristics ([BluetoothCharacteristic](lib/src/bluetooth_characteristic.dart)), and descriptors ([BluetoothDescriptor](lib/src/bluetooth_descriptor.dart)).
The BluetoothDevice object is then used to directly interact with characteristics and descriptors.

### FlutterBlue API
| | Android | iOS | Description |
| :--------------- | :----------------: | :------------------: | :-------------------------------- |
| startScan | :white_check_mark: | :white_large_square: | Starts a scan for Bluetooth Low Energy devices. |
| stopScan | :white_check_mark: | :white_large_square: | Stops a scan for Bluetooth Low Energy devices. |
| connect | :white_check_mark: | :white_large_square: | Establishes a connection to the Bluetooth Device. |
| cancelConnection | :white_check_mark: | :white_large_square: | Cancels a connection to the Bluetooth Device. |
| state | :white_check_mark: | :white_large_square: | Gets the current state of the Bluetooth Adapter. |
| onStateChanged | :white_check_mark: | :white_large_square: | Stream of state changes for the Bluetooth Adapter. |

### BluetoothDevice API
| | Android | iOS | Description |
| :-------------------------- | :------------------: | :------------------: | :-------------------------------- |
| discoverServices | :white_check_mark: | :white_large_square: | Discovers services offered by the remote device as well as their characteristics and descriptors. |
| services | :white_check_mark: | :white_large_square: | Gets a list of services. Requires that discoverServices() has completed. |
| readCharacteristic | :white_check_mark: | :white_large_square: | Retrieves the value of a specified characteristic. |
| readDescriptor | :white_check_mark: | :white_large_square: | Retrieves the value of a specified descriptor. |
| writeCharacteristic | :white_check_mark: | :white_large_square: | Writes the value of a characteristic. |
| writeDescriptor | :white_check_mark: | :white_large_square: | Writes the value of a descriptor. |
| setNotifyValue | :white_large_square: | :white_large_square: | Sets notifications or indications for the value of a specified characteristic. |
| canSendWriteWithoutResponse | :white_large_square: | :white_large_square: | Indicates whether the Bluetooth Device can send a write without response. |
| state | :white_large_square: | :white_large_square: | Gets the current state of the Bluetooth Device. |
| onStateChanged | :white_large_square: | :white_large_square: | Stream of state changes for the Bluetooth Device. |
55 changes: 45 additions & 10 deletions android/build.gradle
Original file line number Diff line number Diff line change
@@ -4,44 +4,79 @@ 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'
}
}
}
}

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>
45 changes: 0 additions & 45 deletions android/src/main/java/com/pauldemarco/flutterblue/Adapter.java

This file was deleted.

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();
}
}

This file was deleted.

25 changes: 0 additions & 25 deletions android/src/main/java/com/pauldemarco/flutterblue/BluetoothLe.java

This file was deleted.

Loading
Oops, something went wrong.

0 comments on commit 2cc68ad

Please sign in to comment.