android 显示附近蓝牙,如何在android中显示蓝牙连接状态

开发者在创建应用程序时遇到一个问题,即无法在ListView中显示蓝牙设备的连接状态。当前代码能够显示Toast消息,但无法在列表视图中更新设备是否已连接到其他设备。已经注册了BroadcastReceiver来监听蓝牙设备的连接和断开事件,并尝试在事件发生时通过notifyDataSetChanged()更新列表。问题在于如何将连接状态正确地反映在ListView的列表项上。
摘要由CSDN通过智能技术生成

我正在开发一个应用程序.我需要显示设备是否已连接到另一个配对设备的状态.我能够显示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);

}

现在通过使用寄存器接收器,当点击连接时,我需要更新列表项的状态为已连接.

这个问题的任何帮助可以节省我的一天.提前致谢.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值