Android中蓝牙设备搜索

参考网址:https://blog.csdn.net/jian11058/article/details/90407568

https://blog.csdn.net/gh8609123/article/details/66969006

 

private void initBlueTooth() {
        BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
        if (pairedDevices.size() > 0) {
            for (BluetoothDevice device : pairedDevices) {
                String str = device.getName() + "|" + device.getAddress();
                System.out.println("BlueTooth已配对的设备:"+str);
            }
        }
        if (!mBluetoothAdapter.isDiscovering()) {
            //搜索蓝牙设备
            mBluetoothAdapter.startDiscovery();
        }
    // 注册Receiver来获取蓝牙设备相关的结果
        IntentFilter intent = new IntentFilter();
        intent.addAction(BluetoothDevice.ACTION_FOUND); // 用BroadcastReceiver来取得搜索结果
        intent.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
        intent.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
        registerReceiver(searchDevices, intent);
}
private BroadcastReceiver searchDevices = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action.equals(BluetoothDevice.ACTION_FOUND)) { //found device
                BluetoothDevice device = intent
                        .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                String str = device.getName() + "|" + device.getAddress();
                System.out.println("BlueTooth搜索到的设备:"+str);
                List list = new ArrayList();
                //如果List中没有str元素则返回-1
                if (list.indexOf(str) == -1)// 防止重复添加
                    list.add(str); // 获取设备名称和mac地址

            } else if (action.equals(BluetoothAdapter.ACTION_DISCOVERY_STARTED)) {
                Toast.makeText(getBaseContext(), "正在扫描", Toast.LENGTH_SHORT).show();
            } else if (action
                    .equals(BluetoothAdapter.ACTION_DISCOVERY_FINISHED)) {
                Toast.makeText(getBaseContext(), "扫描完成,点击列表中的设备来尝试连接", Toast.LENGTH_SHORT).show();
            }
        }
    };
@Override
protected void onDestroy() {
  super.onDestroy();
  unregisterReceiver(searchDevices);
}

 


http://www.niftyadmin.cn/n/638687.html

相关文章

C语言控制51单片机音乐报告,使用51单片机实现音乐播放的程序资料概述

利用单片机(或单板机)奏乐大概是无线电爱好者感兴趣的问题之一。本文从单片机的基本发间实验出发&#xff0c;谈谈音乐程序的设计原理&#xff0c;并给出具体实例&#xff0c;以供参考。下面提供2个列子&#xff0c;大家根据 需要自行选择单片机最小系统&#xff0c;两位LED数码…

下列组件可以用来实现对单选按钮分组的是_C#训练题二

一、选择题1.标签具有下列哪个属性&#xff1f;()A&#xff0e;温度属性B&#xff0e;文本属性C&#xff0e;亮度属性D&#xff0e;硬度属性2.设计时调整窗体的大小可以使用()。A&#xff0e;键盘B&#xff0e;屏幕C&#xff0e;耳麦D&#xff0e;鼠标3.将代码放置在合适的位置…

Android中获取IMEI号

参考网址&#xff1a; https://www.cnblogs.com/fnlingnzb-learner/p/7580691.html https://www.jianshu.com/p/c46b6c9b8990 /*** 获取手机IMEI号** 需要动态权限: android.permission.READ_PHONE_STATE*/public static String getIMEI(Context context) {TelephonyManager t…

db2如何锁定一张表_DB2如何快速清空一张表

在DB2中如何快速清空一张表呢&#xff0c;大家想到的第一想法肯定是采用DELETE或TRUNCATE命令,如果数据量小的话可以正常操作&#xff0c;但是数据量大的时候一方面速度会很慢&#xff0c;而且使用"delete table"命令删除整个大表中的数据&#xff0c;由于这个命令采…

Android判断当前系统时间是否在指定时间的范围内(免消息打扰)

参考网址&#xff1a;https://www.cnblogs.com/zhangminghan/p/5632346.html import android.text.format.Time;public class TimeUtil {/*** 判断当前系统时间是否在指定时间的范围内** param beginHour 开始小时&#xff0c;例如22* param beginMin 开始小时的分钟数&#…

c语言 简单题目及答案,C语言的一些简单题目,没有答案,哪位大神帮忙做一下!!!...

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼20&#xff0e;下面正确的语句是 。c语句中对嵌套if语句的规定是&#xff1a;else总是与___配对。A) 缩进位置相同的ifB) 其同一复合语句中,前面最近的且尚未配对的ifC) 其之前最近的if D) 第一个if1、在C语言中&#xff0c;合法的…

change element 原始值_elementui下拉框调取接口后设置默认值,change时报错

1.项目需求简化下是这样&#xff0c;需要先调用接口获取下拉框的值并且设置第一项为其默认值&#xff0c;然后调用另外个接口需要带上这个参数2.下拉框代码v-for"item in productTypeList":key"item.id":label"item.typeName":value"item.i…

java中阿拉伯数字转换为汉字数字

参考网址&#xff1a;https://blog.csdn.net/qq_39657597/article/details/83687955 public class VedioExtractSpeech {public static void main(String[] args) {System.out.println("toChinese&#xff1a;"toChinese("1230456"));System.out.println(…