我有四个蓝牙设备用于测试获取蓝牙设备名称和地址,分别称为 deviceA , deviceB, deviceC & deviceD
我的程序可以通过 mReceiver 一个一个获取设备,但是 SimpleAdapter 数组 listrow 有问题。当我扫描新设备时,所有行都将更改为相同的名称和地址。我找不到问题在哪里。请帮忙,谢谢。
我的问题例如:
首先扫描设备A时,Result是
设备A 地址A
second 当扫描到 deviceB 时,Result 为
设备 B 地址 B
设备 B 地址 B
第三,扫描deviceC时,Result是deviceC AddressC
设备C 地址C
设备C 地址C
第四,再次扫描deviceB时,Result为
设备 B 地址 B
设备 B 地址 B
设备 B 地址 B
设备 B 地址 B
扫描 deviceD 时,结果为
设备 D 地址 D
设备 D 地址 D
设备 D 地址 D
设备 D 地址 D
设备 D 地址 D
这是我的代码:
private static BluetoothAdapter mBluetoothAdapter = null;
private static BluetoothSocket mBluetoothSocket = null;
private ListView listView;
private ArrayList<Map<String,Object>> list = null;
private SimpleAdapter adapter = null;
private Map<String,Object> hashmap = new HashMap<String,Object>();
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.getbluetooth);
listView = (ListView) findViewById(R.id.listView1);
list = new ArrayList<Map<String,Object>>();
find_bluetooth();
}
public void find_bluetooth()
{
Set<BluetoothDevice> setPairedDevices = mBluetoothAdapter.getBondedDevices();
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mReceiver, filter);
adapter = new impleAdapter(Diver_getEvents.this,list,R.layout.bluetooth_list,new String[]{"devicename","deviceaddress"},new int[]{R.id.devicename,R.id.deviceaddress});
listView.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> arg0, View v, int position,long id)
{
mBluetoothAdapter.cancelDiscovery();
}
}
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);
String deviceBTName = device.getName();
String deviceBaddress = device.getAddress();
hashmap.put("devicename",deviceBTName);
hashmap.put("deviceaddress",deviceBaddress);
list.add(hashmap);
listView.setAdapter(adapter);
}
}
}
}