如何使用Java发现并配对Android蓝牙设备?有什么代码可供我参考吗?

解决方法:

以下代码将在您必须实现客户端和服务器之后发现已配对和未配对设备的列表,该列表负责配对设备并将数据发送到设备,因为您可以利用BluetoothChatSample给你一个主意.

private Set pairedDevices;

public static ArrayList BondedDeviceList;

public static ArrayList NewDeviceList;

public void makeDiscoverable()

{

discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);

discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);

activity.startActivity(discoverableIntent);

}

//It will Add the paired device in the BondedDeviceList

public void queryPairedDevice(){

pairedDevices = mBluetoothAdapter.getBondedDevices();

// If there are paired devices

if(pairedDevices==null)

{

//No Bonded Devices

}else

{

if (pairedDevices.size() > 0) {

// Loop through paired devices

for (BluetoothDevice device : pairedDevices) {

BondedDeviceList.add(device);

}

BondedDeviceList.add("End");

}

}

}

//Broadcast Receiver will find the Available devices and the discovery finished

private final BroadcastReceiver mReceiver = new BroadcastReceiver() {

@Override

public void onReceive(Context context, Intent intent) {

String action = intent.getAction();

// When discovery finds a device

if (BluetoothDevice.ACTION_FOUND.equals(action.trim())) {

// Get the BluetoothDevice object from the Intent

BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

// If it's already paired, skip it, because it's been listed already

if (device.getBondState() != BluetoothDevice.BOND_BONDED) {

NewDeviceList.add(device);

}

// When discovery is finished, change the Activity title

} else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {

if (NewDeviceList.isEmpty() == true) {

String noDevices = "No Devices";

NewDeviceList.add(noDevices);

}

System.out.println("Discovery Finished!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");

NewDeviceList.add("End");

}

}

};

//This is query for the bluetooth devices

public void queryDevices(){

actionFoundFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND);

activity.registerReceiver(mReceiver, actionFoundFilter);

// Don't forget to unregister during onDestroy

discoveryFinishedFilter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);

activity.registerReceiver(mReceiver, discoveryFinishedFilter);

// Don't forget to unregister during onDestroy

queryPairedDevice();

mBluetoothAdapter.startDiscovery();

}

//Unregister the receivers

public void unregisterReceiver() {

// Make sure we're not doing discovery anymore

if (mBluetoothAdapter != null) {

mBluetoothAdapter.cancelDiscovery();

}

// Unregister broadcast listeners

activity.unregisterReceiver(mReceiver);

}

干杯.

标签:android,java,eclipse,bluetooth,device-name

来源: https://codeday.me/bug/20191009/1879975.html

搜索已配对蓝牙 java,java-查找和配对蓝牙设备相关推荐

  1. 蓝牙配对模式 java_【Android】蓝牙开发—— 经典蓝牙配对介绍(Java代码实现演示)附Demo源码...

    目录 前言 一.连接&配对方法介绍 二.演示:第一次连接蓝牙设备  &  直接与蓝牙设备建立配对 三.总结 四.补充 五.Demo案例源码地址: 前言 前面两篇文章[Android]蓝 ...

  2. 【Android】蓝牙开发—— 经典蓝牙配对介绍(Java代码实现演示)附Demo源码

    目录 前言 一.连接&配对方法介绍 二.演示:第一次连接蓝牙设备  &  直接与蓝牙设备建立配对 三.总结 四.补充 五.Demo案例源码地址: 前言 前面两篇文章[Android]蓝 ...

  3. java linkedlist 查找_Java中LinkedList真的是查找慢增删快

    测试结果 废话不多说,先上测试结果.作者分别在ArrayList和LinkedList的头部.尾部和中间三个位置插入与查找100000个元素所消耗的时间来进行对比测试,下面是测试结果 (感谢@Hosa ...

  4. Java/java程序设计:房屋出租系统:要求实现:新增房源,查找房屋信息,修改房屋信息,删除房屋信息,显示所有房屋列表,退出房屋管理系统;

    Java/java程序设计:房屋出租系统: 一.前言: 一.1. 框架图 二.各类包下的代码实现: 1. 主文件(运行文件HouseApp.java) 2. 房屋类文件(House.java) 3. ...

  5. java 用折半查找,java折半查找算法

    //当 low>high 时表示查找区间为空,查找失败 } Java 代码: /** * 二分查找算法 * * @param srcArray 有序数组 * @param target 被查找的 ...

  6. Zip Slip目录遍历漏洞已影响多个Java项目

    \ 看新闻很累?看技术新闻更累?试试下载InfoQ手机客户端,每天上下班路上听新闻,有趣还有料! \ \\ 近日,专注于开源及云安全监控防范工作的 Snyk 公司披露了一种可能会造成任意文件被覆写的安 ...

  7. Java-Runoob-高级教程-实例-数组:10. Java 实例 – 查找数组中的重复元素-un

    ylbtech-Java-Runoob-高级教程-实例-数组:10. Java 实例 – 查找数组中的重复元素 1.返回顶部 1. Java 实例 - 查找数组中的重复元素  Java 实例 以下实例 ...

  8. java 匹配最后一次出现的字符_在Java中查找字符串中字符的最后一次出现

    使用该lastIndexOf()方法在Java中查找字符串中字符的最后一次出现. 假设以下是我们的字符串.String myStr = "Amit Diwan"; 在上面的字符串中 ...

  9. java string查找_查找输出程序(Java String类)

    java string查找 Program 1 程序1 public class iHelp {public static void main (String[] args) {System.out. ...

  10. java字符串字符排列组合_如何在Java中查找字符串的所有排列

    java字符串字符排列组合 In this tutorial, we will learn how to find the permutation of a String in a Java Prog ...

最新文章

  1. Hopfield 网络(下)
  2. C运行时库和标准C++库
  3. 郑州商品交易所与阿里云达成合作,推进核心数据分析平台建设
  4. Git(10)-merge
  5. bzoj 1295: [SCOI2009]最长距离
  6. Linux中locate命令查找文件位置
  7. java连接mysql数据库 R_Java连接Mysql数据库详细代码实例
  8. MySQL Performance Schema
  9. OpenERP工作流不同角色看属于自己审批的方法
  10. 使用spss做各种相关性分析的方法和步骤
  11. Clark变换的等幅值变换乘为什么2/3
  12. 通用数据权限的设计思路
  13. 骨骼动画驱动理解, blender 驱动骨骼
  14. 图计算思维与实践 (一)概览
  15. 小陈java学习笔记0817
  16. 三个步骤 让你学会看懂考研数学课本
  17. 科大讯飞两代AI平台演进之路--讯飞云计算研究院副院长龙明康访谈
  18. 给测试小妹做了一个js版屏幕录制工具iREC,她用后竟说喜欢我
  19. 南京理工大学 计算机调剂科目,南京理工大学2019考研调剂信息
  20. 更新DOTA2显示无法连接到更新服务器,提示“dota2位于更新队列中”怎么办?解决dota2无法更新办法...

热门文章

  1. plc顺序控制设计法的设计基本步骤
  2. iOS Charles捉取正式环境上的数据
  3. python爬虫利用线程池下载视频
  4. request库单一视频下载
  5. 9.3(使用Date类) 编写程序创建一个Date对象,设置它的流逝时间
  6. u盘插电脑计算机卡了,电脑插入U盘就卡顿?3个原因你需要了解一下
  7. HDF5 学习总结1
  8. 关于video标签的视频格式在各个浏览器中的兼容问题
  9. winform 分页打印实例
  10. JEESZ 模块开发文档