Android经典蓝牙
最近一直在做手机硬件相关的东西的,接触到了手机蓝牙,这篇文章主要介绍经典蓝牙相关的东西,下篇文章将介绍低功耗蓝牙BLE相关的东西,由于时间的关系,只是一个大概的总结和记录,相关要点在注释里面。
一、常用的蓝牙操作
1、打开蓝牙
public static void openBluetooth() {BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();if (!bluetoothAdapter.isEnabled()) {bluetoothAdapter.enable();}
}public static void openBluetooth(Activity a) {final BluetoothManager bluetoothManager =(BluetoothManager) a.getSystemService(Context.BLUETOOTH_SERVICE);BluetoothAdapter bluetoothAdapter = bluetoothManager.getAdapter();if (bluetoothAdapter != null || !bluetoothAdapter.isEnabled()) {Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);a.startActivityForResult(enableBtIntent, BLUETOOTH_CODE);}
}
2、关闭蓝牙
public static void closeBluetooth() {BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();if (bluetoothAdapter.isEnabled()) {bluetoothAdapter.disable();}
}
3、判断蓝牙是否打开
public static void openBluetooth() {BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();if (!bluetoothAdapter.isEnabled()) {bluetoothAdapter.enable();
}
4、获取已经配对的蓝牙信息
public void getConnectedBluetoothDevices(JsGetConnectedBluetoothBean bean) {BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();JsCreateDevicesBean jsCreateDevicesBean = new JsCreateDevicesBean();if (pairedDevices.size() > 0) {for (BluetoothDevice device : pairedDevices) {LogUtils.d("BlueTooth已配对的设备:" + device.getAddress() + "----" + device.getName());}}
}
二、蓝牙权限
1、申请权限
静态注册:
<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" />
动态申请定位权限:
String[] p = new String[]{Manifest.permission.ACCESS_FINE_LOCATION};
ActivityCompat.requestPermissions(activity,p,1000);
如果您的目标是 Android 10,那么您需要 ACCESS_FINE_LOCATION 来扫描。ACCESS_COARSE_LOCATION 在 Android 10 中不再起作用。
2、打开手机定位
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
MainActivity a = (MainActivity) activity;
a.startActivityForResult(intent, OPEN_GPS_CODE)
注意:蓝牙的搜索、配对等等需要开启权限,否则啥也获取不到。
三、蓝牙搜索、获取蓝牙地址、信号强度、UUID等
1、注册蓝牙广播
IntentFilter filter = new IntentFilter();
filter.addAction(BluetoothDevice.ACTION_FOUND);
filter.addAction(BluetoothDevice.ACTION_UUID);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
activity.registerReceiver(bluetoothReceiver, filter);
2、开始搜索
if (bluetoothAdapter.isDiscovering()) {//搜索蓝牙设备bluetoothAdapter.cancelDiscovery();
}
bluetoothAdapter.startDiscovery();
这一步非常耗性能和时间,需要在子线程进行。
3、监听广播
private List<BluetoothDevice> deviceList = new ArrayList<>();public BroadcastReceiver bluetoothReceiver = new BroadcastReceiver() {@Overridepublic void onReceive(Context context, Intent intent) {String action = intent.getAction();switch (action) {case BluetoothAdapter.ACTION_DISCOVERY_STARTED:LogUtils.d("开始搜索");break;case BluetoothDevice.ACTION_FOUND:LogUtils.d("搜索中……");BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);short rssi = intent.getShortExtra(BluetoothDevice.EXTRA_RSSI, (short) 0);if (!TextUtils.isEmpty(device.getName())) {String str = device.getName() + "|" + device.getAddress() + "|" + rssi;LogUtils.d("搜索到的蓝牙信息名称、地址、信号强度:" + str);if (!deviceList.contains(device)) {//过滤掉没有名称的重复的内容deviceList.add(device);}}break;case BluetoothAdapter.ACTION_DISCOVERY_FINISHED:LogUtils.d("搜索结束");for (int i = 0; i < deviceList.size(); i++) {BluetoothDevice bluetoothDevice = deviceList.get(i);//这块触发了BluetoothDevice.ACTION_UUID,以便于获取到UUIDbluetoothDevice.fetchUuidsWithSdp();LogUtils.d(bluetoothDevice.getName() + " | " + bluetoothDevice.getAddress());}break;case BluetoothDevice.ACTION_UUID:LogUtils.d("触发UUID");BluetoothDevice bluetoothDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);Parcelable[] uuid = intent.getParcelableArrayExtra(BluetoothDevice.EXTRA_UUID);try {//反射获取本地名称Method method = bluetoothDevice.getClass().getMethod("getAlias");if (method != null) {String localName = (String) method.invoke(bluetoothDevice);}} catch (NoSuchMethodException e) {e.printStackTrace();} catch (InvocationTargetException e) {e.printStackTrace();} catch (IllegalAccessException e) {e.printStackTrace();}if (uuid != null) {for (Parcelable ep : uuid) {LogUtils.d("UUID :" + ep.toString());}} else {LogUtils.d("UUID : null");}break;}}};
4、停止搜索
if (bluetoothAdapter.isDiscovering()) {bluetoothAdapter.cancelDiscovery();
}
activity.unregisterReceiver(bluetoothReceiver);
注意:搜索结束之后需要停止搜索,否则非常耗电,还浪费性能。
三、蓝牙自动配对
1、获取需要配对的蓝牙设备和pin码
String deviceId = data.deviceId;
String pin = data.pin;
deviceId:需要配对的设备。
pin:设置的配对码。
2、开始配对
BluetoothAdapter bluetoothAdapter = bluetoothManager.getAdapter();
BluetoothDevice remoteDevice = bluetoothAdapter.getRemoteDevice(deviceId);
if (remoteDevice.getBondState() == BluetoothDevice.BOND_BONDED) {//已经配对了LogUtils.d("已经配对了");
} else {//没有配对//监听配对状态IntentFilter intentFilter = new IntentFilter();intentFilter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED); //配对状态监听intentFilter.addAction(BluetoothDevice.ACTION_PAIRING_REQUEST); //配对请求makeBluetoothPairReceiver = new MakeBluetoothPairReceiver();activity.registerReceiver(makeBluetoothPairReceiver, intentFilter);//开始配对remoteDevice.setPin(pinByte);remoteDevice.createBond();
}public class MakeBluetoothPairReceiver extends BroadcastReceiver {@Overridepublic void onReceive(Context context, Intent intent) {String action = intent.getAction();if (TextUtils.equals(action, BluetoothDevice.ACTION_BOND_STATE_CHANGED)) {BluetoothDevice bluetoothDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);int bondSate = bluetoothDevice.getBondState();switch (bondSate) {case BluetoothDevice.BOND_NONE:LogUtils.d("已解除配对");unMakeBluetoothPairReceiver();break;case BluetoothDevice.BOND_BONDING:LogUtils.d("正在配对...");break;case BluetoothDevice.BOND_BONDED:LogUtils.d("配对成功");unMakeBluetoothPairReceiver();break;}} else if (TextUtils.equals(action, BluetoothDevice.ACTION_PAIRING_REQUEST)) {int type = intent.getIntExtra(BluetoothDevice.EXTRA_PAIRING_VARIANT, BluetoothDevice.ERROR);LogUtils.d("ACTION_PAIRING_REQUEST:" + type);// abortBroadcast();该方法使配对失败,先不用了}}
}public void unMakeBluetoothPairReceiver() {if (makeBluetoothPairReceiver != null) {activity.unregisterReceiver(makeBluetoothPairReceiver);}
}
四、获取已经连接的蓝牙信息
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
JsCreateDevicesBean jsCreateDevicesBean = new JsCreateDevicesBean();
if (pairedDevices.size() > 0) {List<JsCreateDevicesBean.DeviceData> list = jsCreateDevicesBean.devices;for (BluetoothDevice device : pairedDevices) {JsCreateDevicesBean.DeviceData deviceData = new JsCreateDevicesBean.DeviceData();deviceData.deviceId = device.getAddress();deviceData.name = device.getName();LogUtils.d("BlueTooth已配对的设备:" + device.getAddress() + "----" + device.getName() + "----" + device.getUuids().toString());}
}
Android经典蓝牙相关推荐
- Android经典蓝牙开发全流程
一.基本介绍 所谓蓝牙(Bluetooth)技术,实际上是一种短距离无线电技术,最初是由爱立信公司公司发明的.技术始于爱立信公司 1994 方案,它是研究在移动电话和其他配件间进行低功耗.低成本无 ...
- Android 经典蓝牙开发
本文主要讲解经典蓝牙的开发,主要包含以下几个知识点: 蓝牙 API 简介 经典蓝牙开发的一般步骤 相信通过以上步骤,您会很快上手一个 Android 经典蓝牙开发的 App. 蓝牙 API 简介 An ...
- Android经典蓝牙开发简介(Google官网译文)
公司的项目最近需要用到蓝牙开发的相关内容,因此特地查阅了Google官方文档的内容并进行二次整理,希望能对需要学习该部分的朋友有所帮助. 原文地址:http://developer.android.c ...
- android ble 经典蓝牙,Android 经典蓝牙(Classic Bluetooth)和低功耗蓝牙(BLE)
[实例简介] 从蓝牙4.0开始包含两个蓝牙芯片模块:传统/经典蓝牙模块(Classic Bluetooth,简称BT)和低功耗蓝牙(Bluetooth Low Energy,简称BLE) 经典蓝牙是在 ...
- Android 经典蓝牙与 BLE 蓝牙基础
1. 蓝牙规范简介 蓝牙是一种无线技术标准,用来让固定与移动设备,在短距离间交换数据,以形成个人局域网(PAN).其使用短波特高频(UHF)无线电波,由 2.4 至 2.485 GHz 的 ISM 频 ...
- Android 经典蓝牙(Classic Bluetooth)和低功耗蓝牙(BLE)
从蓝牙4.0开始包含两个蓝牙芯片模块:传统/经典蓝牙模块(Classic Bluetooth,简称BT)和低功耗蓝牙(Bluetooth Low Energy,简称BLE) 经典蓝牙是在之前的蓝牙1. ...
- Android经典蓝牙相关知识
1 蓝牙基础知识 1.1 蓝牙相关的权限 <!--想要用蓝牙进行通信则要申明bluetooth权限--> <uses-permission android:name="an ...
- ANDROID经典蓝牙通讯
最近写了一个关于蓝牙的操作类,对蓝牙不熟悉的同学可以参考一下 功能包括 发现,搜索,连接,发送,接收等/ 直接上代码: package com.***.****.BlueToothM;import a ...
- Android蓝牙开发 — 经典蓝牙BLE蓝牙
一,前期基础知识储备 1)蓝牙是一种支持设备之间短距离通信的无线电技术(其他还包括红外,WIFI): 支持移动电话.笔记本电脑.无线耳机等设备之间进行信息的交换: Android支持的蓝牙协议栈:Bl ...
最新文章
- OpenStack入门修炼之实战--实现阿里云ESC多FLAT网络(21)
- 理解JavaScript的原始类型
- 【IPFS + 区块链 系列】 入门篇 - IPFS + Ethereum (下篇)-ipfs + Ethereum 大图片存储
- Linux内核--网络协议栈深入分析(二)--sk_buff的操作函数
- docker中使用golang:alpine镜像制作开启goweb的dockerfile
- Java面试之JVM参数调优
- 小程序识别车牌php,微信小程序——车牌键盘输入js+css
- linux读写文件测试,Linux下各种主要文件系统的读写性能测试
- pytorch torch.stack
- c语言 原码反码和补码
- jj为什么会变大变小_胡杏儿怀二胎不解鼻子为何变大?其实,还有三个部位变大的更厉害...
- python生成验证码的程序_Python基础篇生成4位随机验证码
- 如何转型成为一家真正发挥大数据作用的 “数据驱动型公司”?
- Windows 8 系统安装
- 收藏!豆瓣9.0分TOP100职场终极必读书单!大萌哥整理
- halcon学习笔记4-字符识别(包括汉字识别)
- 关于虚拟机复制文件时:无法确定本地文件类型。您可能没有执行此操作的权限。 正在取消文件复制操作。的决解方法
- Keil AC5/Keil AC6/IAR指定数据绝对存储地址
- □ 影片名:《陈好-女人要爱自己》(7012) 在线播放
- 国内cdn免费加速出现了,您知道吗?