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;
21 import android.app.Activity;
22 import android.bluetooth.BluetoothAdapter;
23 import android.bluetooth.BluetoothDevice;
24 import android.content.BroadcastReceiver;
25 import android.content.Context;
26 import android.content.Intent;
27 import android.content.IntentFilter;
28 import android.os.Bundle;
29 import android.view.View;
30 import android.view.Window;
31 import android.view.View.OnClickListener;
32 import android.widget.AdapterView;
33 import android.widget.ArrayAdapter;
34 import android.widget.Button;
35 import android.widget.ListView;
36 import android.widget.TextView;
37 import android.widget.AdapterView.OnItemClickListener;
40 * This Activity appears as a dialog. It lists any paired devices and
41 * devices detected in the area after discovery. When a device is chosen
42 * by the user, the MAC address of the device is sent back to the parent
43 * Activity in the result Intent.
45 public class DeviceListActivity extends Activity {
47 // Return Intent extra
48 public static final String EXTRA_DEVICE_ADDRESS = "device_address";
49 public static final String EXTRA_DEVICE_NAME = "device_name";
52 private BluetoothAdapter mBtAdapter;
53 private ArrayAdapter<String> mPairedDevicesArrayAdapter;
54 private ArrayAdapter<String> mNewDevicesArrayAdapter;
57 protected void onCreate(Bundle savedInstanceState) {
58 setTheme(AltosDroid.dialog_themes[AltosDroidPreferences.font_size()]);
59 super.onCreate(savedInstanceState);
62 setContentView(R.layout.device_list);
64 // Set result CANCELED incase the user backs out
65 setResult(Activity.RESULT_CANCELED);
67 // Initialize the button to perform device discovery
68 Button scanButton = (Button) findViewById(R.id.button_scan);
69 scanButton.setOnClickListener(new OnClickListener() {
70 public void onClick(View v) {
72 v.setVisibility(View.GONE);
76 // Initialize array adapters. One for already paired devices and
77 // one for newly discovered devices
78 mPairedDevicesArrayAdapter = new ArrayAdapter<String>(this, R.layout.device_name);
79 mNewDevicesArrayAdapter = new ArrayAdapter<String>(this, R.layout.device_name);
81 // Find and set up the ListView for paired devices
82 ListView pairedListView = (ListView) findViewById(R.id.paired_devices);
83 pairedListView.setAdapter(mPairedDevicesArrayAdapter);
84 pairedListView.setOnItemClickListener(mDeviceClickListener);
86 // Find and set up the ListView for newly discovered devices
87 ListView newDevicesListView = (ListView) findViewById(R.id.new_devices);
88 newDevicesListView.setAdapter(mNewDevicesArrayAdapter);
89 newDevicesListView.setOnItemClickListener(mDeviceClickListener);
91 // Register for broadcasts when a device is discovered
92 IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
93 this.registerReceiver(mReceiver, filter);
95 // Register for broadcasts when discovery has finished
96 filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
97 this.registerReceiver(mReceiver, filter);
99 // Get the local Bluetooth adapter
100 mBtAdapter = BluetoothAdapter.getDefaultAdapter();
102 // Get a set of currently paired devices
103 Set<BluetoothDevice> pairedDevices = mBtAdapter.getBondedDevices();
105 // If there are paired devices, add each one to the ArrayAdapter
106 if (pairedDevices.size() > 0) {
107 findViewById(R.id.title_paired_devices).setVisibility(View.VISIBLE);
108 for (BluetoothDevice device : pairedDevices) {
109 String name = device.getName();
110 if (name != null && name.startsWith("TeleBT"))
111 mPairedDevicesArrayAdapter.add(name + "\n" + device.getAddress());
114 String noDevices = getResources().getText(R.string.none_paired).toString();
115 mPairedDevicesArrayAdapter.add(noDevices);
120 protected void onDestroy() {
123 // Make sure we're not doing discovery anymore
124 if (mBtAdapter != null) {
125 mBtAdapter.cancelDiscovery();
128 // Unregister broadcast listeners
129 this.unregisterReceiver(mReceiver);
133 * Start device discover with the BluetoothAdapter
135 private void doDiscovery() {
136 AltosDebug.debug("doDiscovery()");
138 // Indicate scanning in the title
139 setTitle(R.string.scanning);
141 // Turn on sub-title for new devices
142 findViewById(R.id.title_new_devices).setVisibility(View.VISIBLE);
144 // If we're already discovering, stop it
145 if (mBtAdapter.isDiscovering()) {
146 mBtAdapter.cancelDiscovery();
149 // Request discover from BluetoothAdapter
150 mBtAdapter.startDiscovery();
153 // The on-click listener for all devices in the ListViews
154 private OnItemClickListener mDeviceClickListener = new OnItemClickListener() {
155 public void onItemClick(AdapterView<?> av, View v, int arg2, long arg3) {
156 // Get the device MAC address, which is the last 17 chars in the View
157 String info = ((TextView) v).getText().toString();
159 /* Ignore clicks on items that are too short */
160 if (info.length() <= 17)
163 // Cancel discovery because it's costly and we're about to connect
164 mBtAdapter.cancelDiscovery();
166 String address = info.substring(info.length() - 17);
168 int newline = info.indexOf('\n');
172 name = info.substring(0, newline);
176 AltosDebug.debug("******* selected item '%s'", info);
178 // Create the result Intent and include the MAC address
179 Intent intent = new Intent();
180 intent.putExtra(EXTRA_DEVICE_ADDRESS, address);
181 intent.putExtra(EXTRA_DEVICE_NAME, name);
183 // Set result and finish this Activity
184 setResult(Activity.RESULT_OK, intent);
189 // The BroadcastReceiver that listens for discovered devices and
190 // changes the title when discovery is finished
191 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
193 public void onReceive(Context context, Intent intent) {
194 String action = intent.getAction();
196 // When discovery finds a device
197 if (BluetoothDevice.ACTION_FOUND.equals(action)) {
199 /* Get the BluetoothDevice object from the Intent
201 BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
203 /* If it's already paired, skip it, because it's been listed already
205 if (device != null && device.getBondState() != BluetoothDevice.BOND_BONDED)
207 String name = device.getName();
208 if (name != null && name.startsWith("TeleBT"))
209 mNewDevicesArrayAdapter.add(name + "\n" + device.getAddress());
212 /* When discovery is finished, change the Activity title
214 } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
215 setTitle(R.string.select_device);
216 if (mNewDevicesArrayAdapter.getCount() == 0) {
217 String noDevices = getResources().getText(R.string.none_found).toString();
218 mNewDevicesArrayAdapter.add(noDevices);