-
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 15 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
146 changes: 146 additions & 0 deletions
146
bluetoothle/src/main/java/com/sample/android/bluetoothle/java/BluetoothLeService.java
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,146 @@ | ||
| /* | ||
| * 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.java; | ||
|
|
||
| import android.Manifest; | ||
| import android.app.Service; | ||
| import androidx.annotation.RequiresPermission; | ||
|
|
||
| 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.Nullable; | ||
|
|
||
| // [START android_bluetooth_service_all_java] | ||
| // [START android_bluetooth_binder_java] | ||
| public class BluetoothLeService extends Service { | ||
|
|
||
| public static final String TAG = "BluetoothLeService"; | ||
|
|
||
| private final Binder binder = new LocalBinder(); | ||
| // [START_EXCLUDE silent] | ||
| private BluetoothAdapter bluetoothAdapter; | ||
| private BluetoothGatt bluetoothGatt; | ||
| private int connectionState; | ||
|
|
||
| // [START android_bluetooth_callback_java] | ||
| public static final String ACTION_GATT_CONNECTED = | ||
| "com.example.bluetooth.le.ACTION_GATT_CONNECTED"; | ||
| public static final String ACTION_GATT_DISCONNECTED = | ||
| "com.example.bluetooth.le.ACTION_GATT_DISCONNECTED"; | ||
|
|
||
| private static final int STATE_DISCONNECTED = 0; | ||
| private static final int STATE_CONNECTED = 2; | ||
|
|
||
| private final BluetoothGattCallback bluetoothGattCallback = new BluetoothGattCallback() { | ||
| @Override | ||
| public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { | ||
| 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 android_bluetooth_callback_java] | ||
| // [END_EXCLUDE] | ||
|
|
||
|
|
||
| @Nullable | ||
| @Override | ||
| public IBinder onBind(Intent intent) { | ||
| return binder; | ||
| } | ||
|
|
||
| public class LocalBinder extends Binder { | ||
| public BluetoothLeService getService() { | ||
| return BluetoothLeService.this; | ||
| } | ||
| } | ||
| // [START_EXCLUDE silent] | ||
|
|
||
| // [START android_bluetooth_initialize_java] | ||
| public boolean initialize() { | ||
| bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); | ||
| if (bluetoothAdapter == null) { | ||
| Log.e(TAG, "Unable to obtain a BluetoothAdapter."); | ||
| return false; | ||
| } | ||
| return true; | ||
| } | ||
| // [END android_bluetooth_initialize_java] | ||
|
|
||
| @RequiresPermission(Manifest.permission.BLUETOOTH_CONNECT) | ||
|
ithinkihaveacat marked this conversation as resolved.
Outdated
|
||
| // [START android_bluetooth_connect_java] | ||
| public boolean connect(final String address) { | ||
| if (bluetoothAdapter == null || address == null) { | ||
| Log.w(TAG, "BluetoothAdapter not initialized or unspecified address."); | ||
| return false; | ||
| } | ||
| try { | ||
| final BluetoothDevice device = bluetoothAdapter.getRemoteDevice(address); | ||
| // connect to the GATT server on the device | ||
| // [START android_bluetooth_connect_gatt_java] | ||
| bluetoothGatt = device.connectGatt(this, false, bluetoothGattCallback); | ||
| // [END android_bluetooth_connect_gatt_java] | ||
| return true; | ||
| } catch (IllegalArgumentException exception) { | ||
| Log.w(TAG, "Device not found with provided address. Unable to connect."); | ||
| return false; | ||
| } | ||
| } | ||
| // [END android_bluetooth_connect_java] | ||
|
|
||
| // [START android_bluetooth_broadcast_java] | ||
| private void broadcastUpdate(final String action) { | ||
| final Intent intent = new Intent(action); | ||
| sendBroadcast(intent); | ||
| } | ||
| // [END android_bluetooth_broadcast_java] | ||
|
|
||
| @RequiresPermission(Manifest.permission.BLUETOOTH_CONNECT) | ||
| // [START android_bluetooth_close_java] | ||
| @Override | ||
| public boolean onUnbind(Intent intent) { | ||
| close(); | ||
| return super.onUnbind(intent); | ||
| } | ||
|
|
||
| @RequiresPermission(Manifest.permission.BLUETOOTH_CONNECT) | ||
| private void close() { | ||
| if (bluetoothGatt == null) { | ||
| return; | ||
| } | ||
| bluetoothGatt.close(); | ||
| bluetoothGatt = null; | ||
| } | ||
| // [END android_bluetooth_close_java] | ||
| // [END_EXCLUDE] | ||
| } | ||
| // [END android_bluetooth_binder_java] | ||
|
|
||
| // [END android_bluetooth_service_all_java] | ||
59 changes: 59 additions & 0 deletions
59
bluetoothle/src/main/java/com/sample/android/bluetoothle/java/BluetoothLeServiceSimple.java
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,59 @@ | ||
| /* | ||
| * 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.java; | ||
|
|
||
| import android.bluetooth.BluetoothAdapter; | ||
| import android.bluetooth.BluetoothDevice; | ||
| import android.bluetooth.BluetoothGatt; | ||
| import android.bluetooth.BluetoothGattCallback; | ||
| import android.bluetooth.BluetoothProfile; | ||
| import android.util.Log; | ||
|
|
||
| public class BluetoothLeServiceSimple { | ||
| private static final String TAG = "BluetoothLeService"; | ||
| private static BluetoothAdapter bluetoothAdapter; | ||
|
|
||
| // [START android_bluetooth_connect_simple_java] | ||
| public boolean connect(final String address) { | ||
| if (bluetoothAdapter == null || address == null) { | ||
| Log.w(TAG, "BluetoothAdapter not initialized or unspecified address."); | ||
| return false; | ||
| } | ||
| try { | ||
| final BluetoothDevice device = bluetoothAdapter.getRemoteDevice(address); | ||
| } catch (IllegalArgumentException exception) { | ||
| Log.w(TAG, "Device not found with provided address."); | ||
| return false; | ||
| } | ||
| // connect to the GATT server on the device | ||
| return true; | ||
| } | ||
| // [END android_bluetooth_connect_simple_java] | ||
|
|
||
| // [START android_bluetooth_callback_simple_java] | ||
| private final BluetoothGattCallback bluetoothGattCallback = new BluetoothGattCallback() { | ||
| @Override | ||
| public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { | ||
| 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_java] | ||
| } |
132 changes: 132 additions & 0 deletions
132
bluetoothle/src/main/java/com/sample/android/bluetoothle/java/DeviceControlActivity.java
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,132 @@ | ||
| /* | ||
| * 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.java; | ||
|
|
||
| import android.content.BroadcastReceiver; | ||
| import android.content.ComponentName; | ||
| import android.content.Context; | ||
| import android.content.Intent; | ||
| import android.content.IntentFilter; | ||
| import android.content.ServiceConnection; | ||
| import android.os.Bundle; | ||
| import android.os.IBinder; | ||
| import android.util.Log; | ||
| import androidx.annotation.Nullable; | ||
| import androidx.appcompat.app.AppCompatActivity; | ||
| import android.Manifest; | ||
| import com.sample.android.bluetoothle.R; | ||
| import com.sample.android.bluetoothle.java.BluetoothLeService.LocalBinder; | ||
| import androidx.annotation.RequiresPermission; | ||
|
|
||
| // [START android_bluetooth_activity_all_java] | ||
| // [START android_bluetooth_bind_service_java] | ||
| public class DeviceControlActivity extends AppCompatActivity { | ||
|
|
||
| private static final String TAG = "DeviceControlActivity"; | ||
|
|
||
| private BluetoothLeService bluetoothService; | ||
| private String deviceAddress; | ||
| private boolean connected = false; | ||
| // [START_EXCLUDE silent] | ||
|
|
||
| // [END_EXCLUDE] | ||
| private final ServiceConnection serviceConnection = new ServiceConnection() { | ||
| @Override | ||
| @RequiresPermission(Manifest.permission.BLUETOOTH_CONNECT) | ||
| public void onServiceConnected(ComponentName name, IBinder service) { | ||
| bluetoothService = ((LocalBinder) service).getService(); | ||
| if (bluetoothService != null) { | ||
| // [START android_bluetooth_initialize_activity_java] | ||
| if (!bluetoothService.initialize()) { | ||
| Log.e(TAG, "Unable to initialize Bluetooth"); | ||
| finish(); | ||
| return; | ||
| } | ||
| // [END android_bluetooth_initialize_activity_java] | ||
| // perform device connection | ||
| // [START android_bluetooth_connect_activity_java] | ||
| bluetoothService.connect(deviceAddress); | ||
| // [END android_bluetooth_connect_activity_java] | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void onServiceDisconnected(ComponentName name) { | ||
| bluetoothService = null; | ||
| } | ||
| }; | ||
|
|
||
| @Override | ||
| protected void onCreate(@Nullable Bundle savedInstanceState) { | ||
| super.onCreate(savedInstanceState); | ||
| setContentView(R.layout.gatt_services_characteristics); | ||
|
|
||
| deviceAddress = getIntent().getStringExtra("EXTRAS_DEVICE_ADDRESS"); | ||
|
|
||
| Intent gattServiceIntent = new Intent(this, BluetoothLeService.class); | ||
| bindService(gattServiceIntent, serviceConnection, Context.BIND_AUTO_CREATE); | ||
| } | ||
| // [START_EXCLUDE silent] | ||
|
|
||
| // [START android_bluetooth_update_receiver_java] | ||
| private final BroadcastReceiver gattUpdateReceiver = new BroadcastReceiver() { | ||
| @Override | ||
| public void onReceive(Context context, Intent intent) { | ||
| final String action = intent.getAction(); | ||
| if (BluetoothLeService.ACTION_GATT_CONNECTED.equals(action)) { | ||
| connected = true; | ||
| updateConnectionState(R.string.connected); | ||
| } else if (BluetoothLeService.ACTION_GATT_DISCONNECTED.equals(action)) { | ||
| connected = false; | ||
| updateConnectionState(R.string.disconnected); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| @RequiresPermission(Manifest.permission.BLUETOOTH_CONNECT) | ||
| @Override | ||
| protected void onResume() { | ||
| super.onResume(); | ||
|
|
||
| registerReceiver(gattUpdateReceiver, makeGattUpdateIntentFilter()); | ||
| if (bluetoothService != null) { | ||
| final boolean result = bluetoothService.connect(deviceAddress); | ||
| Log.d(TAG, "Connect request result=" + result); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| protected void onPause() { | ||
| super.onPause(); | ||
| unregisterReceiver(gattUpdateReceiver); | ||
| } | ||
| private static IntentFilter makeGattUpdateIntentFilter() { | ||
|
ithinkihaveacat marked this conversation as resolved.
Outdated
|
||
| final IntentFilter intentFilter = new IntentFilter(); | ||
| intentFilter.addAction(BluetoothLeService.ACTION_GATT_CONNECTED); | ||
| intentFilter.addAction(BluetoothLeService.ACTION_GATT_DISCONNECTED); | ||
| return intentFilter; | ||
| } | ||
| // [END android_bluetooth_update_receiver_java] | ||
|
|
||
|
|
||
| private void updateConnectionState(int resourceId) { | ||
| // Placeholder implementation | ||
| } | ||
| // [END_EXCLUDE] | ||
| } | ||
| // [END android_bluetooth_bind_service_java] | ||
| // [END android_bluetooth_activity_all_java] | ||
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.