蓝牙设置和实现

news/2024/10/18 16:54:46/

蓝牙

为了让支持蓝牙的设备能够在彼此之间传输数据,它们必须先通过配对过程形成通信通道。其中一台设备(可检测到的设备)需将自身设置为可接收传入的连接请求。另一台设备会使用服务发现过程找到此可检测到的设备。在可检测到的设备接受配对请求后,这两台设备会完成绑定过程,并在此期间交换安全密钥。二者会缓存这些密钥,以供日后使用。完成配对和绑定过程后,两台设备会交换信息。当会话完成时,发起配对请求的设备会发布已将其链接到可检测设备的通道。但是,这两台设备仍保持绑定状态,因此在未来的会话期间,只要二者在彼此的范围内且均未移除绑定,便可自动重新连接。

声明权限

<manifest ... ><uses-permission android:name="android.permission.BLUETOOTH" /><uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /><!-- If your app targets Android 9 or lower, you can declareACCESS_COARSE_LOCATION instead. --><uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />...
</manifest>

使用配置文件

BluetoothHeadset bluetoothHeadset;// Get the default adapter
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();private BluetoothProfile.ServiceListener profileListener = new BluetoothProfile.ServiceListener() {public void onServiceConnected(int profile, BluetoothProfile proxy) {if (profile == BluetoothProfile.HEADSET) {bluetoothHeadset = (BluetoothHeadset) proxy;}}public void onServiceDisconnected(int profile) {if (profile == BluetoothProfile.HEADSET) {bluetoothHeadset = null;}}
};// Establish connection to the proxy.
bluetoothAdapter.getProfileProxy(context, profileListener, BluetoothProfile.HEADSET);// ... call functions on bluetoothHeadset// Close proxy connection after use.
bluetoothAdapter.closeProfileProxy(bluetoothHeadset);

设置蓝牙

BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (bluetoothAdapter == null) {// Device doesn't support Bluetooth
}

作为服务器连接

private class AcceptThread extends Thread {private final BluetoothServerSocket mmServerSocket;public AcceptThread() {// Use a temporary object that is later assigned to mmServerSocket// because mmServerSocket is final.BluetoothServerSocket tmp = null;try {// MY_UUID is the app's UUID string, also used by the client code.tmp = bluetoothAdapter.listenUsingRfcommWithServiceRecord(NAME, MY_UUID);} catch (IOException e) {Log.e(TAG, "Socket's listen() method failed", e);}mmServerSocket = tmp;}public void run() {BluetoothSocket socket = null;// Keep listening until exception occurs or a socket is returned.while (true) {try {socket = mmServerSocket.accept();} catch (IOException e) {Log.e(TAG, "Socket's accept() method failed", e);break;}if (socket != null) {// A connection was accepted. Perform work associated with// the connection in a separate thread.manageMyConnectedSocket(socket);mmServerSocket.close();break;}}}// Closes the connect socket and causes the thread to finish.public void cancel() {try {mmServerSocket.close();} catch (IOException e) {Log.e(TAG, "Could not close the connect socket", e);}}
}

作为客户端连接

private class ConnectThread extends Thread {private final BluetoothSocket mmSocket;private final BluetoothDevice mmDevice;public ConnectThread(BluetoothDevice device) {// Use a temporary object that is later assigned to mmSocket// because mmSocket is final.BluetoothSocket tmp = null;mmDevice = device;try {// Get a BluetoothSocket to connect with the given BluetoothDevice.// MY_UUID is the app's UUID string, also used in the server code.tmp = device.createRfcommSocketToServiceRecord(MY_UUID);} catch (IOException e) {Log.e(TAG, "Socket's create() method failed", e);}mmSocket = tmp;}public void run() {// Cancel discovery because it otherwise slows down the connection.bluetoothAdapter.cancelDiscovery();try {// Connect to the remote device through the socket. This call blocks// until it succeeds or throws an exception.mmSocket.connect();} catch (IOException connectException) {// Unable to connect; close the socket and return.try {mmSocket.close();} catch (IOException closeException) {Log.e(TAG, "Could not close the client socket", closeException);}return;}// The connection attempt succeeded. Perform work associated with// the connection in a separate thread.manageMyConnectedSocket(mmSocket);}// Closes the client socket and causes the thread to finish.public void cancel() {try {mmSocket.close();} catch (IOException e) {Log.e(TAG, "Could not close the client socket", e);}}
}

运行结果截图

在这里插入图片描述


http://www.ppmy.cn/news/836855.html

相关文章

设置蓝牙主从机上电自动配对的方法

有时候&#xff0c;我们希望蓝牙的主机在上电之后自动完成与从机的连接&#xff0c;而无需在每次上电后都发送一些AT指令来完成主从机的配对&#xff0c;该怎么做呢&#xff1f; 1、首先完成从机的设置&#xff1a; ATROLE0 //设置为从机 ATPSWDxxxx //xxxx是配对密码&#xff…

蓝牙(一)蓝牙设置详解

一.蓝牙相关的类 1&#xff09;BluetoothSettings.java 该类是蓝牙配置和连接管理界面&#xff0c;就是咱们常见的蓝牙界面。它管理着蓝牙界面的加载&#xff0c;蓝牙搜索&#xff0c;蓝牙连接&#xff0c;蓝牙重命名等管理功能。(顺便借用一下大牛做的图片&#xff1a;https:/…

软件工程——第12章面向对象实现知识点整理

本专栏是博主个人笔记&#xff0c;主要目的是利用碎片化的时间来记忆软工知识点&#xff0c;特此声明&#xff01; 文章目录 1. 面向对象语言技术的特点&#xff1f; 2.选择面向对象程序设计语言时主要应该考虑哪些因素&#xff1f; 3.面向对象设计结果只能用面向对象语言实…

微信小程序音频播放问题createInnerAudioContext为何不调用onTimeUpdate

废话不多说&#xff0c;直接这么写就可以解决&#xff1a; innerAudioContext.play(); innerAudioContext.onCanplay(() > { innerAudioContext.play(); console.log(开始播放) }); innerAudioContext.onWaiting(() > { innerAudioCo…

Android上调用百度人脸识别接口

写了一个简单的调用百度人脸识别在线的JAVA接口&#xff0c;识别率挺高的&#xff0c;但是目前写得还不是很全面&#xff0c;然后打开相册选取图片那一部分&#xff0c;以及工具类代码&#xff0c;是有参考别人写的代码&#xff0c;具体是摘自哪里之前没有保存。 这里贴一些主…

Android 人脸识别了解一下 (中)

转载请注明作者及出处&#xff1a;https://www.jianshu.com/p/b41f64389c21 [25]——Android 人脸识别了解一下 &#xff08;上&#xff09; 在上文中我大致的介绍了官方 Demo 中人脸注册的流程&#xff0c;本文我们接着来看看&#xff0c;在完成了人脸注册之后我们该如何识别…

Adnroid 使用安卓自带的人脸识别API

缺点是精度不高&#xff0c;识别信息很少&#xff0c;只有眼睛的识别 MainActivity.java import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Color; import and…

基于Android平台实现人脸识别

前言 在深度学习出现后&#xff0c;人脸识别技术才真正有了可用性。这是因为之前的机器学习技术中&#xff0c;难以从图片中取出合适的特征值。轮廓&#xff1f;颜色&#xff1f;眼睛&#xff1f;如此多的面孔&#xff0c;且随着年纪、光线、拍摄角度、气色、表情、化妆、佩饰挂…