Android蓝牙连接设备
我不知道如何通过以下代码将我的绑定设备列入列表:Android蓝牙连接设备,android,textview,android-arrayadapter,Android,Textview,Android Arrayadapter,我不知道如何通过以下代码将我的绑定设备列入列表: bt_scan.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices(); // If there are paired de
bt_scan.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
// If there are paired devices
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());
}
}
}});}
bt_scan.setOnClickListener(新视图.OnClickListener(){
公共void onClick(视图v){
设置pairedDevices=mBluetoothAdapter.getBondedDevices();
//如果有配对的设备
如果(pairedDevices.size()>0){
//循环通过配对设备
用于(蓝牙设备:pairedDevices){
//将名称和地址添加到要在列表视图中显示的阵列适配器
mArrayAdapter.add(device.getName()+“\n”+device.getAddress());
}
}
}});}
我不知道如何在文本视图或列表视图中获得相同的布局,如果可能的话,甚至在另一个布局中。
我想连接一个找到的设备,但那完全是另一回事
(如果有帮助,下面是我的完整代码:)
非常感谢 查看Android SDK中提供的蓝牙聊天应用程序,它提供了一种显示当前配对手机的干净方式
它还提供了一个BluetoothChatService类,您可以轻松地调整该类以连接找到的设备。是的,我同意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>
代码将搜索活动的蓝牙设备并将其显示在listView中。如果只想将arrayAdapter结果显示在listView中,只需将arrayAdapter设置为listView控件即可。我做了一个简单的视频,介绍如何通过适配器将项目添加到列表视图中,您可以或多或少地跳到第4分钟。
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();
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() );
}
}
};