Skip to content
Open
Show file tree
Hide file tree
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 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
Comment thread
ithinkihaveacat marked this conversation as resolved.
Outdated
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)
Comment thread
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]
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]
}
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() {
Comment thread
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]
Loading
Loading