我正在开发一个应用程序.我需要显示设备是否已连接到另一个配对设备的状态.我能够显示Toast消息,但无法在List视图中显示蓝牙是否连接到其他设备.
你能否请一次查看我添加的代码并帮助我解决问题..
我的DeviceListActivity.class
mListView = (ListView) findViewById(R.id.lv_paired);
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
Set pairedDevices = mBluetoothAdapter.getBondedDevices();
mDeviceList = new ArrayList();
mDeviceList.addAll(pairedDevices);
mAdapter = new DeviceListAdapter(this);
mAdapter.setData(mDeviceList);
mAdapter.setListener(new OnConnectButtonClickListener() {
public void onConnectButtonClick(int position) {
BluetoothDevice device = mDeviceList.get(position);
ConnectDevice(device);
}
});
mListView.setAdapter(mAdapter);
registerReceiver(mConnectReceiver,new IntentFilter(BluetoothDevice.ACTION_ACL_CONNECTED));
registerReceiver(mConnectReceiver,new IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECTED));
}
private void ConnectDevice(BluetoothDevice device) {
try {
// SERIAL_UUID = device.getUuids()[0].getUuid();
// msocket = device.createInsecureRfcommSocketToServiceRecord(SERIAL_UUID);
Method method = device.getClass().getMethod("createRfcommSocket",new Class[] { int.class });
msocket = (BluetoothSocket) method.invoke(device,2);
msocket.connect();
} catch (Exception ex) {
ex.printStackTrace();
try {
msocket.close();
} catch (IOException closeEx) {
closeEx.printStackTrace();
}
return;
}
}
private final BroadcastReceiver mConnectReceiver = new BroadcastReceiver() {
public void onReceive(Context context,Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) {
showToast("BlueTooth is Connected");
} else if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)) {
showToast("BlueTooth DisConnected");
}
mAdapter.notifyDataSetChanged();
}
};
@Override
public void onDestroy() {
unregisterReceiver(mConnectReceiver);
super.onDestroy();
}
我的DeviceListAdapter类
public class DeviceListAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private List mData;
private OnConnectButtonClickListener connectListener;
public DeviceListAdapter(Context context) {
mInflater = LayoutInflater.from(context);
}
public void setData(List data) {
mData = data;
}
public void setListener(OnConnectButtonClickListener listener) {
connectListener = listener;
}
public int getCount() {
return (mData == null) ? 0 : mData.size();
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return position;
}
public View getView(final int position,View convertView,ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item_device,parent,false);
holder = new ViewHolder();
holder.nameTv = (TextView) convertView.findViewById(R.id.tv_name);
holder.addressTv = (TextView) convertView.findViewById(R.id.tv_address);
holder.connectBtn = (Button) convertView.findViewById(R.id.btn_connect);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
BluetoothDevice device = mData.get(position);
holder.nameTv.setText(device.getName());
holder.addressTv.setText(device.getAddress());
holder.connectBtn.setText("connect");
holder.connectBtn.setText("connected");
holder.connectBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (connectListener != null) {
connectListener.onConnectButtonClick(position);
}
}
});
return convertView;
}
static class ViewHolder {
TextView nameTv;
TextView addressTv;
Button connectBtn;
}
public interface OnConnectButtonClickListener {
public abstract void onConnectButtonClick(int position);
}
现在通过使用寄存器接收器,当点击连接时,我需要更新列表项的状态为已连接.
这个问题的任何帮助可以节省我的一天.提前致谢.