1 /*
2  * Copyright 2023 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.google.snippet.bluetooth;
18 
19 import static android.bluetooth.BluetoothGattService.SERVICE_TYPE_PRIMARY;
20 
21 import android.bluetooth.BluetoothAdapter;
22 import android.bluetooth.BluetoothGattServer;
23 import android.bluetooth.BluetoothGattServerCallback;
24 import android.bluetooth.BluetoothGattService;
25 import android.bluetooth.BluetoothManager;
26 import android.bluetooth.le.AdvertiseData;
27 import android.bluetooth.le.AdvertisingSetCallback;
28 import android.bluetooth.le.AdvertisingSetParameters;
29 import android.content.Context;
30 import android.os.Handler;
31 import android.os.Looper;
32 import android.os.ParcelUuid;
33 
34 import java.util.UUID;
35 
36 public final class BluetoothGattMultiDevicesServer {
37     private static final String TAG = "BluetoothGattMultiDevicesServer";
38 
39     private Context mContext;
40     private BluetoothManager mBluetoothManager;
41     private BluetoothAdapter mBluetoothAdapter;
42 
BluetoothGattMultiDevicesServer(Context context, BluetoothManager manager)43     public BluetoothGattMultiDevicesServer(Context context, BluetoothManager manager) {
44         mContext = context;
45         mBluetoothManager = manager;
46         mBluetoothAdapter = manager.getAdapter();
47     }
48 
createGattServer(String uuid)49     public BluetoothGattServer createGattServer(String uuid) {
50         var bluetoothGattServer =
51                 mBluetoothManager.openGattServer(mContext, new BluetoothGattServerCallback() {});
52         var service = new BluetoothGattService(UUID.fromString(uuid), SERVICE_TYPE_PRIMARY);
53         bluetoothGattServer.addService(service);
54         return bluetoothGattServer;
55     }
56 
createAndAdvertiseServer(String uuid)57     public void createAndAdvertiseServer(String uuid) {
58         createGattServer(uuid);
59 
60         var bluetoothLeAdvertiser = mBluetoothAdapter.getBluetoothLeAdvertiser();
61         var params = new AdvertisingSetParameters.Builder().setConnectable(true).build();
62         var data =
63                 new AdvertiseData.Builder()
64                         .addServiceUuid(new ParcelUuid(UUID.fromString(uuid)))
65                         .build();
66 
67         bluetoothLeAdvertiser.startAdvertisingSet(
68                 params, data, null, null, null, new AdvertisingSetCallback() {});
69     }
70 
createAndAdvertiseIsolatedServer(String uuid)71     public void createAndAdvertiseIsolatedServer(String uuid) {
72         var gattServer = createGattServer(uuid);
73 
74         var bluetoothLeAdvertiser = mBluetoothAdapter.getBluetoothLeAdvertiser();
75         var params =
76                 new AdvertisingSetParameters.Builder()
77                         .setConnectable(true)
78                         .setOwnAddressType(
79                                 AdvertisingSetParameters.ADDRESS_TYPE_RANDOM_NON_RESOLVABLE)
80                         .build();
81         var data =
82                 new AdvertiseData.Builder()
83                         .addServiceUuid(new ParcelUuid(UUID.fromString(uuid)))
84                         .build();
85 
86         bluetoothLeAdvertiser.startAdvertisingSet(
87                 params,
88                 data,
89                 null,
90                 null,
91                 null,
92                 0,
93                 0,
94                 gattServer,
95                 new AdvertisingSetCallback() {},
96                 new Handler(Looper.getMainLooper()));
97     }
98 }
99