1 package com.android.car.messenger.bluetooth; 2 3 import android.app.PendingIntent; 4 import android.bluetooth.BluetoothAdapter; 5 import android.bluetooth.BluetoothDevice; 6 import android.bluetooth.BluetoothMapClient; 7 import android.net.Uri; 8 9 import androidx.annotation.NonNull; 10 import androidx.annotation.Nullable; 11 12 import java.util.Collections; 13 import java.util.Set; 14 15 /** 16 * Provides helper methods for performing bluetooth actions. 17 */ 18 public class BluetoothHelper { 19 20 /** 21 * Returns a (potentially empty) immutable set of bonded (paired) devices. 22 */ getPairedDevices()23 public static Set<BluetoothDevice> getPairedDevices() { 24 BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); 25 26 if (adapter != null) { 27 Set<BluetoothDevice> devices = adapter.getBondedDevices(); 28 if (devices != null) { 29 return devices; 30 } 31 } 32 33 return Collections.emptySet(); 34 } 35 36 /** 37 * Helper method to send an SMS message through bluetooth. 38 * 39 * @param client the MAP Client used to send the message 40 * @param deviceAddress the device used to send the SMS 41 * @param contacts contacts to send the message to 42 * @param message message to send 43 * @param sentIntent callback issued once the message was sent 44 * @param deliveredIntent callback issued once the message was delivered 45 * @return true if the message was enqueued, false on error 46 * @throws IllegalArgumentException if deviceAddress is invalid 47 */ sendMessage(@onNull BluetoothMapClient client, String deviceAddress, Uri[] contacts, String message, @Nullable PendingIntent sentIntent, @Nullable PendingIntent deliveredIntent)48 public static boolean sendMessage(@NonNull BluetoothMapClient client, 49 String deviceAddress, 50 Uri[] contacts, 51 String message, 52 @Nullable PendingIntent sentIntent, 53 @Nullable PendingIntent deliveredIntent) 54 throws IllegalArgumentException { 55 56 BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); 57 if (adapter == null) { 58 return false; 59 } 60 BluetoothDevice device = adapter.getRemoteDevice(deviceAddress); 61 62 return client.sendMessage(device, contacts, message, sentIntent, deliveredIntent); 63 } 64 } 65