I am developing one application. I need to show whether the device is connected or not to another paired device. I can show a toast message, but I can’t show whether Bluetooth is connected to another device or not in the List view.
Can you see my added code once and help me with this issue.
my 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 {
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();
}
My class is 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);
}
Now, using the receiver, when I click on the connection, I need to update the status of the list item as connected.
Any help on this could save my day. Thanks in advance.
source
share