Android 蓝牙BlueTooth(开关搜索 发送各种数据操作)

首先写蓝牙我们需要的权限 <uses-permission

<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />   
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />   位置

固定UUID UUID uuid = UUID.fromString(“00001106-0000-1000-8000-00805F9B34FB”);

布局
在这里插入图片描述
布局代码

// An highlighted block
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/btn1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="开启蓝牙"/>
    <Button
        android:id="@+id/btn2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="搜索蓝牙"
        android:layout_marginTop="40dp"/>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rv1"
        android:layout_width="match_parent"
        android:layout_height="430dp"
        android:layout_marginTop="100dp"></android.support.v7.widget.RecyclerView>



</RelativeLayout>

Java代码

开启

注意蓝牙我们这里用了隐式意图
使用隐士意图打开蓝牙
BluetoothAdapter.ACTION_REQUEST_ENABLE:请求打开本设备蓝牙
BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE:允许本设备蓝牙被扫描
BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION:允许本设备蓝牙被扫描时长
BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE:允许本设备蓝牙被扫描
BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION:允许本设备蓝牙被扫描时长

// An highlighted block
    Intent intent = new Intent();
                intent.setAction(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                intent.setAction(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
                intent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION,3000);  //后面是你蓝牙能被别人搜索到的时间这里就是3000秒
                startActivityForResult(intent,303);

关闭

bluetoothAdapter.disable()
适配器对象.disable();

搜索

开启广播判断是否搜索到

 class MyDemo extends BroadcastReceiver{
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();

            if(action == BluetoothAdapter.ACTION_DISCOVERY_FINISHED){
                Toast.makeText(context, "搜索完毕", Toast.LENGTH_SHORT).show();
            }else if(action == BluetoothDevice.ACTION_FOUND){
                bluetoothDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                Log.d("地址",bluetoothDevice.getAddress());
                arrayList.add(bluetoothDevice);

                mAdapter.notifyDataSetChanged();
            }
        }
    }

上面java代码 展示到ReclerView控件上

IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(BluetoothDevice.ACTION_FOUND);
        intentFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
        mAdapter = new MAdapter(MainActivity.this, arrayList);
        mlv1.setAdapter(mAdapter);
        mlv1.setLayoutManager(new LinearLayoutManager(this));
        mAdapter.setMyclick(this);

        registerReceiver(myDemo,intentFilter);

适配器代码

package com.example.machenike.myapplication;

import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;

public class MAdapter extends RecyclerView.Adapter<MAdapter.MMAdapter> {

    Context context;
    ArrayList<BluetoothDevice> arrayList;

    public MAdapter(Context context, ArrayList<BluetoothDevice> arrayList) {
        this.context = context;
        this.arrayList = arrayList;
    }

    @NonNull
    @Override
    public MMAdapter onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View inflate = LayoutInflater.from(context).inflate(R.layout.m1hang, null);
        return new MMAdapter(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull MMAdapter mmAdapter, final int i) {
        mmAdapter.m1tv1.setText(arrayList.get(i).getAddress()+"\t"+arrayList.get(i).getName());

       mmAdapter.itemView.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               tt.show(i);
           }
       });

       mmAdapter.itemView.setOnLongClickListener(new View.OnLongClickListener() {
           @Override
           public boolean onLongClick(View v) {
               tt.show1(i);

               return true;
           }
       });
    }

    @Override
    public int getItemCount() {
        return arrayList.size();
    }

    class MMAdapter extends RecyclerView.ViewHolder{

        TextView m1tv1;

        public MMAdapter(@NonNull View itemView) {
            super(itemView);
            m1tv1 = itemView.findViewById(R.id.m1tv1);
        }
    }

    interface TT{
        void show(int i);
        void show1(int i);
    }

    TT tt;

    public void setTt(TT tt) {
        this.tt = tt;
    }
}

适配器里面有点击事件和长按点击事件

点击事件配对

 @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    @Override
    public void show(int i) {
        arrayList.get(i).createBond();
    }
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值