Added documentation for Bluetooth MIDI components
[pdlib:btmidi.git] / BluetoothMidi / src / com / noisepages / nettoyeur / bluetooth / DeviceListActivity.java
1 /*
2  * Derived from DeviceListActivity.java in android-7/samples/BluetoothChat
3  *
4  * Modifications
5  * Copyright (C) 2011 Peter Brinkmann (peter.brinkmann@gmail.com)
6  *
7  * Copyright (C) 2009 The Android Open Source Project
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */
21
22 package com.noisepages.nettoyeur.bluetooth;
23
24 import java.util.Set;
25
26 import android.app.Activity;
27 import android.bluetooth.BluetoothAdapter;
28 import android.bluetooth.BluetoothDevice;
29 import android.content.Intent;
30 import android.os.Bundle;
31 import android.view.View;
32 import android.widget.AdapterView;
33 import android.widget.ArrayAdapter;
34 import android.widget.ListView;
35 import android.widget.TextView;
36 import android.widget.AdapterView.OnItemClickListener;
37
38
39 /**
40  * A simple activity that offers the user a list of paired Bluetooth devices to choose from.
41  */
42 public class DeviceListActivity extends Activity {
43
44         public static final String DEVICE_ADDRESS = "device_address";
45
46         private BluetoothAdapter btAdapter;
47         private ArrayAdapter<String> pairedDevicesAdapter;
48         private boolean empty;
49
50         @Override
51         protected void onCreate(Bundle savedInstanceState) {
52                 super.onCreate(savedInstanceState);
53                 setContentView(R.layout.device_list);
54                 setResult(Activity.RESULT_CANCELED);
55                 pairedDevicesAdapter = new ArrayAdapter<String>(this, R.layout.device_name);
56                 ListView pairedListView = (ListView) findViewById(R.id.paired_devices);
57                 pairedListView.setAdapter(pairedDevicesAdapter);
58                 pairedListView.setOnItemClickListener(clickListener);
59                 btAdapter = BluetoothAdapter.getDefaultAdapter();
60                 Set<BluetoothDevice> pairedDevices = btAdapter.getBondedDevices();
61                 empty = pairedDevices.isEmpty();
62                 if (!empty) {
63                         findViewById(R.id.title_paired_devices).setVisibility(View.VISIBLE);
64                         for (BluetoothDevice device : pairedDevices) {
65                                 pairedDevicesAdapter.add(device.getName() + "\n" + device.getAddress());
66                         }
67                 } else {
68                         String noDevices = getResources().getText(R.string.none_paired).toString();
69                         pairedDevicesAdapter.add(noDevices);
70                 }
71         }
72
73         @Override
74         protected void onDestroy() {
75                 super.onDestroy();
76         }
77
78         private OnItemClickListener clickListener = new OnItemClickListener() {
79                 public void onItemClick(AdapterView<?> av, View v, int arg2, long arg3) {
80                         if (!empty) {
81                                 String info = ((TextView) v).getText().toString();
82                                 String address = info.substring(info.length() - 17);
83                                 Intent intent = new Intent();
84                                 intent.putExtra(DEVICE_ADDRESS, address);
85                                 setResult(Activity.RESULT_OK, intent);
86                         }
87                         finish();
88                 }
89         };
90 }