ボンディングされたBluetoothデバイスを取得しようとしていますが、リストではなく長い文字列として取得できます。
これは私のコードです:
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
ArrayList<String> listview =
new ArrayList<String>(Arrays.asList(pairedDevices.toString()));
setListAdapter(new ArrayAdapter<String>(this, R.layout.list, listview));
私はこのようなものを得ています:[00:23:7F:1c, f0:09:f1:b4:b0]
。そして、そのすべてが一列に並んでいます。すべてを1行でなくリストに含めるにはどうすればよいですか?
また、これらの番号ではなく、デバイスのわかりやすい名前を取得するにはどうすればよいですか?
ありがとう!!!
以下のようにコードを変更する必要があります。
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
List<String> s = new ArrayList<String>();
for(BluetoothDevice bt : pairedDevices)
s.add(bt.getName());
setListAdapter(new ArrayAdapter<String>(this, R.layout.list, s));
検索のリストNearby Bluetoothデバイス:
同じスクリーンショットを見つけます。
MainActivity.Java:
public class MainActivity extends ActionBarActivity {
private ListView listView;
private ArrayList<String> mDeviceList = new ArrayList<String>();
private BluetoothAdapter mBluetoothAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.listView);
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
mBluetoothAdapter.startDiscovery();
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mReceiver, filter);
}
@Override
protected void onDestroy() {
unregisterReceiver(mReceiver);
super.onDestroy();
}
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
BluetoothDevice device = intent
.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
mDeviceList.add(device.getName() + "\n" + device.getAddress());
Log.i("BT", device.getName() + "\n" + device.getAddress());
listView.setAdapter(new ArrayAdapter<String>(context,
Android.R.layout.simple_list_item_1, mDeviceList));
}
}
};
activity_main.xml:
<RelativeLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
xmlns:tools="http://schemas.Android.com/tools"
Android:layout_width="match_parent"
Android:layout_height="match_parent"
tools:context="com.example.bluetoothdemo.MainActivity" >
<ListView
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:id="@+id/listView"/>
</RelativeLayout>
マニフェストファイル:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:Android="http://schemas.Android.com/apk/res/Android"
package="com.example.bluetoothdemo"
Android:versionCode="1"
Android:versionName="1.0" >
<uses-sdk
Android:minSdkVersion="8"
Android:targetSdkVersion="21" />
<uses-permission Android:name="Android.permission.BLUETOOTH" />
<uses-permission Android:name="Android.permission.BLUETOOTH_ADMIN" />
<application
Android:allowBackup="true"
Android:icon="@drawable/ic_launcher"
Android:label="@string/app_name"
Android:theme="@style/AppTheme" >
<activity
Android:name=".MainActivity"
Android:label="@string/app_name" >
<intent-filter>
<action Android:name="Android.intent.action.MAIN" />
<category Android:name="Android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
完了
私は以下のコードを試しました、
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
Android:orientation="vertical"
Android:layout_width="fill_parent"
Android:layout_height="fill_parent"
>
<TextView
Android:layout_width="fill_parent"
Android:layout_height="wrap_content"
Android:text="@string/hello"
/>
<TextView
Android:id="@+id/bluetoothstate"
Android:layout_width="fill_parent"
Android:layout_height="wrap_content"
/>
<Button
Android:id="@+id/listpaireddevices"
Android:layout_width="fill_parent"
Android:layout_height="wrap_content"
Android:text="List Paired Devices"
Android:enabled="false"
/>
<TextView
Android:id="@+id/bluetoothstate"
Android:layout_width="fill_parent"
Android:layout_height="wrap_content"
/>
ListPairedDevicesActivity.Java
import Java.util.Set;
import Android.app.ListActivity;
import Android.bluetooth.BluetoothAdapter;
import Android.bluetooth.BluetoothClass;
import Android.bluetooth.BluetoothDevice;
import Android.content.Intent;
import Android.os.Bundle;
import Android.view.View;
import Android.widget.ArrayAdapter;
import Android.widget.ListView;
public class ListPairedDevicesActivity extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
ArrayAdapter<String> btArrayAdapter
= new ArrayAdapter<String>(this,
Android.R.layout.simple_list_item_1);
BluetoothAdapter bluetoothAdapter
= BluetoothAdapter.getDefaultAdapter();
Set<BluetoothDevice> pairedDevices
= bluetoothAdapter.getBondedDevices();
if (pairedDevices.size() > 0) {
for (BluetoothDevice device : pairedDevices) {
String deviceBTName = device.getName();
String deviceBTMajorClass
= getBTMajorDeviceClass(device
.getBluetoothClass()
.getMajorDeviceClass());
btArrayAdapter.add(deviceBTName + "\n"
+ deviceBTMajorClass);
}
}
setListAdapter(btArrayAdapter);
}
private String getBTMajorDeviceClass(int major){
switch(major){
case BluetoothClass.Device.Major.AUDIO_VIDEO:
return "AUDIO_VIDEO";
case BluetoothClass.Device.Major.COMPUTER:
return "COMPUTER";
case BluetoothClass.Device.Major.HEALTH:
return "HEALTH";
case BluetoothClass.Device.Major.IMAGING:
return "IMAGING";
case BluetoothClass.Device.Major.MISC:
return "MISC";
case BluetoothClass.Device.Major.NETWORKING:
return "NETWORKING";
case BluetoothClass.Device.Major.PERIPHERAL:
return "PERIPHERAL";
case BluetoothClass.Device.Major.PHONE:
return "PHONE";
case BluetoothClass.Device.Major.TOY:
return "TOY";
case BluetoothClass.Device.Major.UNCATEGORIZED:
return "UNCATEGORIZED";
case BluetoothClass.Device.Major.WEARABLE:
return "AUDIO_VIDEO";
default: return "unknown!";
}
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
Intent intent = new Intent();
setResult(RESULT_OK, intent);
finish();
}
}
AndroidBluetooth.Java
import Android.app.Activity;
import Android.bluetooth.BluetoothAdapter;
import Android.content.Intent;
import Android.os.Bundle;
import Android.view.View;
import Android.widget.Button;
import Android.widget.TextView;
public class AndroidBluetooth extends Activity {
private static final int REQUEST_ENABLE_BT = 1;
private static final int REQUEST_PAIRED_DEVICE = 2;
/** Called when the activity is first created. */
Button btnListPairedDevices;
TextView stateBluetooth;
BluetoothAdapter bluetoothAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnListPairedDevices = (Button)findViewById(R.id.listpaireddevices);
stateBluetooth = (TextView)findViewById(R.id.bluetoothstate);
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
CheckBlueToothState();
btnListPairedDevices.setOnClickListener(btnListPairedDevicesOnClickListener);
}
private void CheckBlueToothState(){
if (bluetoothAdapter == null){
stateBluetooth.setText("Bluetooth NOT support");
}else{
if (bluetoothAdapter.isEnabled()){
if(bluetoothAdapter.isDiscovering()){
stateBluetooth.setText("Bluetooth is currently in device discovery process.");
}else{
stateBluetooth.setText("Bluetooth is Enabled.");
btnListPairedDevices.setEnabled(true);
}
}else{
stateBluetooth.setText("Bluetooth is NOT Enabled!");
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
}
}
private Button.OnClickListener btnListPairedDevicesOnClickListener
= new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setClass(AndroidBluetooth.this, ListPairedDevicesActivity.class);
startActivityForResult(intent, REQUEST_PAIRED_DEVICE);
}};
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
if(requestCode == REQUEST_ENABLE_BT){
CheckBlueToothState();
}if (requestCode == REQUEST_PAIRED_DEVICE){
if(resultCode == RESULT_OK){
}
}
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:Android="http://schemas.Android.com/apk/res/Android"
package="com.test.AndroidBluetooth"
Android:versionCode="1"
Android:versionName="1.0">
<uses-sdk Android:minSdkVersion="7" />
<uses-permission Android:name="Android.permission.BLUETOOTH"></uses-permission>
<application Android:icon="@drawable/icon" Android:label="@string/app_name">
<activity Android:name=".AndroidBluetooth"
Android:label="@string/app_name">
<intent-filter>
<action Android:name="Android.intent.action.MAIN" />
<category Android:name="Android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity Android:name=".ListPairedDevicesActivity"
Android:label="AndroidBluetooth: List of Paired Devices"/>
</application>
</manifest>
package com.sekurtrack.myapplication;
import Android.bluetooth.BluetoothAdapter;
import Android.bluetooth.BluetoothDevice;
import Android.content.BroadcastReceiver;
import Android.content.Context;
import Android.content.Intent;
import Android.content.IntentFilter;
import Android.support.v7.app.AppCompatActivity;
import Android.os.Bundle;
import Android.util.Log;
import Android.widget.ArrayAdapter;
import Android.widget.ListView;
import Android.widget.Toast;
import Java.util.ArrayList;
import Java.util.Set;
public class MainActivity extends AppCompatActivity {
ListView listView;
private BluetoothAdapter BA;
private ArrayList<String> mDeviceList = new ArrayList<String>();
private Set<BluetoothDevice> pairedDevices;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView=(ListView)findViewById(R.id.devicesList);
BA = BluetoothAdapter.getDefaultAdapter();
BA.startDiscovery();
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mReceiver, filter);
/* BA = BluetoothAdapter.getDefaultAdapter();
pairedDevices = BA.getBondedDevices();
ArrayList list = new ArrayList();
for(BluetoothDevice bt : pairedDevices) list.add(bt.getName());
Toast.makeText(getApplicationContext(), "Showing Paired Devices",Toast.LENGTH_SHORT).show();
final ArrayAdapter adapter = new ArrayAdapter(this,Android.R.layout.simple_list_item_1, list);
listView.setAdapter(adapter);*/
}
@Override
protected void onDestroy() {
unregisterReceiver(mReceiver);
super.onDestroy();
}
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
BluetoothDevice device = intent
.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
mDeviceList.add(device.getName() + "\n" + device.getAddress());
Log.i("BT1", device.getName() + "\n" + device.getAddress());
listView.setAdapter(new ArrayAdapter<String>(context,
Android.R.layout.simple_list_item_1, mDeviceList));
}
}
};
}
このコードでは、ボタンクリックで呼び出すだけです。
private void list_paired_Devices() {
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
ArrayList<String> devices = new ArrayList<>();
for (BluetoothDevice bt : pairedDevices) {
devices.add(bt.getName() + "\n" + bt.getAddress());
}
ArrayAdapter arrayAdapter = new ArrayAdapter(bluetooth.this, Android.R.layout.simple_list_item_1, devices);
emp.setAdapter(arrayAdapter);
}