summaryrefslogtreecommitdiff
path: root/src/com/android/car/messenger/bluetooth/BluetoothHelper.java
blob: e95b386a65a70710ae021476a18669d82469e931 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package com.android.car.messenger.bluetooth;

import android.app.PendingIntent;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothMapClient;
import android.net.Uri;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.Collections;
import java.util.Set;

/**
 * Provides helper methods for performing bluetooth actions.
 */
public class BluetoothHelper {

    /**
     * Returns a (potentially empty) immutable set of bonded (paired) devices.
     */
    public static Set<BluetoothDevice> getBondedDevices() {
        BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();

        if (adapter != null) {
            Set<BluetoothDevice> devices = adapter.getBondedDevices();
            if (devices != null) {
                return devices;
            }
        }

        return Collections.emptySet();
    }

    /**
     * Helper method to send an SMS message through bluetooth.
     *
     * @param client the MAP Client used to send the message
     * @param deviceAddress the device used to send the SMS
     * @param contacts contacts to send the message to
     * @param message message to send
     * @param sentIntent callback issued once the message was sent
     * @param deliveredIntent callback issued once the message was delivered
     * @return true if the message was enqueued, false on error
     * @throws IllegalArgumentException if deviceAddress is invalid
     */
    public static boolean sendMessage(@NonNull BluetoothMapClient client,
            String deviceAddress,
            Uri[] contacts,
            String message,
            @Nullable PendingIntent sentIntent,
            @Nullable PendingIntent deliveredIntent)
            throws IllegalArgumentException {

        BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
        if (adapter == null) {
            return false;
        }
        BluetoothDevice device = adapter.getRemoteDevice(deviceAddress);

        return client.sendMessage(device, contacts, message, sentIntent, deliveredIntent);
    }
}