Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
51240a7
fix(bluetoothle): fix typo in BluetoothLeService and migrate to snippets
ithinkihaveacat Jun 1, 2026
0007881
fix(bluetoothle): add early return on initialization failure
ithinkihaveacat Jun 3, 2026
58c7c55
Merge branch 'main' into fix-359501045
ithinkihaveacat Jun 3, 2026
92111e2
fix(bluetoothle): Address PR review comments on GATT callback and con…
ithinkihaveacat Jun 5, 2026
4d88daa
fix(bluetoothle): merge update receiver and lifecycle snippets
ithinkihaveacat Jun 5, 2026
f57ca90
fix(bluetoothle): migrate binder snippet
ithinkihaveacat Jun 5, 2026
643d93f
fix(bluetoothle): split binder snippet to new files
ithinkihaveacat Jun 5, 2026
c6470a5
refactor(bluetoothle): remove unused binder tags
ithinkihaveacat Jun 5, 2026
fb39150
refactor(bluetoothle): remove redundant tags
ithinkihaveacat Jun 5, 2026
bd730c2
feat(bluetoothle): add connectGatt, expand bind
ithinkihaveacat Jun 5, 2026
aece89b
refactor(bluetoothle): revert split binder files
ithinkihaveacat Jun 6, 2026
b716a48
Merge branch 'main' into fix-359501045
ithinkihaveacat Jun 6, 2026
8a6b2ba
refactor(ble): merge callback and bind tags
ithinkihaveacat Jun 6, 2026
8e690ba
style(ble): spotless formatting fix
ithinkihaveacat Jun 6, 2026
1667104
refactor(bluetooth): Restore class wrappers and split snippets to avo…
ithinkihaveacat Jun 8, 2026
5a135dd
refactor(bluetooth): align Kotlin snippets
ithinkihaveacat Jun 10, 2026
a5228f8
Merge branch 'main' into fix-359501045
ithinkihaveacat Jun 10, 2026
0ef0384
style: format and clean up bluetooth snippets
ithinkihaveacat Jun 11, 2026
124f23b
refactor(bluetooth): remove service discovery and hide annotations
ithinkihaveacat Jun 11, 2026
7a58235
refactor(bluetooth): remove unnecessary permission annotations
ithinkihaveacat Jun 11, 2026
252479f
fix(bluetoothle): fix Lint MissingPermission error on gatt.close() us…
ithinkihaveacat Jun 11, 2026
c536cec
style(bluetoothle): fix spotless formatting (import order) in Bluetoo…
ithinkihaveacat Jun 11, 2026
adabe02
Merge remote-tracking branch 'upstream/main' into fix-359501045
ithinkihaveacat Jun 16, 2026
416bee9
refactor(ble): namespace progressive snippets
ithinkihaveacat Jun 16, 2026
9b6d0df
fix(ble): resolve Android Lint errors
ithinkihaveacat Jun 16, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,331 @@
/*
* Copyright 2026 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
*
* https://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.
*/

package com.sample.android.bluetoothle.kotlin

import android.Manifest
import android.annotation.SuppressLint
import android.app.Service
import android.bluetooth.BluetoothAdapter
import android.bluetooth.BluetoothDevice
import android.bluetooth.BluetoothGatt
import android.bluetooth.BluetoothGattCallback
import android.bluetooth.BluetoothProfile
import android.content.Intent
import android.os.Binder
import android.os.IBinder
import android.util.Log
import androidx.annotation.RequiresPermission

private const val TAG = "BluetoothLeService"

