aboutsummaryrefslogtreecommitdiff
path: root/tests/EmbeddedKitchenSinkApp/src/com/google/android/car/kitchensink/bluetooth/BluetoothHeadsetFragment.java
blob: 30804feb087c7f0cac4bb09a11625501e6b571a2 (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/*
 * Copyright (C) 2016 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
 *
 *      http://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.google.android.car.kitchensink.bluetooth;

import android.app.PendingIntent;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothDevicePicker;
import android.bluetooth.BluetoothHeadsetClient;
import android.bluetooth.BluetoothMapClient;
import android.bluetooth.BluetoothProfile;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.car.kitchensink.R;

import java.util.List;

public class BluetoothHeadsetFragment extends Fragment {
    private static final String TAG = "CAR.BLUETOOTH.KS";
    BluetoothAdapter mBluetoothAdapter;
    BluetoothDevice mPickedDevice;

    TextView mPickedDeviceText;
    Button mDevicePicker;
    Button mConnect;
    Button mDisconnect;
    Button mScoConnect;
    Button mScoDisconnect;
    Button mEnableQuietMode;

    BluetoothHeadsetClient mHfpClientProfile;

    // Intent for picking a Bluetooth device
    public static final String DEVICE_PICKER_ACTION = "android.bluetooth.devicepicker.action.LAUNCH";

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
            @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.bluetooth_headset, container, false);

        mPickedDeviceText = (TextView) v.findViewById(R.id.bluetooth_device);
        mDevicePicker = (Button) v.findViewById(R.id.bluetooth_pick_device);
        mConnect = (Button) v.findViewById(R.id.bluetooth_headset_connect);
        mDisconnect = (Button) v.findViewById(R.id.bluetooth_headset_disconnect);
        mScoConnect = (Button) v.findViewById(R.id.bluetooth_sco_connect);
        mScoDisconnect= (Button) v.findViewById(R.id.bluetooth_sco_disconnect);
        mEnableQuietMode = (Button) v.findViewById(R.id.bluetooth_quiet_mode_enable);

        // Pick a bluetooth device
        mDevicePicker.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                launchDevicePicker();
            }
        });

        // Connect profile
        mConnect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                connect();
            }
        });

        // Disonnect profile
        mDisconnect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                disconnect();
            }
        });

        // Connect SCO
        mScoConnect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                connectSco();
            }
        });

        // Disconnect SCO
        mScoDisconnect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                disconnectSco();
            }
        });

        // Enable quiet mode
        mEnableQuietMode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mBluetoothAdapter.enableNoAutoConnect();
            }
        });
        return v;
    }

    void launchDevicePicker() {
        IntentFilter filter = new IntentFilter();
        filter.addAction(BluetoothDevicePicker.ACTION_DEVICE_SELECTED);
        getContext().registerReceiver(mPickerReceiver, filter);

        Intent intent = new Intent(DEVICE_PICKER_ACTION);
        intent.setFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
        getContext().startActivity(intent);
    }

    void connect() {
        if (mPickedDevice == null) {
            Log.w(TAG, "Device null when trying to connect sco!");
            return;
        }

        // Check if we have the proxy and connect the device.
        if (mHfpClientProfile == null) {
            Log.w(TAG, "HFP Profile proxy not available, cannot connect sco to " + mPickedDevice);
            return;
        }
        mHfpClientProfile.connect(mPickedDevice);
    }

    void disconnect() {
        if (mPickedDevice == null) {
            Log.w(TAG, "Device null when trying to connect sco!");
            return;
        }

        // Check if we have the proxy and connect the device.
        if (mHfpClientProfile == null) {
            Log.w(TAG, "HFP Profile proxy not available, cannot connect sco to " + mPickedDevice);
            return;
        }
        mHfpClientProfile.disconnect(mPickedDevice);
    }

    void connectSco() {
        if (mPickedDevice == null) {
            Log.w(TAG, "Device null when trying to connect sco!");
            return;
        }

        // Check if we have the proxy and connect the device.
        if (mHfpClientProfile == null) {
            Log.w(TAG, "HFP Profile proxy not available, cannot connect sco to " + mPickedDevice);
            return;
        }
        mHfpClientProfile.connectAudio(mPickedDevice);
    }

    void disconnectSco() {
        if (mPickedDevice == null) {
            Log.w(TAG, "Device null when trying to disconnect sco!");
            return;
        }

        if (mHfpClientProfile == null) {
            Log.w(TAG, "HFP Profile proxy not available, cannot disconnect sco to " +
                mPickedDevice);
            return;
        }
        mHfpClientProfile.disconnectAudio(mPickedDevice);
    }

    private final BroadcastReceiver mPickerReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();

            Log.v(TAG, "mPickerReceiver got " + action);

            if (BluetoothDevicePicker.ACTION_DEVICE_SELECTED.equals(action)) {
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                mPickedDevice = device;
                String text = device.getName() == null ?
                    device.getAddress() : device.getName() + " " + device.getAddress();
                mPickedDeviceText.setText(text);

                // The receiver can now be disabled.
                getContext().unregisterReceiver(mPickerReceiver);
            }
        }
    };

    @Override
    public void onResume() {
        super.onResume();
        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        mBluetoothAdapter.getProfileProxy(
            getContext(), new ProfileServiceListener(), BluetoothProfile.HEADSET_CLIENT);
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    class ProfileServiceListener implements BluetoothProfile.ServiceListener {
        @Override
        public void onServiceConnected(int profile, BluetoothProfile proxy) {
            Log.d(TAG, "Proxy connected for profile: " + profile);
            switch (profile) {
                case BluetoothProfile.HEADSET_CLIENT:
                    mHfpClientProfile = (BluetoothHeadsetClient) proxy;
                    break;
                default:
                    Log.w(TAG, "onServiceConnected not supported profile: " + profile);
            }
        }

        @Override
        public void onServiceDisconnected(int profile) {
            Log.d(TAG, "Proxy disconnected for profile: " + profile);
            switch (profile) {
                case BluetoothProfile.HEADSET_CLIENT:
                    mHfpClientProfile = null;
                    break;
                default:
                    Log.w(TAG, "onServiceDisconnected not supported profile: " + profile);
            }
        }
    }
}