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); } }
获取已配对的蓝牙
最新推荐文章于 2025-06-05 12:36:34 发布