蓝牙发送、接收消息

客户端

蓝牙的四个基本功能

<?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
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值