客户端
蓝牙的四个基本功能
<?xml version="1.0" encoding="utf-8"?>
<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:orientation="vertical"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="50dp">
<Button
android:id="@+id/open"
android:text="打开蓝牙"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent" />
<Button
android:id="@+id/close"
android:text="关闭蓝牙"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent" />
<Button
android:id="@+id/find"
android:text="搜索"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent" />
<Button
android:id="@+id/show"
android:text="已匹配"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent" />
</LinearLayout>
<TextView
android:text="周围设备,点击进行连接"
android:gravity="center"
android:textSize="20sp"
android:textColor="#000"
android:background="#00BCD4"
android:layout_width="match_parent"
android:layout_height="30dp" />
<android.support.v7.widget.RecyclerView
android:id="@+id/find_view"
android:layout_width="match_parent"
android:layout_height="200dp"></android.support.v7.widget.RecyclerView>
<TextView
android:text="已匹配设备,点击进行数据传输"
android:gravity="center"
android:textSize="20sp"
android:textColor="#000"
android:background="#00BCD4"
android:layout_width="match_parent"
android:layout_height="30dp" />
<android.support.v7.widget.RecyclerView
android:id="@+id/show_view"
android:layout_width="match_parent"
android:layout_height="match_parent"></android.support.v7.widget.RecyclerView>
</LinearLayout>
package com.example.day1107_homework;
import android.Manifest;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothManager;
import android.bluetooth.BluetoothSocket;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.example.day1107_homework.listener.MyBlueListener;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Set;
import java.util.UUID;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private String[] PERMISSIONS = {
Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.BLUETOOTH, Manifest.permission.BLUETOOTH_ADMIN,Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.WRITE_EXTERNAL_STORAGE};
private int CODE = 100;
private Button open;
private Button close;
private Button find;
private Button show;
private RecyclerView find_view;
private RecyclerView show_view;
public static BluetoothSocket bluetoothSocket;
private MyR myR;
private MyBlueAdapter myBlueAdapter1;
private ArrayList<BluetoothDevice> list_find = new ArrayList<>();
private MyBlueAdapter myBlueAdapter2;
private ArrayList<BluetoothDevice> list_show = new ArrayList<>();
private UUID uuid = UUID.fromString("00001106-0000-1000-8000-00805F9B34FB");
private BluetoothManager bluetoothManager;
private BluetoothAdapter bluetoothAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
myBlueAdapter1 = new MyBlueAdapter(list_find,this);
find_view.setAdapter(myBlueAdapter1);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
find_view.se