@SuppressLint("MissingPermission")
// [START android_bluetooth_service_all]
// [START android_bluetooth_binder]
class BluetoothLeService : Service() {

private val binder = LocalBinder()
// [START_EXCLUDE silent]

private var bluetoothAdapter: BluetoothAdapter? = null
private var bluetoothGatt: BluetoothGatt? = null
Comment thread
ithinkihaveacat marked this conversation as resolved.
private var connectionState = STATE_DISCONNECTED

companion object {
const val ACTION_GATT_CONNECTED =
"com.example.bluetooth.le.ACTION_GATT_CONNECTED"
const val ACTION_GATT_DISCONNECTED =
"com.example.bluetooth.le.ACTION_GATT_DISCONNECTED"

private const val STATE_DISCONNECTED = 0
private const val STATE_CONNECTED = 2
}

private val bluetoothGattCallback = object : BluetoothGattCallback() {
override fun onConnectionStateChange(gatt: BluetoothGatt?, status: Int, newState: Int) {
if (newState == BluetoothProfile.STATE_CONNECTED) {
// successfully connected to the GATT Server
connectionState = STATE_CONNECTED
broadcastUpdate(ACTION_GATT_CONNECTED)
} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
// disconnected from the GATT Server
connectionState = STATE_DISCONNECTED
broadcastUpdate(ACTION_GATT_DISCONNECTED)
}
}
}
// [END_EXCLUDE]

override fun onBind(intent: Intent): IBinder? {
return binder
}

inner class LocalBinder : Binder() {
fun getService(): BluetoothLeService {
return this@BluetoothLeService
}
}
// [START_EXCLUDE silent]

// In BluetoothLeService
fun initialize(): Boolean {
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter()
if (bluetoothAdapter == null) {
Log.e(TAG, "Unable to obtain a BluetoothAdapter.")
return false
}
return true
}

@RequiresPermission(Manifest.permission.BLUETOOTH_CONNECT)
fun connect(address: String): Boolean {
bluetoothAdapter?.let { adapter ->
try {
val device = adapter.getRemoteDevice(address)
// connect to the GATT server on the device
bluetoothGatt = device.connectGatt(this, false, bluetoothGattCallback)
return true
} catch (exception: IllegalArgumentException) {
Log.w(TAG, "Device not found with provided address. Unable to connect.")
return false
}
} ?: run {
Log.w(TAG, "BluetoothAdapter not initialized")
return false
}
}
Comment thread
ithinkihaveacat marked this conversation as resolved.

// [START android_bluetooth_broadcast]
private fun broadcastUpdate(action: String) {
val intent = Intent(action)
sendBroadcast(intent)
}
// [END android_bluetooth_broadcast]

@RequiresPermission(Manifest.permission.BLUETOOTH_CONNECT)
override fun onUnbind(intent: Intent?): Boolean {
close()
return super.onUnbind(intent)
}

private fun close() {
bluetoothGatt?.let { gatt ->
gatt.close()
bluetoothGatt = null
}
}
// [END_EXCLUDE]
}
// [END android_bluetooth_binder]

// [END android_bluetooth_service_all]

/**
* Namespaces for simplified versions of BluetoothLeService to match documentation.
*/
private object ConnectSimpleNamespace {
class BluetoothLeService {
private var bluetoothAdapter: BluetoothAdapter? = null
private val TAG = "BluetoothLeService"

// [START android_bluetooth_connect_simple]
fun connect(address: String): Boolean {
bluetoothAdapter?.let { adapter ->
try {
val device = adapter.getRemoteDevice(address)
} catch (exception: IllegalArgumentException) {
Log.w(TAG, "Device not found with provided address.")
return false
}
// connect to the GATT server on the device
return true
} ?: run {
Log.w(TAG, "BluetoothAdapter not initialized")
return false
}
}
// [END android_bluetooth_connect_simple]
}
}

private object CallbackSimpleNamespace {
class BluetoothLeService {
// [START android_bluetooth_callback_simple]
private val bluetoothGattCallback = object : BluetoothGattCallback() {
override fun onConnectionStateChange(gatt: BluetoothGatt?, status: Int, newState: Int) {
if (newState == BluetoothProfile.STATE_CONNECTED) {
// successfully connected to the GATT Server
} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
// disconnected from the GATT Server
}
}
}
// [END android_bluetooth_callback_simple]
}
}

private object InitializeNamespace {
// [START android_bluetooth_initialize]
private const val TAG = "BluetoothLeService"

class BluetoothLeService : Service() {

private var bluetoothAdapter: BluetoothAdapter? = null

fun initialize(): Boolean {
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter()
if (bluetoothAdapter == null) {
Log.e(TAG, "Unable to obtain a BluetoothAdapter.")
return false
}
return true
}

// [START_EXCLUDE]
override fun onBind(intent: Intent): IBinder? {
return null
}
// [END_EXCLUDE]
}
// [END android_bluetooth_initialize]
}

