forked from android/snippets
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add first code snippets for bluetoothle
- Loading branch information
Showing
23 changed files
with
429 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,3 +9,5 @@ | |
/build | ||
/captures | ||
.externalNativeBuild | ||
.idea/* | ||
/.idea/* |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
57 changes: 57 additions & 0 deletions
57
bluetoothle/src/main/java/com/sample/android/bluetoothle/java/DeviceScanActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | ||
} |
18 changes: 18 additions & 0 deletions
18
bluetoothle/src/main/java/com/sample/android/bluetoothle/java/LeDeviceListAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
} | ||
} |
Oops, something went wrong.