はい、私はVishwaに同意します、Bluetoothチャットアプリケーションは非常に役立ちます、私は同じ種類の開発を行いました、ここに私のコードがあります、
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:id="@+id/title_new_devices"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:background="#666" android:textColor="#fff"
android:paddingLeft="5dp" />
<ListView android:id="@+id/new_devices" android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
アクティビティクラス
private static BluetoothAdapter mBtAdapter;
private static ArrayAdapter<String> mNewDevicesArrayAdapter;
private static ArrayList<String> nameList = null;
private static ArrayList<String> macList = null;
private static ListView newDevicesListView = null;
// Register for broadcasts when a device is discovered
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
this.registerReceiver(mReceiver, filter);
// Register for broadcasts when discovery has finished
filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
this.registerReceiver(mReceiver, filter);
filter = new IntentFilter( BluetoothAdapter.ACTION_DISCOVERY_STARTED );
this.registerReceiver( mReceiver, filter );
// Get the local Bluetooth adapter
mBtAdapter = BluetoothAdapter.getDefaultAdapter();
BroadCastReceiverクラス
private final BroadcastReceiver mReceiver = new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent)
{
try
{
String action = intent.getAction();
// When discovery finds a device
if ( BluetoothDevice.ACTION_FOUND.equals(action) )
{
// Get the BluetoothDevice object from the Intent
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
String deviceName = device.getName();
String deviceAddress = device.getAddress();
if ( !nameList.contains( deviceName.trim() + " " + deviceAddress.trim() ) )
{
nameList.add( deviceName.trim() + " " + deviceAddress.trim() );
macList.add( deviceAddress.trim() + "&" + ConstantCodes.TIME + ConstantCodes.EQUALS + DateUtility.getDateTime() );
}
// System.out.println ( device.getName() );
mNewDevicesArrayAdapter.notifyDataSetChanged();
}
else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action))
{
mNewDevicesArrayAdapter.notifyDataSetChanged();
}
else if ( BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals( action ) )
{
// nameList.clear();
}
}
catch ( Exception e )
{
System.out.println ( "Broadcast Error : " + e.toString() );
}
}
};
コードはアクティブなBluetoothデバイスを検索し、それをlistViewに表示します。