Bluetooth devices

Bluetooth
I. is a wireless technology standard, short distance data exchange between stationary devices, mobile devices, and Personal Area Network Building

Add Permissions

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

Bluetooth allows the setting to be searched

Intent new new = the Intent the Intent ();
intent.setAction (BluetoothAdapter.ACTION_REQUEST_ENABLE); // open blue
intent.setAction (BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE); // be searched Bluetooth allows
intent.putExtra (BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 200); // Set search time can be allowed to be searched into 200S
the startActivityForResult (Intent, 100);

bluetoothAdapter.disable (); Turn off Bluetooth
bluetoothAdapter.startDiscovery (); // search for nearby Bluetooth
bluetoothManager = (BluetoothManager) getSystemService (BLUETOOTH_SERVICE ); // get Bluetooth Manager

Turn off Bluetooth
adapter disable ()
paired Bluetooth
adapter startDiscovery ()
to search for Bluetooth
adapter .startDiscovery ()
commonly used class
BluetoothManager Bluetooth management class management bluetoothAdapter manage Bluetooth connections
BluetppthAdaper on behalf of the Bluetooth adapter Bluetooth
BluetoothDevice Bluetooth device that is connected
BluetoothServiceSocket server
BluetoothSocket client

UUID Bluetooth transmission functions are: 00001101-0000-1000-8000-00805F9B34FB
UUID.fromString ( "00001101-0000-1000-8000-00805F9B34FB") type conversion

Code


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;
            }
        }
    }
}


Guess you like

Origin blog.csdn.net/weixin_45038475/article/details/92846299