Skip to content

Commit

Permalink
Add first code snippets for bluetoothle
Browse files Browse the repository at this point in the history
  • Loading branch information
calren committed Aug 4, 2020
1 parent 3985af9 commit 499efb1
Show file tree
Hide file tree
Showing 23 changed files with 429 additions and 106 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@
/build
/captures
.externalNativeBuild
.idea/*
/.idea/*
134 changes: 109 additions & 25 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 0 additions & 19 deletions .idea/gradle.xml

This file was deleted.

36 changes: 0 additions & 36 deletions .idea/misc.xml

This file was deleted.

12 changes: 0 additions & 12 deletions .idea/runConfigurations.xml

This file was deleted.

6 changes: 0 additions & 6 deletions .idea/vcs.xml

This file was deleted.

1 change: 1 addition & 0 deletions bluetoothle/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
36 changes: 36 additions & 0 deletions bluetoothle/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

android {
compileSdkVersion 29
buildToolsVersion "29.0.3"

defaultConfig {
minSdkVersion 23
targetSdkVersion 29
versionCode 1
versionName "1.0"

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
consumerProguardFiles 'consumer-rules.pro'
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}

}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
Empty file added bluetoothle/consumer-rules.pro
Empty file.
21 changes: 21 additions & 0 deletions bluetoothle/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
26 changes: 26 additions & 0 deletions bluetoothle/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sample.android.bluetoothle">

<application>
<activity android:name=".kotlin.MainActivity"></activity>
<activity android:name=".java.DeviceScanActivity" />
<activity android:name=".java.MainActivity" />
</application>
<!--
If your app targets Android 9 or lower, you can declare
ACCESS_COARSE_LOCATION instead.
-->
<uses-feature
android:name="android.hardware.bluetooth_le"
android:required="true" />
<!--
This declares that this app is only available for devices that support Bluetooth Low
Energy.
-->
<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,57 @@
package com.sample.android.bluetoothle.java;

import android.app.ListActivity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.le.BluetoothLeScanner;
import android.bluetooth.le.ScanCallback;
import android.bluetooth.le.ScanResult;
import android.os.Handler;

/**
* Activity for scanning and displaying available BLE devices.
*/
public class DeviceScanActivity extends ListActivity {
// [START process_scan_result]
private LeDeviceListAdapter leDeviceListAdapter;

// Device scan callback.
private ScanCallback leScanCallback =
new ScanCallback() {
@Override
public void onScanResult(int callbackType, ScanResult result) {
super.onScanResult(callbackType, result);
leDeviceListAdapter.addDevice(result.getDevice());
leDeviceListAdapter.notifyDataSetChanged();
}
};
// [END process_scan_result]

// [START start_and_stop_scan]
private BluetoothLeScanner bluetoothLeScanner =
BluetoothAdapter.getDefaultAdapter().getBluetoothLeScanner();
private boolean mScanning;
private Handler handler = new Handler();

// Stops scanning after 10 seconds.
private static final long SCAN_PERIOD = 10000;

private void scanLeDevice() {
if (!mScanning) {
// Stops scanning after a pre-defined scan period.
handler.postDelayed(new Runnable() {
@Override
public void run() {
mScanning = false;
bluetoothLeScanner.stopScan(leScanCallback);
}
}, SCAN_PERIOD);

mScanning = true;
bluetoothLeScanner.startScan(leScanCallback);
} else {
mScanning = false;
bluetoothLeScanner.stopScan(leScanCallback);
}
}
// [END start_and_stop_scan]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.sample.android.bluetoothle.java;

import android.bluetooth.BluetoothClass;
import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.widget.ArrayAdapter;

public class LeDeviceListAdapter extends ArrayAdapter<BluetoothClass.Device> {

public LeDeviceListAdapter(Context context, int layout) {
super(context, layout);
}

public void addDevice(BluetoothDevice device) {
// This is where you can add devices to the adapter to show a list of discovered
// devices in the UI.
}
}
Loading

0 comments on commit 499efb1

Please sign in to comment.