我正在开发一款应用。我需要显示设备是否已连接到另一个配对设备的状态。我能够显示toast消息,但无法在列表视图中显示蓝牙是否连接到其他设备。
您能否请一次查看我添加的代码,并帮助我解决问题..
我的DeviceListActivity.class
mListView = (ListView) findViewById(R.id.lv_paired);
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
mDeviceList = new ArrayList<BluetoothDevice>();
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<BluetoothDevice> mData;
private OnConnectButtonClickListener connectListener;
public DeviceListAdapter(Context context) {
mInflater = LayoutInflater.from(context);
}
public void setData(List<BluetoothDevice> 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);
}
现在通过使用注册接收器,当点击连接时,我需要更新列表项的状态为已连接。
此问题的任何帮助都可以节省我的一天。提前谢谢。
答案 0 :(得分:0)
我认为您需要更改存储数据的方式。 BluetoothDevice
允许您使用getBondState()
检查您的绑定状态,但是如果您查看文档,则不一定会有所帮助:
与远程设备绑定(配对)并不一定意味着设备当前已连接。它只是意味着挂起的过程在较早的时间完成,并且链接密钥仍然存储在本地,准备在下一个连接上使用。
您可能希望围绕BluetoothDevice
创建一个简单的包装器,您可以设置连接状态。
class BluetoohInfo {
BluetoothDevice device;
boolean connected;
BluetoothInfo(BluetoothDevice device, boolean connected) {
this.device = device;
this.connected = connected;
}
public boolean isConnected() {
return connected;
}
// getters and setters of your choosing
}
现在,不要在Toast
内显示BroadcastReceiver
,只需将BluetoothInfo
的连接属性设置为true
。
然后,将mDeviceList
列为BluetoothInfo
,以便他们支持ListView
,而不仅仅是BluetoothDevice
的原始列表:
mDeviceList = new ArrayList<BluetoothInfo>();
在列表适配器的getView()
内,检查您的已连接属性。
if (bluetoothInfo.isConnected()) {
holder.connectBtn.setText("connected");
} else {
holder.connectBtn.setText("connect");
}
通过这样做,接收者内部的notifyDataSetChanged()
应该使这些变化有效。