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 if (device.getName().startsWith("TeleBT"))
110 mPairedDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress());
113 String noDevices = getResources().getText(R.string.none_paired).toString();
114 mPairedDevicesArrayAdapter.add(noDevices);
119 protected void onDestroy() {
122 // Make sure we're not doing discovery anymore
123 if (mBtAdapter != null) {
124 mBtAdapter.cancelDiscovery();
127 // Unregister broadcast listeners
128 this.unregisterReceiver(mReceiver);
132 * Start device discover with the BluetoothAdapter
134 private void doDiscovery() {
135 AltosDebug.debug("doDiscovery()");
137 // Indicate scanning in the title
138 setTitle(R.string.scanning);
140 // Turn on sub-title for new devices
141 findViewById(R.id.title_new_devices).setVisibility(View.VISIBLE);
143 // If we're already discovering, stop it
144 if (mBtAdapter.isDiscovering()) {
145 mBtAdapter.cancelDiscovery();
148 // Request discover from BluetoothAdapter
149 mBtAdapter.startDiscovery();
152 // The on-click listener for all devices in the ListViews
153 private OnItemClickListener mDeviceClickListener = new OnItemClickListener() {
154 public void onItemClick(AdapterView<?> av, View v, int arg2, long arg3) {
155 // Cancel discovery because it's costly and we're about to connect
156 mBtAdapter.cancelDiscovery();
158 // Get the device MAC address, which is the last 17 chars in the View
159 String info = ((TextView) v).getText().toString();
160 String address = info.substring(info.length() - 17);
162 int newline = info.indexOf('\n');
166 name = info.substring(0, newline);
170 AltosDebug.debug("******* selected item '%s'", info);
172 // Create the result Intent and include the MAC address
173 Intent intent = new Intent();
174 intent.putExtra(EXTRA_DEVICE_ADDRESS, address);
175 intent.putExtra(EXTRA_DEVICE_NAME, name);
177 // Set result and finish this Activity
178 setResult(Activity.RESULT_OK, intent);
183 // The BroadcastReceiver that listens for discovered devices and
184 // changes the title when discovery is finished
185 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
187 public void onReceive(Context context, Intent intent) {
188 String action = intent.getAction();
190 // When discovery finds a device
191 if (BluetoothDevice.ACTION_FOUND.equals(action)) {
193 /* Get the BluetoothDevice object from the Intent
195 BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
197 /* If it's already paired, skip it, because it's been listed already
199 if (device != null && device.getBondState() != BluetoothDevice.BOND_BONDED)
201 String name = device.getName();
202 if (name != null && name.startsWith("TeleBT"))
203 mNewDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress());
206 /* When discovery is finished, change the Activity title
208 } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
209 setTitle(R.string.select_device);
210 if (mNewDevicesArrayAdapter.getCount() == 0) {
211 String noDevices = getResources().getText(R.string.none_found).toString();
212 mNewDevicesArrayAdapter.add(noDevices);