Bluetooth is turned on and turned off the search
Layout file
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".Demo">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/start"
android:text="开启"
android:onClick="blueTooth"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/shou"
android:text="搜索"
android:onClick="blueTooth"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/stop"
android:text="关闭"
android:onClick="blueTooth"
/>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/rlist"
></android.support.v7.widget.RecyclerView>
</LinearLayout>
Master classes and class Bean
Bean class
public class Bean {
private String name;
private String address;
private BluetoothDevice device;
public Bean(String name, String address, BluetoothDevice device) {
this.name = name;
this.address = address;
this.device = device;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public BluetoothDevice getDevice() {
return device;
}
public void setDevice(BluetoothDevice device) {
this.device = device;
}
}
The main class
public class Demo extends AppCompatActivity {
MyAdapter adapter;
RecyclerView rlist;
List<Bean> lists = new ArrayList<>();
BluetoothAdapter blueadapter;
BluetoothManager manager;
BlueBluetoothReceiver receiver;
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_demo);
initView();
initData();
}
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
private void initData() {
manager = (BluetoothManager) getSystemService(BLUETOOTH_SERVICE);
blueadapter = manager.getAdapter();
if(blueadapter == null){
finish();
return;
}
receiver = new BlueBluetoothReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction(BluetoothDevice.ACTION_FOUND);//可发现
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);//搜索结束
filter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);//绑定状态
registerReceiver(receiver,filter);
}
private void initView() {
rlist = findViewById(R.id.rlist);
LinearLayoutManager manager = new LinearLayoutManager(this);
manager.setOrientation(LinearLayoutManager.VERTICAL);
rlist.setLayoutManager(manager);
adapter = new MyAdapter(this, (ArrayList<Bean>) lists);
rlist.setAdapter(adapter);
}
public void blueTooth(View view) {
switch (view.getId()){
case R.id.start:
boolean enable = blueadapter.enable();
if(!enable){
Intent intent = new Intent();
intent.setAction(BluetoothAdapter.ACTION_REQUEST_ENABLE);//启动蓝牙
intent.setAction(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);//开启设备可发现
intent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION,200);//设定搜索设备时间
startActivityForResult(intent,100);
}
break;
case R.id.shou:
blueadapter.startDiscovery();//开启搜索
break;
case R.id.stop:
blueadapter.disable();//关闭蓝牙
lists.clear();//清空集合
adapter.notifyDataSetChanged();//刷新适配器
break;
default:
break;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 100){
if(resultCode == RESULT_OK){
Toast.makeText(this, "开启蓝牙", Toast.LENGTH_SHORT).show();
}
}
}
class BlueBluetoothReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
switch (action){
case BluetoothDevice.ACTION_FOUND:
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
String name = device.getName();
String address = device.getAddress();
Bean bean = new Bean(name,address,device);
lists.add(bean);
adapter.notifyDataSetChanged();
break;
case BluetoothAdapter.ACTION_DISCOVERY_FINISHED:
adapter.notifyDataSetChanged();
break;
default:
break;
}
}
}
}
Adapter class
Adapter class
class MyAdapter extends RecyclerView.Adapter<MyAdapter.RHolder>{
Context context;
ArrayList<Bean> lists;
public MyAdapter(Context context, ArrayList<Bean> lists) {
this.context = context;
this.lists = lists;
}
@NonNull
@Override
public RHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(context).inflate(R.layout.item,viewGroup,false);
return new RHolder(view);
}
@Override
public void onBindViewHolder(@NonNull RHolder rHolder, int i) {
rHolder.name.setText(lists.get(i).getName());
rHolder.address.setText(lists.get(i).getAddress());
}
@Override
public int getItemCount() {
return lists.size();
}
class RHolder extends RecyclerView.ViewHolder{
TextView name;
TextView address;
public RHolder(@NonNull View itemView) {
super(itemView);
name = itemView.findViewById(R.id.name);
address = itemView.findViewById(R.id.address);
}
}
}
layout
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/name"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/address"
/>
</LinearLayout>
Renderings