-
Notifications
You must be signed in to change notification settings - Fork 379
fix(bluetoothle): fix typo in BluetoothLeService and migrate to snippets #934
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ithinkihaveacat
wants to merge
25
commits into
android:main
Choose a base branch
from
ithinkihaveacat:fix-359501045
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
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 0007881
fix(bluetoothle): add early return on initialization failure
ithinkihaveacat 58c7c55
Merge branch 'main' into fix-359501045
ithinkihaveacat 92111e2
fix(bluetoothle): Address PR review comments on GATT callback and con…
ithinkihaveacat 4d88daa
fix(bluetoothle): merge update receiver and lifecycle snippets
ithinkihaveacat f57ca90
fix(bluetoothle): migrate binder snippet
ithinkihaveacat 643d93f
fix(bluetoothle): split binder snippet to new files
ithinkihaveacat c6470a5
refactor(bluetoothle): remove unused binder tags
ithinkihaveacat fb39150
refactor(bluetoothle): remove redundant tags
ithinkihaveacat bd730c2
feat(bluetoothle): add connectGatt, expand bind
ithinkihaveacat aece89b
refactor(bluetoothle): revert split binder files
ithinkihaveacat b716a48
Merge branch 'main' into fix-359501045
ithinkihaveacat 8a6b2ba
refactor(ble): merge callback and bind tags
ithinkihaveacat 8e690ba
style(ble): spotless formatting fix
ithinkihaveacat 1667104
refactor(bluetooth): Restore class wrappers and split snippets to avo…
ithinkihaveacat 5a135dd
refactor(bluetooth): align Kotlin snippets
ithinkihaveacat a5228f8
Merge branch 'main' into fix-359501045
ithinkihaveacat 0ef0384
style: format and clean up bluetooth snippets
ithinkihaveacat 124f23b
refactor(bluetooth): remove service discovery and hide annotations
ithinkihaveacat 7a58235
refactor(bluetooth): remove unnecessary permission annotations
ithinkihaveacat 252479f
fix(bluetoothle): fix Lint MissingPermission error on gatt.close() us…
ithinkihaveacat c536cec
style(bluetoothle): fix spotless formatting (import order) in Bluetoo…
ithinkihaveacat adabe02
Merge remote-tracking branch 'upstream/main' into fix-359501045
ithinkihaveacat 416bee9
refactor(ble): namespace progressive snippets
ithinkihaveacat 9b6d0df
fix(ble): resolve Android Lint errors
ithinkihaveacat File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
331 changes: 331 additions & 0 deletions
331
bluetoothle/src/main/java/com/sample/android/bluetoothle/kotlin/BluetoothLeService.kt
This file contains hidden or 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,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 | ||
| 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 | ||
| } | ||
| } | ||
|
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) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() {} | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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] | ||
| } | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.