Skip to content

Commit 499efb1

Browse files
committed
Add first code snippets for bluetoothle
1 parent 3985af9 commit 499efb1

23 files changed

+429
-106
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,5 @@
99
/build
1010
/captures
1111
.externalNativeBuild
12+
.idea/*
13+
/.idea/*

.idea/codeStyles/Project.xml

+109-25
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/gradle.xml

-19
This file was deleted.

.idea/misc.xml

-36
This file was deleted.

.idea/runConfigurations.xml

-12
This file was deleted.

.idea/vcs.xml

-6
This file was deleted.

bluetoothle/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

bluetoothle/build.gradle

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
apply plugin: 'com.android.library'
2+
apply plugin: 'kotlin-android'
3+
apply plugin: 'kotlin-android-extensions'
4+
5+
android {
6+
compileSdkVersion 29
7+
buildToolsVersion "29.0.3"
8+
9+
defaultConfig {
10+
minSdkVersion 23
11+
targetSdkVersion 29
12+
versionCode 1
13+
versionName "1.0"
14+
15+
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
16+
consumerProguardFiles 'consumer-rules.pro'
17+
}
18+
19+
buildTypes {
20+
release {
21+
minifyEnabled false
22+
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
23+
}
24+
}
25+
26+
}
27+
28+
dependencies {
29+
implementation fileTree(dir: 'libs', include: ['*.jar'])
30+
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
31+
implementation 'androidx.appcompat:appcompat:1.1.0'
32+
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
33+
testImplementation 'junit:junit:4.12'
34+
androidTestImplementation 'com.android.support.test:runner:1.0.2'
35+
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
36+
}

bluetoothle/consumer-rules.pro

Whitespace-only changes.

bluetoothle/proguard-rules.pro

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Add project specific ProGuard rules here.
2+
# You can control the set of applied configuration files using the
3+
# proguardFiles setting in build.gradle.
4+
#
5+
# For more details, see
6+
# http://developer.android.com/guide/developing/tools/proguard.html
7+
8+
# If your project uses WebView with JS, uncomment the following
9+
# and specify the fully qualified class name to the JavaScript interface
10+
# class:
11+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12+
# public *;
13+
#}
14+
15+
# Uncomment this to preserve the line number information for
16+
# debugging stack traces.
17+
#-keepattributes SourceFile,LineNumberTable
18+
19+
# If you keep the line number information, uncomment this to
20+
# hide the original source file name.
21+
#-renamesourcefileattribute SourceFile
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="com.sample.android.bluetoothle">
4+
5+
<application>
6+
<activity android:name=".kotlin.MainActivity"></activity>
7+
<activity android:name=".java.DeviceScanActivity" />
8+
<activity android:name=".java.MainActivity" />
9+
</application>
10+
<!--
11+
If your app targets Android 9 or lower, you can declare
12+
ACCESS_COARSE_LOCATION instead.
13+
-->
14+
<uses-feature
15+
android:name="android.hardware.bluetooth_le"
16+
android:required="true" />
17+
<!--
18+
This declares that this app is only available for devices that support Bluetooth Low
19+
Energy.
20+
-->
21+
<uses-permission android:name="android.permission.BLUETOOTH" />
22+
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
23+
24+
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
25+
26+
</manifest>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.sample.android.bluetoothle.java;
2+
3+
import android.app.ListActivity;
4+
import android.bluetooth.BluetoothAdapter;
5+
import android.bluetooth.le.BluetoothLeScanner;
6+
import android.bluetooth.le.ScanCallback;
7+
import android.bluetooth.le.ScanResult;
8+
import android.os.Handler;
9+
10+
/**
11+
* Activity for scanning and displaying available BLE devices.
12+
*/
13+
public class DeviceScanActivity extends ListActivity {
14+
// [START process_scan_result]
15+
private LeDeviceListAdapter leDeviceListAdapter;
16+
17+
// Device scan callback.
18+
private ScanCallback leScanCallback =
19+
new ScanCallback() {
20+
@Override
21+
public void onScanResult(int callbackType, ScanResult result) {
22+
super.onScanResult(callbackType, result);
23+
leDeviceListAdapter.addDevice(result.getDevice());
24+
leDeviceListAdapter.notifyDataSetChanged();
25+
}
26+
};
27+
// [END process_scan_result]
28+
29+
// [START start_and_stop_scan]
30+
private BluetoothLeScanner bluetoothLeScanner =
31+
BluetoothAdapter.getDefaultAdapter().getBluetoothLeScanner();
32+
private boolean mScanning;
33+
private Handler handler = new Handler();
34+
35+
// Stops scanning after 10 seconds.
36+
private static final long SCAN_PERIOD = 10000;
37+
38+
private void scanLeDevice() {
39+
if (!mScanning) {
40+
// Stops scanning after a pre-defined scan period.
41+
handler.postDelayed(new Runnable() {
42+
@Override
43+
public void run() {
44+
mScanning = false;
45+
bluetoothLeScanner.stopScan(leScanCallback);
46+
}
47+
}, SCAN_PERIOD);
48+
49+
mScanning = true;
50+
bluetoothLeScanner.startScan(leScanCallback);
51+
} else {
52+
mScanning = false;
53+
bluetoothLeScanner.stopScan(leScanCallback);
54+
}
55+
}
56+
// [END start_and_stop_scan]
57+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.sample.android.bluetoothle.java;
2+
3+
import android.bluetooth.BluetoothClass;
4+
import android.bluetooth.BluetoothDevice;
5+
import android.content.Context;
6+
import android.widget.ArrayAdapter;
7+
8+
public class LeDeviceListAdapter extends ArrayAdapter<BluetoothClass.Device> {
9+
10+
public LeDeviceListAdapter(Context context, int layout) {
11+
super(context, layout);
12+
}
13+
14+
public void addDevice(BluetoothDevice device) {
15+
// This is where you can add devices to the adapter to show a list of discovered
16+
// devices in the UI.
17+
}
18+
}

0 commit comments

Comments
 (0)