This repository has been archived by the owner on Sep 19, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 4686c69
Showing
42 changed files
with
1,845 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
|
||
# GOOGLE SAMPLE PACKAGING DATA | ||
# | ||
# This file is used by Google as part of our samples packaging process. | ||
# End users may safely ignore this file. It has no relevance to other systems. | ||
--- | ||
status: DRAFT | ||
technologies: [Android] | ||
categories: [Connectivity] | ||
languages: [Java] | ||
solutions: [Mobile] | ||
github: android-BluetoothAdvertisements | ||
level: ADVANCED | ||
icon: screenshots/icon-web.png | ||
apiRefs: | ||
- android:android.bluetooth.le.BluetoothLeAdvertiser | ||
- android:android.bluetooth.le.BluetoothLeScanner | ||
license: apache2 |
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,73 @@ | ||
|
||
buildscript { | ||
repositories { | ||
jcenter() | ||
} | ||
|
||
dependencies { | ||
classpath 'com.android.tools.build:gradle:1.1.0' | ||
} | ||
} | ||
|
||
apply plugin: 'com.android.application' | ||
|
||
repositories { | ||
jcenter() | ||
} | ||
|
||
dependencies { | ||
compile "com.android.support:support-v4:21.0.2" | ||
compile "com.android.support:support-v13:21.0.2" | ||
compile "com.android.support:cardview-v7:21.0.2" | ||
} | ||
|
||
// The sample build uses multiple directories to | ||
// keep boilerplate and common code separate from | ||
// the main sample code. | ||
List<String> dirs = [ | ||
'main', // main sample code; look here for the interesting stuff. | ||
'common', // components that are reused by multiple samples | ||
'template'] // boilerplate code that is generated by the sample template process | ||
|
||
android { | ||
compileSdkVersion 22 | ||
buildToolsVersion "22.0.1" | ||
|
||
defaultConfig { | ||
minSdkVersion 21 | ||
targetSdkVersion 22 | ||
} | ||
|
||
compileOptions { | ||
sourceCompatibility JavaVersion.VERSION_1_7 | ||
targetCompatibility JavaVersion.VERSION_1_7 | ||
} | ||
|
||
sourceSets { | ||
main { | ||
dirs.each { dir -> | ||
java.srcDirs "src/${dir}/java" | ||
res.srcDirs "src/${dir}/res" | ||
} | ||
} | ||
androidTest.setRoot('tests') | ||
androidTest.java.srcDirs = ['tests/src'] | ||
|
||
} | ||
|
||
} | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
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,39 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
Copyright 2013 The Android Open Source Project | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
--> | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="com.example.android.bluetoothadvertisements" | ||
android:versionCode="1" | ||
android:versionName="1.0"> | ||
|
||
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/> | ||
<uses-permission android:name="android.permission.BLUETOOTH"/> | ||
|
||
<application android:allowBackup="true" | ||
android:label="@string/app_name" | ||
android:icon="@drawable/ic_launcher" | ||
android:theme="@style/AppTheme"> | ||
|
||
<activity android:name=".MainActivity" | ||
android:label="@string/app_name"> | ||
<intent-filter> | ||
<action android:name="android.intent.action.MAIN" /> | ||
<category android:name="android.intent.category.LAUNCHER" /> | ||
</intent-filter> | ||
</activity> | ||
</application> | ||
|
||
</manifest> |
190 changes: 190 additions & 0 deletions
190
...ication/src/main/java/com/example/android/bluetoothadvertisements/AdvertiserFragment.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,190 @@ | ||
package com.example.android.bluetoothadvertisements; | ||
|
||
import android.bluetooth.BluetoothAdapter; | ||
import android.bluetooth.le.AdvertiseCallback; | ||
import android.bluetooth.le.AdvertiseData; | ||
import android.bluetooth.le.AdvertiseSettings; | ||
import android.bluetooth.le.BluetoothLeAdvertiser; | ||
import android.os.Bundle; | ||
import android.support.v4.app.Fragment; | ||
import android.view.LayoutInflater; | ||
import android.view.View; | ||
import android.view.ViewGroup; | ||
import android.widget.Switch; | ||
import android.widget.Toast; | ||
|
||
/** | ||
* Allows user to start & stop Bluetooth LE Advertising of their device. | ||
*/ | ||
public class AdvertiserFragment extends Fragment { | ||
|
||
private BluetoothAdapter mBluetoothAdapter; | ||
|
||
private BluetoothLeAdvertiser mBluetoothLeAdvertiser; | ||
|
||
private AdvertiseCallback mAdvertiseCallback; | ||
|
||
private Switch mSwitch; | ||
|
||
/** | ||
* Must be called after object creation by MainActivity. | ||
* | ||
* @param btAdapter the local BluetoothAdapter | ||
*/ | ||
public void setBluetoothAdapter(BluetoothAdapter btAdapter) { | ||
this.mBluetoothAdapter = btAdapter; | ||
mBluetoothLeAdvertiser = mBluetoothAdapter.getBluetoothLeAdvertiser(); | ||
} | ||
|
||
@Override | ||
public void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
setRetainInstance(true); | ||
} | ||
|
||
@Override | ||
public View onCreateView(LayoutInflater inflater, ViewGroup container, | ||
Bundle savedInstanceState) { | ||
|
||
View view = inflater.inflate(R.layout.fragment_advertiser, container, false); | ||
|
||
mSwitch = (Switch) view.findViewById(R.id.advertise_switch); | ||
mSwitch.setOnClickListener(new View.OnClickListener() { | ||
@Override | ||
public void onClick(View v) { | ||
onSwitchClicked(v); | ||
} | ||
}); | ||
|
||
return view; | ||
} | ||
|
||
@Override | ||
public void onStop() { | ||
super.onStop(); | ||
|
||
if(mAdvertiseCallback != null){ | ||
stopAdvertising(); | ||
} | ||
} | ||
|
||
/** | ||
* Called when switch is toggled - starts or stops advertising. | ||
* | ||
* @param view is the Switch View object | ||
*/ | ||
public void onSwitchClicked(View view) { | ||
|
||
// Is the toggle on? | ||
boolean on = ((Switch) view).isChecked(); | ||
|
||
if (on) { | ||
startAdvertising(); | ||
} else { | ||
stopAdvertising(); | ||
} | ||
} | ||
|
||
/** | ||
* Starts BLE Advertising. | ||
*/ | ||
private void startAdvertising() { | ||
|
||
mAdvertiseCallback = new SampleAdvertiseCallback(); | ||
|
||
if (mBluetoothLeAdvertiser != null) { | ||
mBluetoothLeAdvertiser.startAdvertising(buildAdvertiseSettings(), buildAdvertiseData(), | ||
mAdvertiseCallback); | ||
} else { | ||
mSwitch.setChecked(false); | ||
Toast.makeText(getActivity(), getString(R.string.bt_null), Toast.LENGTH_LONG).show(); | ||
} | ||
} | ||
|
||
/** | ||
* Stops BLE Advertising. | ||
*/ | ||
private void stopAdvertising() { | ||
|
||
if (mBluetoothLeAdvertiser != null) { | ||
|
||
mBluetoothLeAdvertiser.stopAdvertising(mAdvertiseCallback); | ||
mAdvertiseCallback = null; | ||
|
||
} else { | ||
mSwitch.setChecked(false); | ||
Toast.makeText(getActivity(), getString(R.string.bt_null), Toast.LENGTH_LONG).show(); | ||
} | ||
} | ||
|
||
/** | ||
* Returns an AdvertiseData object which includes the Service UUID and Device Name. | ||
*/ | ||
private AdvertiseData buildAdvertiseData() { | ||
|
||
// Note: There is a strict limit of 31 Bytes on packets sent over BLE Advertisements. | ||
// This includes everything put into AdvertiseData including UUIDs, device info, & | ||
// arbitrary service or manufacturer data. | ||
// Attempting to send packets over this limit will result in a failure with error code | ||
// AdvertiseCallback.ADVERTISE_FAILED_DATA_TOO_LARGE. Catch this error in the | ||
// onStartFailure() method of an AdvertiseCallback implementation. | ||
|
||
AdvertiseData.Builder dataBuilder = new AdvertiseData.Builder(); | ||
dataBuilder.addServiceUuid(Constants.Service_UUID); | ||
dataBuilder.setIncludeDeviceName(true); | ||
|
||
return dataBuilder.build(); | ||
} | ||
|
||
/** | ||
* Returns an AdvertiseSettings object set to use low power (to help preserve battery life). | ||
*/ | ||
private AdvertiseSettings buildAdvertiseSettings() { | ||
AdvertiseSettings.Builder settingsBuilder = new AdvertiseSettings.Builder(); | ||
settingsBuilder.setAdvertiseMode(AdvertiseSettings.ADVERTISE_MODE_LOW_POWER); | ||
|
||
return settingsBuilder.build(); | ||
} | ||
|
||
/** | ||
* Custom callback after Advertising succeeds or fails to start. | ||
*/ | ||
private class SampleAdvertiseCallback extends AdvertiseCallback { | ||
|
||
@Override | ||
public void onStartFailure(int errorCode) { | ||
super.onStartFailure(errorCode); | ||
|
||
mSwitch.setChecked(false); | ||
|
||
String errorMessage = getString(R.string.start_error_prefix); | ||
switch (errorCode) { | ||
case AdvertiseCallback.ADVERTISE_FAILED_ALREADY_STARTED: | ||
errorMessage += " " + getString(R.string.start_error_already_started); | ||
break; | ||
case AdvertiseCallback.ADVERTISE_FAILED_DATA_TOO_LARGE: | ||
errorMessage += " " + getString(R.string.start_error_too_large); | ||
break; | ||
case AdvertiseCallback.ADVERTISE_FAILED_FEATURE_UNSUPPORTED: | ||
errorMessage += " " + getString(R.string.start_error_unsupported); | ||
break; | ||
case AdvertiseCallback.ADVERTISE_FAILED_INTERNAL_ERROR: | ||
errorMessage += " " + getString(R.string.start_error_internal); | ||
break; | ||
case AdvertiseCallback.ADVERTISE_FAILED_TOO_MANY_ADVERTISERS: | ||
errorMessage += " " + getString(R.string.start_error_too_many); | ||
break; | ||
} | ||
|
||
Toast.makeText(getActivity(), errorMessage, Toast.LENGTH_LONG).show(); | ||
|
||
} | ||
|
||
@Override | ||
public void onStartSuccess(AdvertiseSettings settingsInEffect) { | ||
super.onStartSuccess(settingsInEffect); | ||
// Don't need to do anything here, advertising successfully started. | ||
} | ||
} | ||
|
||
} |
22 changes: 22 additions & 0 deletions
22
Application/src/main/java/com/example/android/bluetoothadvertisements/Constants.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,22 @@ | ||
package com.example.android.bluetoothadvertisements; | ||
|
||
import android.os.ParcelUuid; | ||
|
||
/** | ||
* Constants for use in the Bluetooth Advertisements sample | ||
*/ | ||
public class Constants { | ||
|
||
/** | ||
* UUID identified with this app - set as Service UUID for BLE Advertisements. | ||
* | ||
* Bluetooth requires a certain format for UUIDs associated with Services. | ||
* The official specification can be found here: | ||
* {@link https://www.bluetooth.org/en-us/specification/assigned-numbers/service-discovery} | ||
*/ | ||
public static final ParcelUuid Service_UUID = ParcelUuid | ||
.fromString("0000b81d-0000-1000-8000-00805f9b34fb"); | ||
|
||
public static final int REQUEST_ENABLE_BT = 1; | ||
|
||
} |
Oops, something went wrong.