我是Android的新手。我创建了一个蓝牙模块,该模块可以打开/关闭蓝牙(单独的按钮),扫描附近的设备(使用单独的按钮)和显示配对的设备(单独的按钮)。现在,如果单击“扫描”,它将在列出的每个设备旁边显示一个设备列表,其中包括一个小对按钮和一个连接按钮。
我为配对和连接编写了代码,并且工作正常。现在,我想再次单击同一连接按钮来断开连接。连接到指定设备后,连接按钮内的文本应显示为“断开连接”。如果单击“连接”,则表明它正在连接设备,但未断开连接。
这是示例代码:
package net.londatiga.android.bluetooth;
import java.util.List;
import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.drm.DrmStore.Action;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
public class DeviceListAdapter extends BaseAdapter{
private LayoutInflater mInflater;
private List<BluetoothDevice> mData;
private OnPairButtonClickListener mListener;
private String str;
Button btnSend;
Connect con=null;
Button connectbtn;
//ProgressDialog mConnectingDlg;
public DeviceListAdapter(Context context) {
mInflater = LayoutInflater.from(context);
}
public void setData(List<BluetoothDevice> data) {
mData = data;
}
public void setListener(OnPairButtonClickListener listener) {
mListener = 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) {
final ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item_device, null);
holder = new ViewHolder();
holder.nameTv = (TextView) convertView.findViewById(R.id.tv_name);
holder.addressTv = (TextView) convertView.findViewById(R.id.tv_address);
holder.pairBtn = (Button) convertView.findViewById(R.id.btn_pair);
holder.connectbtn = (Button) convertView.findViewById(R.id.btn_connect);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
final BluetoothDevice device = mData.get(position);
holder.nameTv.setText(device.getName());
holder.addressTv.setText(device.getAddress());
holder.pairBtn.setText((device.getBondState() == BluetoothDevice.BOND_BONDED) ? "Unpair" : "Pair");
holder.pairBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mListener != null) {
mListener.onPairButtonClick(position);
}
}
});
// holder.connectbtn.setText((device.getBondState() == BluetoothDevice.BOND_BONDED) ? "Disonnect" : "connect");
holder.connectbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String s=holder.connectbtn.getText().toString();
Connect con=new Connect(new Connect.P25ConnectionListener() {
@Override
public void onStartConnecting() {
// TODO Auto-generated method stub
}
@Override
public void onDisconnected() {
// TODO Auto-generated method stub
}
@Override
public void onConnectionSuccess() {
// TODO Auto-generated method stub
}
@Override
public void onConnectionFailed(String error) {
// TODO Auto-generated method stub
}
@Override
public void onConnectionCancelled() {
// TODO Auto-generated method stub
}
});
con.connect(device);
System.out.println("Device is connected");
}
});
return convertView;
}
static class ViewHolder {
Button connectbtn;
TextView nameTv;
TextView addressTv;
Button pairBtn;
}
public interface OnPairButtonClickListener {
public abstract void onPairButtonClick(int position);
}
}
最佳答案
//Get bluetooth adapter
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
您想要在打开应用程序以通过此布尔值更改按钮状态(连接/断开)时检查状态
boolean bluetoothEnabled = mBluetoothAdapter.isEnabled();
如果要启用蓝牙
mBluetoothAdapter.enable();
如果要禁用蓝牙
mBluetoothAdapter.disable();
关于java - Android代码断开蓝牙,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37025909/