获取已配对的蓝牙

package com.example.administrator.blue.demo2;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.example.administrator.blue.R;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
public class PairBlueActivity extends AppCompatActivity {
    private TextView show_tv;
    private CheckBox cb;
    private ListView lv;
    private BluetoothAdapter defaultAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pair_blue);
        show_tv = (TextView) findViewById(R.id.show_tv);
        cb = (CheckBox) findViewById(R.id.show_cb);
        lv = (ListView) findViewById(R.id.show_lv);

        cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean ischae) {

                if (ischae)
                {
                    if (defaultAdapter.getScanMode()!=defaultAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE);

                    Intent intent =new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
                    intent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION,200);
                    startActivity(intent);
                }
            }
        });
        chackBluetooth();
    }
    private void chackBluetooth() {
        defaultAdapter = BluetoothAdapter.getDefaultAdapter();
        if (defaultAdapter==null) {
            show_tv.setText("本机不支持蓝牙功能");
        return;
        }
        if (!defaultAdapter.isEnabled()) {
            show_tv.setText("有蓝牙功能,还没打开");
            AlertDialog.Builder builder =new AlertDialog.Builder(this);

            builder.setTitle("提示");
            builder.setMessage("蓝牙设备没打开,是否打开");
            builder.setNegativeButton("取消",null);
            builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    Intent intent =new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                    startActivityForResult(intent,100);
                }
            });
            builder.show();

        }else {
            show_tv.setText("蓝牙已开启");
        }
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode==100&&resultCode== Activity.RESULT_OK) {
            Toast.makeText(this,"蓝牙已开启",Toast.LENGTH_SHORT).show();
            show_tv.setText("蓝牙已开启");
        }
    }
    public void onClick(View view) {
            getBondBluetooth();
    }
    private void getBondBluetooth() {
        Set<BluetoothDevice> bondedDevices = defaultAdapter.getBondedDevices();


        List<String> list =new ArrayList<>();

        for (BluetoothDevice bond:bondedDevices) {

            String msg ="设备名称"+bond.getName()+"\n设备地址"+bond.getAddress();
            list.add(msg);
        }
        ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, list);

        lv.setAdapter(adapter);


    }


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值