| /* |
| * Copyright (C) 2012 The Android Open Source Project |
| * |
| * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| * use this file except in compliance with the License. You may obtain a copy of |
| * the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| * License for the specific language governing permissions and limitations under |
| * the License. |
| */ |
| package com.google.android.apps.adk2.activity; |
| |
| import java.util.Set; |
| |
| import android.bluetooth.BluetoothAdapter; |
| import android.bluetooth.BluetoothDevice; |
| import android.content.Intent; |
| import android.os.Bundle; |
| import android.view.View; |
| import android.widget.AdapterView; |
| import android.widget.AdapterView.OnItemClickListener; |
| import android.widget.ArrayAdapter; |
| import android.widget.ListView; |
| import android.widget.TextView; |
| |
| import com.google.android.apps.adk2.R; |
| |
| public class BTDeviceListActivity extends Adk2BaseActivity implements |
| OnItemClickListener { |
| private BluetoothAdapter mBtAdapter; |
| private ArrayAdapter<String> mPairedDevicesArrayAdapter; |
| |
| public static String EXTRA_DEVICE_ADDRESS = "device_address"; |
| |
| @Override |
| public void onCreate(Bundle savedInstanceState) { |
| super.onCreate(savedInstanceState); |
| setContentView(R.layout.device_list); |
| |
| mPairedDevicesArrayAdapter = new ArrayAdapter<String>(this, |
| R.layout.device_name); |
| |
| // Find and set up the ListView for paired devices |
| ListView pairedListView = (ListView) findViewById(R.id.paired_devices); |
| pairedListView.setAdapter(mPairedDevicesArrayAdapter); |
| pairedListView.setOnItemClickListener(this); |
| |
| // Get the local Bluetooth adapter |
| mBtAdapter = BluetoothAdapter.getDefaultAdapter(); |
| |
| // Get a set of currently paired devices |
| Set<BluetoothDevice> pairedDevices = mBtAdapter.getBondedDevices(); |
| |
| // If there are paired devices, add each one to the ArrayAdapter |
| if (pairedDevices.size() > 0) { |
| findViewById(R.id.title_paired_devices).setVisibility(View.VISIBLE); |
| for (BluetoothDevice device : pairedDevices) { |
| mPairedDevicesArrayAdapter.add(device.getName() + "\n" |
| + device.getAddress()); |
| } |
| } else { |
| String noDevices = getResources().getText(R.string.none_paired) |
| .toString(); |
| mPairedDevicesArrayAdapter.add(noDevices); |
| } |
| } |
| |
| public void onItemClick(AdapterView<?> arg0, View v, int arg2, long arg3) { |
| mBtAdapter.cancelDiscovery(); |
| |
| // Get the device MAC address, which is the last 17 chars in the View |
| String info = ((TextView) v).getText().toString(); |
| |
| if (info.equals(getResources().getText(R.string.none_paired).toString())) |
| return; // do not proceed if we clicked the "there are no devices" |
| // message |
| |
| String address = info.substring(info.length() - 17); |
| Intent connectIntent = new Intent(this, HomeActivity.class); |
| connectIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); |
| connectIntent.putExtra(EXTRA_DEVICE_ADDRESS, address); |
| startActivity(connectIntent); |
| } |
| |
| } |