java - Android代码断开蓝牙

标签 java android bluetooth

我是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/

相关文章:

java - 基于文件的进程间通信的 EOFException

android - ListView 中的应用内结算工作流

java - 我试图在这里移动 SDK 中获取手势,但不明白要传递给手势方法的内容是什么?如何解决这个问题

android - Android和Ubuntu之间的蓝牙串行连接

java - Maven、Spring Boot - 包依赖问题、集成测试

java - 如何为 Web 应用程序设置 tomcat 上下文路径? .war 文件名位于 url 中应用程序名称的前面

java - MongoDB 和 Java : Query doesn't return results

android - 如何设置大文本以动态提示textinputlayout?

c++ - Qt蓝牙无法将套接字连接到设备

android 蓝牙配对请求