蓝牙设备

蓝牙
一. 是一种无线技术标准,可实现固定设备、移动设备和楼宇个人域网之间的短距离数据交换

添加权限

	    <uses-permission android:name="android.permission.BLUETOOTH" />
    	<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

设置蓝牙允许被搜索到

Intent intent = new Intent();
intent.setAction(BluetoothAdapter.ACTION_REQUEST_ENABLE);//开启蓝
intent.setAction(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);//允许蓝牙被搜索
intent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION,200);//设置允许被搜索时间200s内可以被搜索到
startActivityForResult(intent,100);

bluetoothAdapter.disable();关闭蓝牙
bluetoothAdapter.startDiscovery();//搜索附近的蓝牙
bluetoothManager = (BluetoothManager) getSystemService(BLUETOOTH_SERVICE);//获得蓝牙管理器

关闭蓝牙
适配器disable()
配对蓝牙
适配器startDiscovery()
搜索蓝牙
适配器.startDiscovery()
常用类
BluetoothManager 蓝牙管理类 管理bluetoothAdapter 管理蓝牙连接
BluetppthAdaper 蓝牙适配器 代表本蓝牙
BluetoothDevice 蓝牙设备是连接的那个设备
BluetoothServiceSocket服务器
BluetoothSocket客户端

蓝牙的传输功能的UUID是:00001101-0000-1000-8000-00805F9B34FB
UUID.fromString(“00001101-0000-1000-8000-00805F9B34FB”)类型转换

代码


package com.example.day9.Lesson2;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.os.Parcelable;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.example.day9.Lesson1.ConnectionManager;
import com.example.day9.R;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;


public class Main3Activity extends AppCompatActivity implements View.OnClickListener {

    Button button1, button2, button3;
    RecyclerView recyclerView;
    BluetoothDevice device;
    BluetoothAdapter adapter;
    BluetoothManager manager;
    MyAdapter myAdapter;
    ArrayList<BluetoothDevice> devices = new ArrayList<>();

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);

        init();
    }

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
    private void init() {
        button1 = findViewById(R.id.kailanya);
        button2 = findViewById(R.id.guanlanya);
        button3 = findViewById(R.id.soulanya);
        button1.setOnClickListener(this);
        button2.setOnClickListener(this);
        button3.setOnClickListener(this);
        recyclerView = findViewById(R.id.recycler);
        manager = (BluetoothManager) getSystemService(BLUETOOTH_SERVICE);
        adapter = manager.getAdapter();
        if (adapter == null) {
            finish();
            return;
        }
        BluetoothReceiver bluetoothReceiver = new BluetoothReceiver();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(BluetoothDevice.ACTION_FOUND);
        intentFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
        intentFilter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
        registerReceiver(bluetoothReceiver, intentFilter);
        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(manager);
        myAdapter = new MyAdapter(Main3Activity.this, devices);
        recyclerView.setAdapter(myAdapter);
        myAdapter.setItemClick(new MyAdapter.ItemClick() {
            @Override
            public void onclick(int position) {
                try {
                    Method createBond = BluetoothDevice.class.getMethod("createBond");
                    createBond.invoke(devices.get(position));
                } catch (NoSuchMethodException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.kailanya:
                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.guanlanya:
                adapter.disable();
                break;
            case R.id.soulanya:
                adapter.startDiscovery();
                break;
            default:
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 100 && resultCode == RESULT_OK) {
            Log.e("###", "打开蓝牙成功");
        }
    }

    class BluetoothReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            switch (action) {
                case BluetoothDevice.ACTION_FOUND:
                    device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                    devices.add(device);
                    myAdapter.notifyDataSetChanged();
                    break;
                case BluetoothAdapter.ACTION_DISCOVERY_FINISHED:
                    myAdapter.notifyDataSetChanged();
                    break;
                case BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED:
                    int bondState = device.getBondState();
                    switch (bondState) {
                        case BluetoothDevice.BOND_BONDED:
                            ConnectionManager connectionManager = new ConnectionManager(device);
                            break;
                        case BluetoothDevice.BOND_BONDING:
                            break;
                        case BluetoothDevice.BOND_NONE:

                            break;
                        default:
                            break;
                    }
                    break;
            }
        }
    }
}


猜你喜欢

转载自blog.csdn.net/weixin_45038475/article/details/92846299