【问题标题】:How to Show bluetooth Connection status in android如何在android中显示蓝牙连接状态
【发布时间】:2015-12-29 11:07:12
【问题描述】:

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

}

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

在这个问题上的任何帮助都可以挽救我的一天。提前致谢。

【问题讨论】:

  • 另外,您是否在实际显示列表视图中的状态时遇到了问题,或者您是否试图获得有关指示连接的方法的建议?
  • 是的,我在显示连接状态时遇到问题,即列表中的每个列表项是否已连接。
  • 好吧,看看我的回答。如果我理解它们,我认为它适合您的需求。
  • @lase,我根据您的答案进行了计算,但无法正确理解。您能否对我的问题进行编辑。

标签: android android-activity android-bluetooth


【解决方案1】:

我认为您需要改变存储数据的方式。 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
}

现在,不要在BroadcastReceiver 中显示Toast,只需将BluetoothInfo 的connected 属性设置为true

然后,将mDeviceList 设为BluetoothInfo 的列表,以便他们支持您的ListView,而不仅仅是BluetoothDevice 的原始列表:

mDeviceList = new ArrayList<BluetoothInfo>();

在您的列表适配器的getView() 内部,检查您连接的属性。

if (bluetoothInfo.isConnected()) {
    holder.connectBtn.setText("connected");
} else {
    holder.connectBtn.setText("connect");
}

通过这样做,接收器内部的notifyDataSetChanged() 应该使这些更改生效。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-03-12
    • 1970-01-01
    • 2014-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多