@SuppressLint("MissingPermission")
private object ConnectGattNamespace {
class BluetoothLeService : Service() {
// [START android_bluetooth_connect_gatt]
// [START_EXCLUDE silent]
fun dummy(device: BluetoothDevice, bluetoothGattCallback: BluetoothGattCallback) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: can we call it a different placeholder name?

// [END_EXCLUDE]
var bluetoothGatt: BluetoothGatt? = null
// ...
bluetoothGatt = device.connectGatt(this, false, bluetoothGattCallback)
// [START_EXCLUDE silent]
}
// [END_EXCLUDE]
// [END android_bluetooth_connect_gatt]
override fun onBind(intent: Intent): IBinder? = null
}
}

@SuppressLint("MissingPermission")
private object ConnectNamespace {
// [START android_bluetooth_connect]
class BluetoothLeService : Service() {

// [START_EXCLUDE silent]
private var bluetoothAdapter: BluetoothAdapter? = null
private val bluetoothGattCallback = object : BluetoothGattCallback() {}
private val TAG = "BluetoothLeService"
// [END_EXCLUDE]
// [START_EXCLUDE]
override fun onBind(intent: Intent): IBinder? = null
fun dummy() {}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here and check for other occurences in the files too

// [END_EXCLUDE]
private var bluetoothGatt: BluetoothGatt? = null

// [START_EXCLUDE]
fun dummy2() {}
// [END_EXCLUDE]
fun connect(address: String): Boolean {
bluetoothAdapter?.let { adapter ->
try {
val device = adapter.getRemoteDevice(address)
// connect to the GATT server on the device
bluetoothGatt = device.connectGatt(this, false, bluetoothGattCallback)
return true
} catch (exception: IllegalArgumentException) {
Log.w(TAG, "Device not found with provided address. Unable to connect.")
return false
}
} ?: run {
Log.w(TAG, "BluetoothAdapter not initialized")
return false
}
}
}
// [END android_bluetooth_connect]
}

@SuppressLint("MissingPermission")
private object CallbackNamespace {
// [START android_bluetooth_callback]
class BluetoothLeService : Service() {

// [START_EXCLUDE silent]
private val binder = LocalBinder()
private var bluetoothAdapter: BluetoothAdapter? = null
private var bluetoothGatt: BluetoothGatt? = null
private fun broadcastUpdate(action: String) {}
override fun onBind(intent: Intent): IBinder? = null
inner class LocalBinder : Binder()
// [END_EXCLUDE]
private var connectionState = STATE_DISCONNECTED

private val bluetoothGattCallback = object : BluetoothGattCallback() {
override fun onConnectionStateChange(gatt: BluetoothGatt?, status: Int, newState: Int) {
if (newState == BluetoothProfile.STATE_CONNECTED) {
// successfully connected to the GATT Server
connectionState = STATE_CONNECTED
broadcastUpdate(ACTION_GATT_CONNECTED)
} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
// disconnected from the GATT Server
connectionState = STATE_DISCONNECTED
broadcastUpdate(ACTION_GATT_DISCONNECTED)
}
}
}

// [START_EXCLUDE]
fun dummy() {}
// [END_EXCLUDE]
companion object {
const val ACTION_GATT_CONNECTED =
"com.example.bluetooth.le.ACTION_GATT_CONNECTED"
const val ACTION_GATT_DISCONNECTED =
"com.example.bluetooth.le.ACTION_GATT_DISCONNECTED"

private const val STATE_DISCONNECTED = 0
private const val STATE_CONNECTED = 2
}
}
// [END android_bluetooth_callback]
}

@SuppressLint("MissingPermission")
private object CloseNamespace {
// [START android_bluetooth_close]
class BluetoothLeService : Service() {

// [START_EXCLUDE]
private var bluetoothGatt: BluetoothGatt? = null
override fun onBind(intent: Intent): IBinder? = null
// [END_EXCLUDE]
override fun onUnbind(intent: Intent?): Boolean {
close()
return super.onUnbind(intent)
}

private fun close() {
bluetoothGatt?.let { gatt ->
gatt.close()
bluetoothGatt = null
}
}
}
// [END android_bluetooth_close]
}

Loading
Loading