2 * Copyright (C) 2009 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.altusmetrum.AltosDroid;
20 import org.altusmetrum.AltosDroid.R;
22 import android.app.Activity;
23 import android.bluetooth.BluetoothAdapter;
24 import android.bluetooth.BluetoothDevice;
25 import android.content.BroadcastReceiver;
26 import android.content.Context;
27 import android.content.Intent;
28 import android.content.IntentFilter;
29 import android.os.Bundle;
30 import android.util.Log;
31 import android.view.View;
32 import android.view.Window;
33 import android.view.View.OnClickListener;
34 import android.widget.AdapterView;
35 import android.widget.ArrayAdapter;
36 import android.widget.Button;
37 import android.widget.ListView;
38 import android.widget.TextView;
39 import android.widget.AdapterView.OnItemClickListener;
42 * This Activity appears as a dialog. It lists any paired devices and
43 * devices detected in the area after discovery. When a device is chosen
44 * by the user, the MAC address of the device is sent back to the parent
45 * Activity in the result Intent.
47 public class DeviceListActivity extends Activity {
49 private static final String TAG = "DeviceListActivity";
50 private static final boolean D = true;
52 // Return Intent extra
53 public static String EXTRA_DEVICE_ADDRESS = "device_address";
56 private BluetoothAdapter mBtAdapter;
57 private ArrayAdapter<String> mPairedDevicesArrayAdapter;
58 private ArrayAdapter<String> mNewDevicesArrayAdapter;
61 protected void onCreate(Bundle savedInstanceState) {
62 super.onCreate(savedInstanceState);
65 requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
66 setContentView(R.layout.device_list);
68 // Set result CANCELED incase the user backs out
69 setResult(Activity.RESULT_CANCELED);
71 // Initialize the button to perform device discovery
72 Button scanButton = (Button) findViewById(R.id.button_scan);
73 scanButton.setOnClickListener(new OnClickListener() {
74 public void onClick(View v) {
76 v.setVisibility(View.GONE);
80 // Initialize array adapters. One for already paired devices and
81 // one for newly discovered devices
82 mPairedDevicesArrayAdapter = new ArrayAdapter<String>(this, R.layout.device_name);
83 mNewDevicesArrayAdapter = new ArrayAdapter<String>(this, R.layout.device_name);
85 // Find and set up the ListView for paired devices
86 ListView pairedListView = (ListView) findViewById(R.id.paired_devices);
87 pairedListView.setAdapter(mPairedDevicesArrayAdapter);
88 pairedListView.setOnItemClickListener(mDeviceClickListener);
90 // Find and set up the ListView for newly discovered devices
91 ListView newDevicesListView = (ListView) findViewById(R.id.new_devices);
92 newDevicesListView.setAdapter(mNewDevicesArrayAdapter);
93 newDevicesListView.setOnItemClickListener(mDeviceClickListener);
95 // Register for broadcasts when a device is discovered
96 IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
97 this.registerReceiver(mReceiver, filter);
99 // Register for broadcasts when discovery has finished
100 filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
101 this.registerReceiver(mReceiver, filter);
103 // Get the local Bluetooth adapter
104 mBtAdapter = BluetoothAdapter.getDefaultAdapter();
106 // Get a set of currently paired devices
107 Set<BluetoothDevice> pairedDevices = mBtAdapter.getBondedDevices();
109 // If there are paired devices, add each one to the ArrayAdapter
110 if (pairedDevices.size() > 0) {
111 findViewById(R.id.title_paired_devices).setVisibility(View.VISIBLE);
112 for (BluetoothDevice device : pairedDevices) {
113 mPairedDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress());
116 String noDevices = getResources().getText(R.string.none_paired).toString();
117 mPairedDevicesArrayAdapter.add(noDevices);
122 protected void onDestroy() {
125 // Make sure we're not doing discovery anymore
126 if (mBtAdapter != null) {
127 mBtAdapter.cancelDiscovery();
130 // Unregister broadcast listeners
131 this.unregisterReceiver(mReceiver);
135 * Start device discover with the BluetoothAdapter
137 private void doDiscovery() {
138 if (D) Log.d(TAG, "doDiscovery()");
140 // Indicate scanning in the title
141 setProgressBarIndeterminateVisibility(true);
142 setTitle(R.string.scanning);
144 // Turn on sub-title for new devices
145 findViewById(R.id.title_new_devices).setVisibility(View.VISIBLE);
147 // If we're already discovering, stop it
148 if (mBtAdapter.isDiscovering()) {
149 mBtAdapter.cancelDiscovery();
152 // Request discover from BluetoothAdapter
153 mBtAdapter.startDiscovery();
156 // The on-click listener for all devices in the ListViews
157 private OnItemClickListener mDeviceClickListener = new OnItemClickListener() {
158 public void onItemClick(AdapterView<?> av, View v, int arg2, long arg3) {
159 // Cancel discovery because it's costly and we're about to connect
160 mBtAdapter.cancelDiscovery();
162 // Get the device MAC address, which is the last 17 chars in the View
163 String info = ((TextView) v).getText().toString();
164 String address = info.substring(info.length() - 17);
166 // Create the result Intent and include the MAC address
167 Intent intent = new Intent();
168 intent.putExtra(EXTRA_DEVICE_ADDRESS, address);
170 // Set result and finish this Activity
171 setResult(Activity.RESULT_OK, intent);
176 // The BroadcastReceiver that listens for discovered devices and
177 // changes the title when discovery is finished
178 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
180 public void onReceive(Context context, Intent intent) {
181 String action = intent.getAction();
183 // When discovery finds a device
184 if (BluetoothDevice.ACTION_FOUND.equals(action)) {
185 // Get the BluetoothDevice object from the Intent
186 BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
187 // If it's already paired, skip it, because it's been listed already
188 if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
189 mNewDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress());
191 // When discovery is finished, change the Activity title
192 } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
193 setProgressBarIndeterminateVisibility(false);
194 setTitle(R.string.select_device);
195 if (mNewDevicesArrayAdapter.getCount() == 0) {
196 String noDevices = getResources().getText(R.string.none_found).toString();
197 mNewDevicesArrayAdapter.add(noDevices);