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

传统蓝牙适用于较为耗电的操作,其中包括 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();

        });

    }

}

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 206,602评论 6 481
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 88,442评论 2 382
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 152,878评论 0 344
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 55,306评论 1 279
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 64,330评论 5 373
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,071评论 1 285
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,382评论 3 400
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,006评论 0 259
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 43,512评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,965评论 2 325
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,094评论 1 333
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,732评论 4 323
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,283评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,286评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,512评论 1 262
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,536评论 2 354
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,828评论 2 345

推荐阅读更多精彩内容