前の記事:Bluetoothアダプタを有効にする
ペアディバイスを取得してListViewに表示
import java.util.Set;
import android.os.Bundle;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Bluetoothアダプタが利用可能か調べる
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
//ペアディバイスの表示
Set pairedDevices = mBluetoothAdapter.getBondedDevices();
//ペアディバイスをListViewに表示
//ListView を取得
ListView listView = (ListView) findViewById(R.id.listView1);
//ListView のアダプタ設定
ArrayAdapter mArrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1);
if (pairedDevices.size() > 0) {
// Loop through paired devices
for (BluetoothDevice device : pairedDevices) {
// Add the name and address to an array adapter to show in a ListView
mArrayAdapter.add(device.getName() + "\n" + device.getAddress());
//Log.v("タグ", device.getName() + "\n" + device.getAddress() );
}
}
listView.setAdapter(mArrayAdapter);
}
}

0 件のコメント:
コメントを投稿