美文网首页
Android传统蓝牙实现多连接,消息发送与接收

Android传统蓝牙实现多连接,消息发送与接收

作者: 路人甲_47a3 | 来源:发表于2020-12-11 17:04 被阅读0次

    传统蓝牙适用于较为耗电的操作,其中包括 Android 设备之间的流式传输和通信等。针对具有低功耗要求的蓝牙设备,Android 4.3(API 级别 18)中引入了面向低功耗蓝牙的 API 支持。因为项目需求,编写了关于传统蓝牙的demo,实现了蓝牙多连,蓝牙间信息发送与接收,有不对的地方,望指导,大家一起学习,谢谢!

    大概效果如下:


    安卓传统蓝牙多连接,消息接送#戏精上身的我#_腾讯视频

    go!go!go!

    首先权限问题

    如要在应用中使用蓝牙功能,您必须声明两个权限。第一个是 BLUETOOTH。您需要此权限才能执行任何蓝牙通信,例如请求连接、接受连接和传输数据等。

    第二个必须声明的权限是 ACCESS_FINE_LOCATION。您的应用需要此权限,因为蓝牙扫描可用于收集用户的位置信息。此类信息可能来自用户自己的设备,以及在商店和交通设施等位置使用的蓝牙信标。

    如果您想让应用启动设备发现或操纵蓝牙设置,则除了 BLUETOOTH 权限以外,您还必须声明 BLUETOOTH_ADMIN 权限。大多数应用只是需利用此权限发现本地蓝牙设备。除非应用是根据用户请求修改蓝牙设置的“超级管理员”,否则不应使用此权限所授予的其他功能。

    在您的应用清单文件中声明蓝牙权限。例如:

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

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

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

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

    1.蓝牙帮助类 BlueToothUtils

    public class BlueToothUtils implements BlueToothUtilsBroadcastReceiver.BlueToothUtilsBroadcastReceiverListener {

    private static BlueToothUtilsblueToothUtils;

        public synchronized static BlueToothUtilsgetInstance(Activity activity) {

    if (null ==blueToothUtils) {

    blueToothUtils =new BlueToothUtils(activity);

            }

    return blueToothUtils;

        }

    private BluetoothAdapterbluetoothAdapter;

        private BluetoothServerSocketmmServerSocket;

        private Threadthread;

        private boolean isRunning =false;

        private final Stringlock ="lock";

        private final Stringlock2 ="lock2";

        private BlueToothUtilsBroadcastReceiverblueToothUtilsBroadcastReceiver;

        private MapstringBluetoothSocketMap;

        private BlueToothUtilsListenerlistener;

        private StringIMEI;

        private Activitycontext;

        public BlueToothUtils(Activity context) {

    this.context = context;

            bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

            stringBluetoothSocketMap =new HashMap<>();

            IMEI =bluetoothAdapter.getName();

            registerBlueStateReceiver();

            if (null !=bluetoothAdapter) {

    try {

    mmServerSocket =bluetoothAdapter.listenUsingRfcommWithServiceRecord(IMEI, UUID.nameUUIDFromBytes(IMEI.getBytes()));

                }catch (IOException e) {

    e.printStackTrace();

                }

    }

    }

    private void registerBlueStateReceiver() {

    blueToothUtilsBroadcastReceiver =new BlueToothUtilsBroadcastReceiver();

            blueToothUtilsBroadcastReceiver.setOnBlueToothUtilsBroadcastReceiverListener(this);

            IntentFilter intentFilter =new IntentFilter();

            // 监视蓝牙关闭和打开的状态

            intentFilter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);

            // 监视蓝牙设备与APP连接的状态

            intentFilter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);

            intentFilter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED);

            // 注册广播

            context.registerReceiver(blueToothUtilsBroadcastReceiver, intentFilter);

        }

    public void start() {

    synchronized (lock) {

    if (null ==mmServerSocket) {

    return;

                }

    if (isRunning)return;

                isRunning =true;

                thread =new Thread(() -> {

    while (isRunning) {

    try {

    BluetoothSocket socket =mmServerSocket.accept();

                            BluetoothDevice remoteDevice = socket.getRemoteDevice();

                            String name = remoteDevice.getName();

                            addSocket(name, socket);

                            receiveMessage(name);

                        }catch (IOException e) {

    e.printStackTrace();

                        }

    }

    });

                thread.start();

            }

    }

    public void receiveMessage(String name) {

    synchronized (lock2) {

    if (null ==stringBluetoothSocketMap)return;

                if (!stringBluetoothSocketMap.containsKey(name))return;

                BlueToothEntity blueToothEntity =stringBluetoothSocketMap.get(name);

                if (null == blueToothEntity)return;

                if (!blueToothEntity.isRunning()) {

    blueToothEntity.startListener();

                }

    }

    }

    public void addSocket(String key, BluetoothSocket socket) {

    synchronized (lock2) {

    if (TextUtils.isEmpty(key) ||null == socket)return;

                if (null ==stringBluetoothSocketMap) {

    stringBluetoothSocketMap =new HashMap<>();

                }

    stringBluetoothSocketMap.put(key, new BlueToothEntity(socket, listener));

            }

    }

    public void sendMessage(String message) {

    synchronized (lock2) {

    if (null ==stringBluetoothSocketMap ||stringBluetoothSocketMap.size() ==0)return;

                Log.e("阿斯达所asdas0", "sendMessage");

                for (BlueToothEntity blueToothEntity :stringBluetoothSocketMap.values()) {

    blueToothEntity.sendMessage(message);

                }

    }

    }

    public void connect(BluetoothDevice device) {

    MyThreadPool.getInstance().newThread(() -> {

    try {

    String name =device.getName().trim();

                    BluetoothSocket tmp =device.createRfcommSocketToServiceRecord(UUID.nameUUIDFromBytes(name.getBytes()));

                    tmp.connect();

                    addSocket(name, tmp);

                    receiveMessage(name);

                }catch (IOException e) {

    e.printStackTrace();

                }

    });

        }

    public void setOnBlueToothUtilsListener(BlueToothUtilsListener listener) {

    this.listener = listener;

        }

    @Override

        public void onBlueToothClose(int blueToothSate, BluetoothDevice device) {

    synchronized (lock2) {

    if (null ==stringBluetoothSocketMap)return;

                stringBluetoothSocketMap.clear();

            }

    }

    @Override

        public void onBlueToothLink(String action, BluetoothDevice device) {

    synchronized (lock2) {

    if (null ==stringBluetoothSocketMap) {

    stringBluetoothSocketMap =new HashMap<>();

                }

    String name = device.getName();

                if (!stringBluetoothSocketMap.containsKey(name))return;

                BlueToothEntity blueToothEntity =stringBluetoothSocketMap.get(name);

                if (null != blueToothEntity&&!blueToothEntity.isRunning()) {

    blueToothEntity.startListener();

                }

    }

    }

    @Override

        public void onBlueToothLinkBreak(String action, BluetoothDevice device) {

    synchronized (lock2) {

    if (null ==stringBluetoothSocketMap)return;

                String name = device.getName();

                if (stringBluetoothSocketMap.containsKey(name)) {

    BlueToothEntity blueToothEntity =stringBluetoothSocketMap.get(name);

                    if (null != blueToothEntity) {

    blueToothEntity.stopListener();

                        stringBluetoothSocketMap.remove(name);

                    }

    }

    }

    }

    public interface BlueToothUtilsListener {

    void onBlueToothUtilsListener(String message, BluetoothSocket bluetoothSocket);

        }

    public void onDestroy() {

    context.unregisterReceiver(blueToothUtilsBroadcastReceiver);

            this.context =null;

            this.listener =null;

            this.isRunning =false;

            if (null !=thread) {

    thread.interrupt();

                thread =null;

            }

    if (null !=mmServerSocket) {

    try {

    mmServerSocket.close();

                }catch (IOException e) {

    e.printStackTrace();

                }

    mmServerSocket =null;

            }

    bluetoothAdapter =null;

            synchronized (lock2) {

    if (null !=stringBluetoothSocketMap) {

    for (BlueToothEntity toothEntity :stringBluetoothSocketMap.values()) {

    toothEntity.stopListener();

                    }

    stringBluetoothSocketMap.clear();

                    stringBluetoothSocketMap =null;

                }

    }

    }

    }

    2.BlueToothUtils类里接收的BluetoothSocket去实现数据监听

    public class BlueToothEntity {

    private InputStreamins =null;

        private OutputStreamops =null;

        private BluetoothSocketbluetoothSocket;

        private BlueToothUtils.BlueToothUtilsListenerlistener;

        private byte[]mmBuffer;

        private ThreadreceiveThread;

        private boolean isRunning =false;

        public BlueToothEntity(BluetoothSocket bluetoothSocket, BlueToothUtils.BlueToothUtilsListener listener) {

    this.bluetoothSocket = bluetoothSocket;

            this.listener = listener;

            mmBuffer =new byte[1024];

            try {

    ins =this.bluetoothSocket.getInputStream();

                ops =this.bluetoothSocket.getOutputStream();

            }catch (IOException e) {

    e.printStackTrace();

            }

    }

    public boolean isRunning() {

    return isRunning;

        }

    public void startListener() {

    if (null ==ins)return;

            if (this.isRunning)return;

            this.isRunning =true;

            this.receiveThread =new Thread(() -> {

    while (this.isRunning) {

    int numBytes =0;

                    try {

    this.mmBuffer =new byte[1024];

                        numBytes =ins.read(mmBuffer);

                        if (numBytes >0) {

    @SuppressLint({"NewApi", "LocalSuppress"}) String order =new String(mmBuffer, 0, mmBuffer.length, "GBK").trim();

                            if (null !=this.listener && !TextUtils.isEmpty(order)) {

    this.listener.onBlueToothUtilsListener(order, this.bluetoothSocket);

                            }

    }

    }catch (IOException e) {

    e.printStackTrace();

                    }finally {

    this.mmBuffer =null;

                        if (numBytes <=0) {

    stopListener();

    break;

                        }

    }

    }

    });

            receiveThread.start();

        }

    public void stopListener() {

    this.isRunning =false;

            if (null !=receiveThread) {

    receiveThread.interrupt();

                receiveThread =null;

            }

    }

    @SuppressLint("NewApi")

    public void sendMessage(String message) {

    if (TextUtils.isEmpty(message))return;

            MyThreadPool.getInstance().newSingleThread(() -> {

    if (null !=ops) {

    try {

    ops.write(message.trim().getBytes("GBK"));

                        ops.flush();

                    }catch (IOException e) {

    e.printStackTrace();

                    }

    }

    });

        }

    }

    3.MainActivity界面布局

    <?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:layout_height="match_parent"

        android:orientation="vertical"

        tools:context=".MainActivity">

            android:layout_width="match_parent"

            android:layout_height="wrap_content">

                android:id="@+id/startBlueTooth"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:text="开启蓝牙"/>

                android:id="@+id/stopBlueTooth"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:text="关闭蓝牙"

                android:layout_centerInParent="true"/>

                android:id="@+id/scanBlueTooth"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:text="扫描设备"

                android:layout_alignParentRight="true"/>

            android:id="@+id/et"

            android:layout_width="match_parent"

            android:layout_height="wrap_content"

            android:inputType="text"

            android:hint="请输入命令"

            android:layout_marginTop="5dp"

            android:layout_marginLeft="10dp"

            android:layout_marginRight="40dp"/>

            android:id="@+id/sendMessage"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:layout_marginTop="5dp"

            android:text="发送信息"/>

            android:id="@+id/recyclerView"

            android:layout_width="match_parent"

            android:layout_height="match_parent"

            android:layout_marginTop="2dp"

            android:orientation="vertical"

            android:background="#eeeeee"

            app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"/>

    </LinearLayout>

    4.MainActivity调用

    public class MainActivityextends AppCompatActivityimplements View.OnClickListener

    , ScanBroadcastReceiver.ScanBroadcastResultListener

    , BlueToothStateBroadcastReceiver.BlueToothStateBroadcastReceiverListener

    , BlueDeviceAdapter.BlueDeviceListener

    , BlueToothUtils.BlueToothUtilsListener {

    private static final int REQUEST_ENABLE_BT =1005;

        private static final int SACN_TIME =10 *1000;

        private ButtonstartBlueTooth, stopBlueTooth, scanBlueTooth;

        private RecyclerViewrecyclerView;

        private ListbluetoothDevices;

        private ListnoPairedBluetoothDevices;

        private ListpairedBluetoothDevices;

        private BlueDeviceAdaptermAdapter;

        private ScanBroadcastReceiverscanBroadcastReceiver;

        private BlueToothStateBroadcastReceiverblueToothStateBroadcastReceiver;

        private BlueToothUtilsblueToothUtils;

        private EditTextet;

        @Override

        protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

            setContentView(R.layout.activity_main);

            blueToothUtils = BlueToothUtils.getInstance(this);

            blueToothUtils.start();

            initView();

            initRecy();

            setAdapter();

            setListeners();

            register();

            getPairedList();

            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION

                            , Manifest.permission.BLUETOOTH

                            , Manifest.permission.BLUETOOTH_ADMIN},

                    19);

            if (isBlueBoothEnabled()) {

    exposeBlueTooth();

            }

    }

    private void getUnPairList() {

    if (isBlueBoothAvailable()) {

    if (BluetoothAdapter.getDefaultAdapter().isDiscovering()) {

    Toast.makeText(this, "扫描中...", Toast.LENGTH_SHORT).show();

    return;

                }

    boolean isOk = BluetoothAdapter.getDefaultAdapter().startDiscovery();

                Handler handler =new Handler(new Handler.Callback() {

    @Override

                    public boolean handleMessage(@NonNull Message message) {

    BluetoothAdapter.getDefaultAdapter().cancelDiscovery();

    return true;

                    }

    });

                if (isOk) {

    handler.sendEmptyMessageDelayed(0, SACN_TIME);

                }

    }

    }

    private void getPairedList() {

    List datas = getBlueDeviceList();

            for (BluetoothDevice device : datas) {

    BlueDevice blueDevice =new BlueDevice();

                blueDevice.bluetoothDevice = device;

                blueDevice.isPaired =true;

                pairedBluetoothDevices.add(blueDevice);

            }

    bluetoothDevices.addAll(pairedBluetoothDevices);

            mAdapter.notifyDataSetChanged();

        }

    private void initRecy() {

    recyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {

    @Override

                public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {

    super.getItemOffsets(outRect, view, parent, state);

                    int index = parent.getChildAdapterPosition(view);

                    if (index ==0) {

    outRect.top = DensityUtil.dp2px(MainActivity.this, 2);

                    }

    outRect.bottom = DensityUtil.dp2px(MainActivity.this, 2);

                }

    });

        }

    private void setAdapter() {

    noPairedBluetoothDevices =new ArrayList<>();

            pairedBluetoothDevices =new ArrayList<>();

            bluetoothDevices =new ArrayList<>();

            mAdapter =new BlueDeviceAdapter(bluetoothDevices);

            mAdapter.setOnBlueDeviceListener(this);

            recyclerView.setAdapter(mAdapter);

        }

    private void register() {

    registerBlueStateReceiver();

            registerBlueScanReceiver();

        }

    private void registerBlueScanReceiver() {

    scanBroadcastReceiver =new ScanBroadcastReceiver();

            scanBroadcastReceiver.setOnScanBroadcastResultListener(this);

            IntentFilter filter =new IntentFilter(BluetoothDevice.ACTION_FOUND);

            registerReceiver(scanBroadcastReceiver, filter);

        }

    private void registerBlueStateReceiver() {

    blueToothStateBroadcastReceiver =new BlueToothStateBroadcastReceiver();

            blueToothStateBroadcastReceiver.setOnBlueToothStateBroadcastReceiverListener(this);

            IntentFilter intentFilter =new IntentFilter();

            // 监视蓝牙关闭和打开的状态

            intentFilter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);

            // 监视蓝牙设备与APP连接的状态

            intentFilter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);

            intentFilter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED);

            intentFilter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED);

            //配对的状态

            intentFilter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);

            // 注册广播

            registerReceiver(blueToothStateBroadcastReceiver, intentFilter);

        }

    private void setListeners() {

    startBlueTooth.setOnClickListener(this);

            stopBlueTooth.setOnClickListener(this);

            scanBlueTooth.setOnClickListener(this);

            findViewById(R.id.sendMessage).setOnClickListener(view -> {

    String message =et.getText().toString().trim();

                if (TextUtils.isEmpty(message))return;

                blueToothUtils.sendMessage(message);

            });

            blueToothUtils.setOnBlueToothUtilsListener(this);

        }

    private void initView() {

    startBlueTooth = findViewById(R.id.startBlueTooth);

            stopBlueTooth = findViewById(R.id.stopBlueTooth);

            recyclerView = findViewById(R.id.recyclerView);

            scanBlueTooth = findViewById(R.id.scanBlueTooth);

            et = findViewById(R.id.et);

        }

    @Override

        public void onClick(View view) {

    if (!isBlueBoothAvailable()) {

    Toast.makeText(this, "蓝牙设备不可用", Toast.LENGTH_SHORT).show();

    return;

            }

    boolean isEnabled = isBlueBoothEnabled();

            switch (view.getId()) {

    case R.id.startBlueTooth:

    if (isEnabled) {

    Toast.makeText(this, "蓝牙已开启", Toast.LENGTH_SHORT).show();

    return;

                    }

    Intent enableBtIntent =new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);

                    startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);

    break;

                case R.id.stopBlueTooth:

    if (!isEnabled) {

    Toast.makeText(this, "蓝牙已关闭", Toast.LENGTH_SHORT).show();

    return;

                    }

    BluetoothAdapter.getDefaultAdapter().disable();

    break;

                case R.id.scanBlueTooth:

    getUnPairList();

    break;

            }

    }

    @Override

        public void onBlueToothStateBroadcastReceiver(int blueToothSate, BluetoothDevice device) {

    if (blueToothSate == BluetoothAdapter.STATE_ON) {

    exposeBlueTooth();

                List datas = getBlueDeviceList();

                for (BluetoothDevice btc : datas) {

    BlueDevice blueDevice =new BlueDevice();

                    blueDevice.bluetoothDevice = btc;

                    blueDevice.isPaired =true;

                    pairedBluetoothDevices.add(blueDevice);

                }

    bluetoothDevices.addAll(0, pairedBluetoothDevices);

                mAdapter.notifyItemRangeInserted(0, pairedBluetoothDevices.size());

            }

    if (blueToothSate == BluetoothAdapter.STATE_OFF) {

    bluetoothDevices.clear();

                pairedBluetoothDevices.clear();

                noPairedBluetoothDevices.clear();

                mAdapter.notifyDataSetChanged();

            }

    }

    @Override

        public void onBlueToothPairedStateBroadcastReceiver(int pairState, BluetoothDevice device) {

    if (null == device)return;

            BlueDevice blueDevice = getBlueDeviceFromList(device);

            if (pairState == BluetoothDevice.BOND_BONDED) {

    if (null == blueDevice) {

    blueDevice =new BlueDevice();

                    blueDevice.isPaired =true;

                    blueDevice.bluetoothDevice = device;

                }else {

    noPairedBluetoothDevices.remove(blueDevice);

                    int index =bluetoothDevices.indexOf(blueDevice);

                    bluetoothDevices.remove(blueDevice);

                    mAdapter.notifyItemRemoved(index);

                    blueDevice.bluetoothDevice = device;

                    blueDevice.isPaired =true;

                }

    pairedBluetoothDevices.add(blueDevice);

                if (noPairedBluetoothDevices.size() ==0) {

    bluetoothDevices.add(blueDevice);

                }else {

    bluetoothDevices.add(pairedBluetoothDevices.size() -1, blueDevice);

                }

    mAdapter.notifyItemInserted(bluetoothDevices.indexOf(blueDevice));

            }

    if (pairState == BluetoothDevice.BOND_NONE) {

    if (null == blueDevice) {

    blueDevice =new BlueDevice();

                    blueDevice.isPaired =false;

                    blueDevice.bluetoothDevice = device;

                }else {

    pairedBluetoothDevices.remove(blueDevice);

                    int index =bluetoothDevices.indexOf(blueDevice);

                    bluetoothDevices.remove(blueDevice);

                    mAdapter.notifyItemRemoved(index);

                    blueDevice.bluetoothDevice = device;

                    blueDevice.isPaired =false;

                }

    noPairedBluetoothDevices.add(blueDevice);

                bluetoothDevices.add(blueDevice);

                mAdapter.notifyItemInserted(bluetoothDevices.indexOf(blueDevice));

            }

    }

    @Override

        public void onBlueToothLinkedStateBroadcastReceiver(String action, BluetoothDevice device) {

    if (null == device)return;

            BlueDevice blueDevice = getBlueDeviceFromList(device);

            if (null == blueDevice)return;

            if (action.equals(BluetoothDevice.ACTION_ACL_CONNECTED)) {

    blueDevice.isConnected =true;

            }

    if (action.equals(BluetoothDevice.ACTION_ACL_DISCONNECTED)) {

    blueDevice.isConnected =false;

            }

    mAdapter.notifyItemChanged(bluetoothDevices.indexOf(blueDevice));

        }

    @Override

        public void onBlueDeviceListener(int position) {

    BlueDevice bd =bluetoothDevices.get(position);

            if (null != bd && !bd.isConnected &&null != bd.bluetoothDevice) {

    blueToothUtils.connect(bd.bluetoothDevice);

            }

    }

    private void exposeBlueTooth() {

    Intent discoverableIntent =

    new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);

            discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 0);

            discoverableIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

            startActivity(discoverableIntent);

        }

    @Override

        public void onScanBroadcastResultListener(BluetoothDevice bluetoothDevice) {

    BlueDevice blueDevice = getBlueDeviceFromList(bluetoothDevice);

            if (null == blueDevice) {

    blueDevice =new BlueDevice();

                blueDevice.bluetoothDevice = bluetoothDevice;

                noPairedBluetoothDevices.add(blueDevice);

                bluetoothDevices.add(blueDevice);

                mAdapter.notifyItemInserted(bluetoothDevices.size() -1);

            }else {

    blueDevice.bluetoothDevice = bluetoothDevice;

                mAdapter.notifyItemChanged(bluetoothDevices.indexOf(blueDevice));

            }

    }

    public BlueDevicegetBlueDeviceFromNoPairList(BluetoothDevice bluetoothDevice) {

    if (TextUtils.isEmpty(bluetoothDevice.getAddress()))return null;

            for (BlueDevice blueDevice :noPairedBluetoothDevices) {

    if (blueDevice.bluetoothDevice.getAddress().equals(bluetoothDevice.getAddress()))

    return blueDevice;

            }

    return null;

        }

    public BlueDevicegetBlueDeviceFromPairList(BluetoothDevice bluetoothDevice) {

    if (TextUtils.isEmpty(bluetoothDevice.getAddress()))return null;

            for (BlueDevice blueDevice :pairedBluetoothDevices) {

    if (blueDevice.bluetoothDevice.getAddress().equals(bluetoothDevice.getAddress()))

    return blueDevice;

            }

    return null;

        }

    public BlueDevicegetBlueDeviceFromList(BluetoothDevice bluetoothDevice) {

    if (TextUtils.isEmpty(bluetoothDevice.getAddress()))return null;

            for (BlueDevice blueDevice :bluetoothDevices) {

    if (blueDevice.bluetoothDevice.getAddress().equals(bluetoothDevice.getAddress()))

    return blueDevice;

            }

    return null;

        }

    private ListgetBlueDeviceList() {

    if (!isBlueBoothAvailable())return new ArrayList<>();

            Set pairedDevices = BluetoothAdapter.getDefaultAdapter().getBondedDevices();

            List devices =new ArrayList<>();

            if (pairedDevices.size() >0) {

    devices.addAll(pairedDevices);

            }

    return devices;

        }

    private boolean isBlueBoothAvailable() {

    return null != BluetoothAdapter.getDefaultAdapter();

        }

    private boolean isBlueBoothEnabled() {

    if (!isBlueBoothAvailable())return false;

            return BluetoothAdapter.getDefaultAdapter().isEnabled();

        }

    @Override

        protected void onDestroy() {

    super.onDestroy();

            scanBroadcastReceiver.setOnScanBroadcastResultListener(null);

            blueToothStateBroadcastReceiver.setOnBlueToothStateBroadcastReceiverListener(null);

            unregisterReceiver(scanBroadcastReceiver);

            unregisterReceiver(blueToothStateBroadcastReceiver);

            scanBroadcastReceiver =null;

            blueToothStateBroadcastReceiver =null;

            blueToothUtils.onDestroy();

        }

    @Override

        public void onBlueToothUtilsListener(String message, BluetoothSocket bluetoothSocket) {

    runOnUiThread(() -> {

    Log.e("阿斯达所多啊所","value = "+message);

                Toast.makeText(this, message, Toast.LENGTH_SHORT).show();

            });

        }

    }

    相关文章

      网友评论

          本文标题:Android传统蓝牙实现多连接,消息发送与接收

          本文链接:https://www.haomeiwen.com/subject/rfikoftx.html