蓝牙是一种重要的短距离无线通信协议,广泛应用于各种设备(手机,医疗,汽车等)。蓝牙是比较常用的无线通信设备,早研究成为手机的标配。现在的安卓手机基本上都有蓝牙,所有通过蓝牙对数据有很好的硬件基础

在Android中,与蓝牙有关的类和接口在android.bluetooth包中。其中BluetoothAdapter是蓝牙中的核心类,代表本地的蓝牙适配器设备。BluetoothAdapter类让用户能执行基本的蓝牙任务。例如: 初始化设备的搜索,查询可匹配的设备集,使用一个已知的MAC地址来初始化一个BluetoothDevice类,创建一个 BluetoothServerSocket类以监听其它设备对本机的连接请求等。

打开和扫描蓝牙,并将扫面到的蓝牙设备显示在listview上

BluetoothActivity

package cn.edu.cqu.bluetooth;import java.util.ArrayList;
import java.util.Iterator;
import java.util.Set;
import com.example.project.R;import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;public class BluetoothActivity extends Activity{private Button button2;private ListView list;private ArrayAdapter mArrayAdapter;  private BluetoothReceiver bluetoothReceiver;private BluetoothAdapter bluetoothAdapter;private BluetoothDevice device;private ArrayList<String> PairedMaclist;private TextView textView1;int flag;@Overrideprotected void onCreate(Bundle savedInstanceState) {this.requestWindowFeature(Window.FEATURE_NO_TITLE);super.onCreate(savedInstanceState);setContentView(R.layout.bluetooth);list = (ListView)findViewById(R.id.listView);textView1 = (TextView)findViewById(R.id.textView);//创建一个IntentFilter对象,将其action指定为BluetoothDevice.ACTION_FOUND,查找蓝牙IntentFilter intentFileter = new IntentFilter(BluetoothDevice.ACTION_FOUND);bluetoothReceiver = new BluetoothReceiver();//注册广播接收器registerReceiver(bluetoothReceiver, intentFileter);bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();button2 = (Button)findViewById(R.id.scanButton);button2.setOnClickListener(new ScanButtonListener());list.setOnItemClickListener(new ListViewListener());openBluetooth();mArrayAdapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1);PairedMaclist=new ArrayList<String>();}@Overrideprotected void onDestroy() {unregisterReceiver(bluetoothReceiver);super.onDestroy();}private void openBluetooth() {//bluetoothAdapter.enable();if(bluetoothAdapter != null){if(!bluetoothAdapter.isEnabled()){Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);startActivity(intent);}//得到所有已经被对的蓝牙适配器对象Set<BluetoothDevice> devices = bluetoothAdapter.getBondedDevices();if(devices.size() > 0){for(Iterator<BluetoothDevice> iterator = devices.iterator();iterator.hasNext();){BluetoothDevice bluetoothDevice = (BluetoothDevice) iterator.next();//得到远程蓝牙设备的地址System.out.println(bluetoothDevice.getAddress());}}}else {//System.out.println("没有蓝牙设备");Toast.makeText(BluetoothActivity.this, "没有蓝牙设备",0).show();}}class BluetoothReceiver extends BroadcastReceiver{@Overridepublic void onReceive(Context context, Intent intent) {String action = intent.getAction();if(BluetoothDevice.ACTION_FOUND.equals(action)){//得到intent里面的信息device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);Toast.makeText(BluetoothActivity.this, "发现蓝牙设备",0).show();//System.out.println(device.getAddress());mArrayAdapter.add(device.getName() + "\n" + device.getAddress());PairedMaclist.add(device.getAddress());list.setAdapter(mArrayAdapter);}}}class ScanButtonListener implements OnClickListener{@Overridepublic void onClick(View arg0) {mArrayAdapter.clear();bluetoothAdapter.startDiscovery();Toast.makeText(BluetoothActivity.this, "开始扫描",0).show();}}class ListViewListener implements OnItemClickListener{@Overridepublic void onItemClick(AdapterView<?> arg0, View view, int position,long id) {String str = PairedMaclist.get(position);//System.out.println("Str-------------" + str);Intent intent = new Intent(BluetoothActivity.this,BluetoothWaveform.class);intent.putExtra("BluetoothMAC",str);startActivity(intent);}}
}
显示和扫描蓝牙布局文件
<pre name="code" class="html"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:layout_margin="20dp"android:orientation="vertical" ><Button android:id="@+id/scanButton"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginBottom="20dp"android:text="扫描蓝牙设备"/><TextView android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginBottom="20dp"android:textSize="20sp"android:text="搜索到以下设备:"/><ListView android:id="@+id/listView"android:layout_width="match_parent"android:layout_height="wrap_content"></ListView></LinearLayout><img src="https://img-blog.csdn.net/20150409144408261?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY2hlbmZlbmdkZWp1YW5saWFu/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />

android 蓝牙通讯实现手机蓝牙的开启,并扫描附近可见的蓝牙设备相关推荐

  1. android 打开蓝牙可被发现,手机蓝牙隐藏功能,竟然被学姐发现了!

    大家好,我是学姐.今天跟大家聊聊蓝牙.那么什么是蓝牙呢?所谓蓝牙,其实就是一种短距离无线电技术,用于数据传输.学姐在十年前,那时候用的爱立信和诺基亚手机,就有蓝牙功能.学姐经常和同学朋友,利用手机的蓝 ...

  2. 单片机蓝牙模块与手机蓝牙通信(6)小结

    安卓版本不同,代码细节会不同,要耐心根据提示修改调试,必要看看官方文档和百度. 明明已经调试通过,版本不同就报错问题是学习者要克服的心理最大关.前面5篇是按照2022.3月下载版本,API17. 下面 ...

  3. arduino蓝牙通讯代码_蓝牙4.0模块 无线数据传输模块 无线蓝牙串口 Arduino

    产品介绍: 蓝牙4.0模块是专为智能无线数据传输而打造的.该模块遵循BT2.1+EDR/3.0/4.0(BLE)蓝牙规范,支持SPP 蓝牙串口协议等.本模块集成了MCU 和蓝牙芯片,支持UART.SP ...

  4. 嵌入式linux蓝牙通讯,开发板蓝牙通信问题,有这方面经验的请进

    该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 我的板子是fl2440,系统版本2.6.12 主机是红帽5,各项服务都有开. [root@bogon ~]# hciconfig hci0: Type: ...

  5. android afw模式,[HUAWEI-TITANC328]手机加密功能开启后,工作区中添加超级备份手机不断重启的分析报告...

    工作区中添加超级备份手机不断重启的分析报告 [华为反馈] [TIT-AL00C328B120_AFW_必现]创建工作空间后升级系统,升级失败,手机循环重启. 通过分析log发现,实际上这个问题与OTA ...

  6. 【Android App】检查手机连接WiFi信息以及扫描周围WiFi的讲解及实战(附源码和演示 超详细必看)

    需要全部代码请点赞关注收藏后评论区留言私信~~~ 一.检查是否连接WiFi以及输出WiFi信息 传统的定位方式不适用于室内的垂直定位,原因如下: (1)卫星定位要求没有障碍物遮挡,它在户外比较精准,在 ...

  7. android 外接扫码枪_Android手机(设备)连接扫描枪扫码遇到的问题

    以下内容以我发布前的时间为准,可能之后厂商给设备改进后都没有这些问题. 1.android手机连接扫描枪有些手机显示不了系统键盘 2.连接扫描枪使用的是百度输入法,条码是字母数字组合的扫码会出现乱码( ...

  8. Android手机蓝牙连接笔记本电脑蓝牙

    1. 实验设备 1.1 笔记电脑 品牌:DELL 蓝牙模块:BCM43142(内置型带蓝牙的无线网卡) 蓝牙驱动:bluetooth_broadcom_6.5.1.6400_w7(通过360驱动大师下 ...

  9. 实现蓝牙模块与手机进行通讯

    之前做过一个Zigbee通信的实验,需要用到蓝牙模块与手机蓝牙模块之间的通信用以接收RSSI,很长时间没有解决就收数据乱码的问题,最后无意间了解到手机蓝牙的baudrate为9600,乱码的问题也就迎 ...

最新文章

  1. JavaScript面向对象的支持(7)
  2. python 以 utf-8 格式写文件
  3. 【安全漏洞】CVE-2020-26567 DSR-250N 远程拒绝服务漏洞分析
  4. linux 实时功能,linux – 什么是同时具有实时循环和非实时功能的更好方法
  5. GitHub项目功能理解
  6. 号称36个月不卡顿!网友:就是有点贵
  7. python情感分析模型_Python有趣|中文文本情感分析
  8. 使用screen来保存你的远程会话
  9. 功能强大的云打印组件-开始使用
  10. cmder 下载及安装
  11. Qt实现图片的简单压缩
  12. DNS 解析出错导致 MySQL 无法连接
  13. 银行wifi认证登录怎么样
  14. 关于Github项目DCRNN运行问题小结(1)——tables模块包
  15. flv视频播放器停止时带图片
  16. Python获取列表list中的非零数据、第一个非零元素、最后一个非零元素
  17. 用Python做一个价值数万的市场调查报告程序,分分钟完成工作
  18. 计算机光驱参数,光驱价格参数简介【图文】
  19. Java工程师培训课(十七【新的领域】)
  20. 虚拟机安装报错-启动失败-Intel VT-x 处于禁用状态

热门文章

  1. C++11 std::bind 和 std::placeholder
  2. 2020大学计算机知到答案,2020年_知到_大学计算机(济南大学)_网课答案
  3. 山东管理学院计算机专业在哪个校区,2019年山东管理学院新生在哪个校区及新生开学报到时间...
  4. 绿联网卡转接mac设置_物联网卡这样设置一下上网全程4G!建议收藏!
  5. oracle or索引失效_oracle数据库中索引会失效的几种情况
  6. 用python语言调试程序你用的平台是_Python 程序如何高效地调试?
  7. Cracer渗透-下载安装软件
  8. 语音识别传统方法(GMM+HMM+NGRAM)概述
  9. 什么叫Web前端?web前端HTML5学习方法分享
  10. jzoj100029. 【NOIP2017提高A组模拟7.8】陪审团(贪心,排序)