-
Notifications
You must be signed in to change notification settings - Fork 240
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #68 from android/cdm-sample
Add new CompanionDeviceManager Sample
- Loading branch information
Showing
10 changed files
with
734 additions
and
16 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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,38 @@ | ||
# Companion Device Manager Sample | ||
|
||
This sample showcases the use of the | ||
[Companion Device Manager](https://developer.android.com/reference/android/companion/CompanionDeviceManager#startSystemDataTransfer(int,%20java.util.concurrent.Executor,%20android.os.OutcomeReceiver%3Cjava.lang.Void,android.companion.CompanionException%3E)) | ||
(CDM) to find and associate devices. | ||
|
||
## How to use the sample: | ||
|
||
1. Use two devices running the sample | ||
2. In one device start the [GATTServerSample](../ble/src/main/java/com/example/platform/connectivity/bluetooth/ble/GATTServerSample.kt) | ||
3. In the other open the [CompanionDeviceManagerSample](/src/main/java/com/example/platform/connectivity/bluetooth/cdm/CompanionDeviceManagerSample.kt)) | ||
4. Click start button in the CDM sample | ||
|
||
It should directly find the server and a system request will appear. Once accepted both devices will | ||
be associated. You can then connect (see [ConnectGATTSample](../ble/src/main/java/com/example/platform/connectivity/bluetooth/ble/ConnectGATTSample.kt) | ||
and receive (see [CompanionDeviceSampleService](/src/main/java/com/example/platform/connectivity/bluetooth/cdm/CompanionDeviceSampleService.kt)) | ||
appear and disappear events (if running A12+). | ||
|
||
> Note: You can associate multiple devices. When the server closes and opens again a new mac address | ||
> will be used, thus you need to associate them again. | ||
## License | ||
|
||
``` | ||
Copyright 2022 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. | ||
``` |
This file contains 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,28 @@ | ||
/* | ||
* Copyright 2022 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. | ||
*/ | ||
|
||
plugins { | ||
id("com.example.platform.sample") | ||
} | ||
|
||
|
||
android { | ||
namespace = "com.example.platform.connectivity.bluetooth.companion" | ||
} | ||
|
||
dependencies { | ||
implementation(project(":samples:connectivity:bluetooth:ble")) | ||
} |
43 changes: 43 additions & 0 deletions
43
samples/connectivity/bluetooth/companion/src/main/AndroidManifest.xml
This file contains 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,43 @@ | ||
<?xml version="1.0" encoding="utf-8"?><!-- | ||
~ Copyright 2023 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. | ||
--> | ||
|
||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"> | ||
|
||
<!-- Define that the app uses CDM --> | ||
<uses-feature android:name="android.software.companion_device_setup" /> | ||
|
||
<!-- Only needed to connect to the device once associated --> | ||
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" /> | ||
|
||
<!-- Needed if we want to be notified when an associated device is in range --> | ||
<uses-permission android:name="android.permission.REQUEST_OBSERVE_COMPANION_DEVICE_PRESENCE" /> | ||
|
||
<!-- Not required by CDM but we use it for notifying when device is in range --> | ||
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" /> | ||
|
||
<application> | ||
|
||
<service | ||
android:name="com.example.platform.connectivity.bluetooth.cdm.CompanionDeviceSampleService" | ||
android:exported="true" | ||
android:permission="android.permission.BIND_COMPANION_DEVICE_SERVICE"> | ||
<intent-filter> | ||
<action android:name="android.companion.CompanionDeviceService" /> | ||
</intent-filter> | ||
</service> | ||
</application> | ||
|
||
</manifest> |
65 changes: 65 additions & 0 deletions
65
...n/src/main/java/com/example/platform/connectivity/bluetooth/cdm/AssociatedDeviceCompat.kt
This file contains 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,65 @@ | ||
/* | ||
* Copyright 2023 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.example.platform.connectivity.bluetooth.cdm | ||
|
||
import android.bluetooth.BluetoothDevice | ||
import android.companion.AssociationInfo | ||
import android.companion.CompanionDeviceManager | ||
import android.os.Build | ||
import androidx.annotation.RequiresApi | ||
|
||
/** | ||
* Wrapper for the different type of classes the CDM returns | ||
*/ | ||
data class AssociatedDeviceCompat( | ||
val id: Int, | ||
val address: String, | ||
val name: String, | ||
val device: BluetoothDevice?, | ||
) | ||
|
||
@RequiresApi(Build.VERSION_CODES.O) | ||
internal fun CompanionDeviceManager.getAssociatedDevices(): List<AssociatedDeviceCompat> { | ||
val associatedDevice = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { | ||
myAssociations.map { it.toAssociatedDevice() } | ||
} else { | ||
// Before Android 34 we can only get the MAC. We could use the BT adapter to find the | ||
// device, but to use CDM we only need the MAC. | ||
@Suppress("DEPRECATION") | ||
associations.map { | ||
AssociatedDeviceCompat( | ||
id = -1, | ||
address = it, | ||
name = "", | ||
device = null, | ||
) | ||
} | ||
} | ||
return associatedDevice | ||
} | ||
|
||
@RequiresApi(Build.VERSION_CODES.TIRAMISU) | ||
internal fun AssociationInfo.toAssociatedDevice() = AssociatedDeviceCompat( | ||
id = id, | ||
address = deviceMacAddress?.toString() ?: "N/A", | ||
name = displayName?.ifBlank { "N/A" }?.toString() ?: "N/A", | ||
device = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) { | ||
associatedDevice?.bleDevice?.device | ||
} else { | ||
null | ||
}, | ||
) |
Oops, something went wrong.