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 if (device.getName().startsWith("TeleBT"))
114 mPairedDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress());
117 String noDevices = getResources().getText(R.string.none_paired).toString();
118 mPairedDevicesArrayAdapter.add(noDevices);
123 protected void onDestroy() {
126 // Make sure we're not doing discovery anymore
127 if (mBtAdapter != null) {
128 mBtAdapter.cancelDiscovery();
131 // Unregister broadcast listeners
132 this.unregisterReceiver(mReceiver);
136 * Start device discover with the BluetoothAdapter
138 private void doDiscovery() {
139 if (D) Log.d(TAG, "doDiscovery()");
141 // Indicate scanning in the title
142 setProgressBarIndeterminateVisibility(true);
143 setTitle(R.string.scanning);
145 // Turn on sub-title for new devices
146 findViewById(R.id.title_new_devices).setVisibility(View.VISIBLE);
148 // If we're already discovering, stop it
149 if (mBtAdapter.isDiscovering()) {
150 mBtAdapter.cancelDiscovery();
153 // Request discover from BluetoothAdapter
154 mBtAdapter.startDiscovery();
157 // The on-click listener for all devices in the ListViews
158 private OnItemClickListener mDeviceClickListener = new OnItemClickListener() {
159 public void onItemClick(AdapterView<?> av, View v, int arg2, long arg3) {
160 // Cancel discovery because it's costly and we're about to connect
161 mBtAdapter.cancelDiscovery();
163 // Get the device MAC address, which is the last 17 chars in the View
164 String info = ((TextView) v).getText().toString();
165 String address = info.substring(info.length() - 17);
167 // Create the result Intent and include the MAC address
168 Intent intent = new Intent();
169 intent.putExtra(EXTRA_DEVICE_ADDRESS, address);
171 // Set result and finish this Activity
172 setResult(Activity.RESULT_OK, intent);
177 // The BroadcastReceiver that listens for discovered devices and
178 // changes the title when discovery is finished
179 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
181 public void onReceive(Context context, Intent intent) {
182 String action = intent.getAction();
184 // When discovery finds a device
185 if (BluetoothDevice.ACTION_FOUND.equals(action)) {
186 // Get the BluetoothDevice object from the Intent
187 BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
188 // If it's already paired, skip it, because it's been listed already
189 if ( device.getBondState() != BluetoothDevice.BOND_BONDED
190 && device.getName().startsWith("TeleBT") ) {
191 mNewDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress());
193 // When discovery is finished, change the Activity title
194 } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
195 setProgressBarIndeterminateVisibility(false);
196 setTitle(R.string.select_device);
197 if (mNewDevicesArrayAdapter.getCount() == 0) {
198 String noDevices = getResources().getText(R.string.none_found).toString();
199 mNewDevicesArrayAdapter.add(noDevices);