当前位置: 首页 > wzjs >正文

海口手机版网站建设设计师网站

海口手机版网站建设,设计师网站,python前端开发,图片展示型网站为了优化经典蓝牙(Classic Bluetooth)和低功耗蓝牙(Bluetooth Low Energy, BLE)的操作,我们可以将功能封装到一个工具类中,支持扫描、连接、通信,并兼容高版本 Android 的动态权限申请。以下是完…

为了优化经典蓝牙(Classic Bluetooth)和低功耗蓝牙(Bluetooth Low Energy, BLE)的操作,我们可以将功能封装到一个工具类中,支持扫描、连接、通信,并兼容高版本 Android 的动态权限申请。以下是完整的工具类实现。

  1. 工具类功能
    经典蓝牙:

扫描设备。

连接设备。

发送和接收数据。

BLE 蓝牙:

扫描设备。

连接设备。

发送和接收数据(通过 GATT 特征值)。

权限管理:

动态申请权限(包括 ACCESS_FINE_LOCATION、BLUETOOTH_SCAN、BLUETOOTH_CONNECT)。

高版本兼容:

支持 Android 12 及以上版本的权限要求。

  1. 工具类实现
    2.1 BluetoothHelper.java
import android.Manifest;
import android.annotation.SuppressLint;
import android.bluetooth.*;
import android.bluetooth.le.BluetoothLeScanner;
import android.bluetooth.le.ScanCallback;
import android.bluetooth.le.ScanResult;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;public class BluetoothHelper {private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); // SPP UUIDprivate static final int PERMISSION_REQUEST_CODE = 100;private Context context;private BluetoothAdapter bluetoothAdapter;private BluetoothLeScanner bleScanner;private BluetoothSocket bluetoothSocket;private BluetoothGatt bluetoothGatt;private Handler handler;private List<BluetoothDevice> classicDevices = new ArrayList<>();private List<BluetoothDevice> bleDevices = new ArrayList<>();// BLE 相关变量private BluetoothGattCharacteristic writeCharacteristic;private BluetoothGattCharacteristic notifyCharacteristic;// 回调接口public interface ScanCallback {void onClassicDeviceFound(BluetoothDevice device);void onBleDeviceFound(BluetoothDevice device);void onScanFailed(String error);}public interface ConnectionCallback {void onConnected();void onConnectionFailed(String error);}public interface DataCallback {void onDataReceived(byte[] data);void onDataSent(boolean success);}public BluetoothHelper(Context context) {this.context = context;this.handler = new Handler(Looper.getMainLooper());this.bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();if (bluetoothAdapter != null) {this.bleScanner = bluetoothAdapter.getBluetoothLeScanner();}}// 检查权限public boolean checkPermissions() {if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {return ContextCompat.checkSelfPermission(context, Manifest.permission.BLUETOOTH_SCAN) == PackageManager.PERMISSION_GRANTED &&ContextCompat.checkSelfPermission(context, Manifest.permission.BLUETOOTH_CONNECT) == PackageManager.PERMISSION_GRANTED &&ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED;} else {return ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED;}}// 请求权限public void requestPermissions() {if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {ActivityCompat.requestPermissions((MainActivity) context,new String[]{Manifest.permission.BLUETOOTH_SCAN,Manifest.permission.BLUETOOTH_CONNECT,Manifest.permission.ACCESS_FINE_LOCATION},PERMISSION_REQUEST_CODE);} else {ActivityCompat.requestPermissions((MainActivity) context,new String[]{Manifest.permission.ACCESS_FINE_LOCATION},PERMISSION_REQUEST_CODE);}}// 开始扫描经典蓝牙设备public void startClassicScan(ScanCallback callback) {if (!checkPermissions()) {callback.onScanFailed("Permissions not granted");return;}classicDevices.clear();IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);context.registerReceiver(new BroadcastReceiver() {@Overridepublic void onReceive(Context context, Intent intent) {String action = intent.getAction();if (BluetoothDevice.ACTION_FOUND.equals(action)) {BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);classicDevices.add(device);callback.onClassicDeviceFound(device);}}}, filter);bluetoothAdapter.startDiscovery();}// 开始扫描 BLE 设备@SuppressLint("MissingPermission")public void startBleScan(ScanCallback callback) {if (!checkPermissions()) {callback.onScanFailed("Permissions not granted");return;}bleDevices.clear();bleScanner.startScan(new ScanCallback() {@Overridepublic void onScanResult(int callbackType, ScanResult result) {BluetoothDevice device = result.getDevice();bleDevices.add(device);callback.onBleDeviceFound(device);}@Overridepublic void onScanFailed(int errorCode) {callback.onScanFailed("BLE scan failed with error code: " + errorCode);}});}// 停止扫描@SuppressLint("MissingPermission")public void stopScan() {bluetoothAdapter.cancelDiscovery();if (bleScanner != null) {bleScanner.stopScan(null);}}// 连接经典蓝牙设备public void connectClassicDevice(BluetoothDevice device, ConnectionCallback callback) {new Thread(() -> {try {bluetoothSocket = device.createRfcommSocketToServiceRecord(MY_UUID);bluetoothSocket.connect();handler.post(callback::onConnected);} catch (IOException e) {handler.post(() -> callback.onConnectionFailed(e.getMessage()));}}).start();}// 连接 BLE 设备@SuppressLint("MissingPermission")public void connectBleDevice(BluetoothDevice device, BluetoothGattCallback gattCallback) {bluetoothGatt = device.connectGatt(context, false, gattCallback);}// 发送数据(经典蓝牙)public void sendClassicData(byte[] data, DataCallback callback) {new Thread(() -> {try {OutputStream outputStream = bluetoothSocket.getOutputStream();outputStream.write(data);handler.post(() -> callback.onDataSent(true));} catch (IOException e) {handler.post(() -> callback.onDataSent(false));}}).start();}// 接收数据(经典蓝牙)public void receiveClassicData(DataCallback callback) {new Thread(() -> {try {InputStream inputStream = bluetoothSocket.getInputStream();byte[] buffer = new byte[1024];int bytes;while ((bytes = inputStream.read(buffer)) != -1) {byte[] receivedData = new byte[bytes];System.arraycopy(buffer, 0, receivedData, 0, bytes);handler.post(() -> callback.onDataReceived(receivedData));}} catch (IOException e) {handler.post(() -> callback.onDataReceived(null));}}).start();}// 发送数据(BLE)@SuppressLint("MissingPermission")public void sendBleData(byte[] data, DataCallback callback) {if (writeCharacteristic != null && bluetoothGatt != null) {writeCharacteristic.setValue(data);bluetoothGatt.writeCharacteristic(writeCharacteristic);handler.post(() -> callback.onDataSent(true));} else {handler.post(() -> callback.onDataSent(false));}}// 启用通知以接收数据(BLE)@SuppressLint("MissingPermission")public void enableBleNotifications(BluetoothGattCharacteristic characteristic, DataCallback callback) {if (bluetoothGatt != null) {bluetoothGatt.setCharacteristicNotification(characteristic, true);BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID.fromString("00002902-0000-1000-8000-00805f9b34fb"));if (descriptor != null) {descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);bluetoothGatt.writeDescriptor(descriptor);}}}// 断开连接public void disconnect() {try {if (bluetoothSocket != null) {bluetoothSocket.close();}if (bluetoothGatt != null) {bluetoothGatt.disconnect();bluetoothGatt.close();}} catch (IOException e) {e.printStackTrace();}}
}

2.2 使用示例
扫描设备

BluetoothHelper bluetoothHelper = new BluetoothHelper(this);// 扫描经典蓝牙设备
bluetoothHelper.startClassicScan(new BluetoothHelper.ScanCallback() {@Overridepublic void onClassicDeviceFound(BluetoothDevice device) {Log.d("BluetoothHelper", "Classic Device Found: " + device.getName());}@Overridepublic void onBleDeviceFound(BluetoothDevice device) {Log.d("BluetoothHelper", "BLE Device Found: " + device.getName());}@Overridepublic void onScanFailed(String error) {Log.e("BluetoothHelper", "Scan Failed: " + error);}
});// 扫描 BLE 设备
bluetoothHelper.startBleScan(new BluetoothHelper.ScanCallback() {@Overridepublic void onClassicDeviceFound(BluetoothDevice device) {Log.d("BluetoothHelper", "Classic Device Found: " + device.getName());}@Overridepublic void onBleDeviceFound(BluetoothDevice device) {Log.d("BluetoothHelper", "BLE Device Found: " + device.getName());}@Overridepublic void onScanFailed(String error) {Log.e("BluetoothHelper", "Scan Failed: " + error);}
});

连接设备

// 连接经典蓝牙设备
bluetoothHelper.connectClassicDevice(device, new BluetoothHelper.ConnectionCallback() {@Overridepublic void onConnected() {Log.d("BluetoothHelper", "Classic Device Connected");}@Overridepublic void onConnectionFailed(String error) {Log.e("BluetoothHelper", "Connection Failed: " + error);}
});// 连接 BLE 设备
bluetoothHelper.connectBleDevice(device, new BluetoothGattCallback() {@Overridepublic void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {if (newState == BluetoothProfile.STATE_CONNECTED) {Log.d("BluetoothHelper", "BLE Device Connected");} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {Log.d("BluetoothHelper", "BLE Device Disconnected");}}
});

经典蓝牙发送和接收数据

// 发送数据(经典蓝牙)
bluetoothHelper.sendClassicData("Hello Bluetooth".getBytes(), new BluetoothHelper.DataCallback() {@Overridepublic void onDataReceived(byte[] data) {// 处理接收到的数据}@Overridepublic void onDataSent(boolean success) {Log.d("BluetoothHelper", "Data Sent: " + success);}
});// 接收数据(经典蓝牙)
bluetoothHelper.receiveClassicData(new BluetoothHelper.DataCallback() {@Overridepublic void onDataReceived(byte[] data) {Log.d("BluetoothHelper", "Data Received: " + new String(data));}@Overridepublic void onDataSent(boolean success) {// 无需处理}
});

BLE蓝牙发送和接收数据

// 连接 BLE 设备
bluetoothHelper.connectBleDevice(device, new BluetoothGattCallback() {@Overridepublic void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {if (newState == BluetoothProfile.STATE_CONNECTED) {gatt.discoverServices();}}@Overridepublic void onServicesDiscovered(BluetoothGatt gatt, int status) {if (status == BluetoothGatt.GATT_SUCCESS) {BluetoothGattService service = gatt.getService(UUID.fromString("你的服务UUID"));if (service != null) {writeCharacteristic = service.getCharacteristic(UUID.fromString("写特征值UUID"));notifyCharacteristic = service.getCharacteristic(UUID.fromString("通知特征值UUID"));// 启用通知bluetoothHelper.enableBleNotifications(notifyCharacteristic, new BluetoothHelper.DataCallback() {@Overridepublic void onDataReceived(byte[] data) {Log.d("BluetoothHelper", "BLE Data Received: " + new String(data));}@Overridepublic void onDataSent(boolean success) {// 无需处理}});}}}@Overridepublic void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {byte[] data = characteristic.getValue();Log.d("BluetoothHelper", "BLE Notification Data: " + new String(data));}
});// 发送数据(BLE)
bluetoothHelper.sendBleData("Hello BLE".getBytes(), new BluetoothHelper.DataCallback() {@Overridepublic void onDataReceived(byte[] data) {// 无需处理}@Overridepublic void onDataSent(boolean success) {Log.d("BluetoothHelper", "BLE Data Sent: " + success);}
});

2.3 注意事项
权限管理:

在 Android 12 及以上版本,需要动态申请 BLUETOOTH_SCAN 和 BLUETOOTH_CONNECT 权限。

在 Android 11 及以下版本,需要动态申请 ACCESS_FINE_LOCATION 权限。

高版本兼容:

使用 @SuppressLint(“MissingPermission”) 忽略权限检查,确保在实际运行时已授予权限。

线程管理:

经典蓝牙的通信操作应在后台线程中进行,避免阻塞主线程。
通过以上工具类,你可以轻松实现经典蓝牙和 BLE 的扫描、连接和通信功能,并兼容高版本 Android 的权限要求。

http://www.dtcms.com/wzjs/267207.html

相关文章:

  • 商城网站建设最近比较火的关键词
  • 响水网站建设服务商培训心得体会1000字
  • 荣成市信用建设网站网络公司取什么名字好
  • 制作网站是什么专业短视频seo营销
  • 金环建设集团网站百度网盘在线登录入口
  • 长沙装修公司性价比最高的是哪个青海seo技术培训
  • 巴中市建设局新网站上google必须翻墙吗
  • 中国企业500强2023seo排名赚app
  • 微信广告推广如何收费西安网络seo公司
  • 网站是如何建设的网站建设多少钱
  • 后端低代码平台青岛seo网络优化公司
  • 纯ajax网站如何做seo网站推广的一般流程是
  • 自己做的博客网站公司网页制作流程
  • 哪一些网站使用vue做的小红书推广怎么做
  • 网站 做 vga旅行网站排名
  • 北京梦活力网站建设首页关键词排名优化
  • 淮南网站建设价格seo入门基础教程
  • 钟表珠宝商城网站建设深圳优化公司统高粱seo
  • 网站组织结构图谷歌seo视频教程
  • 长春市防疫最新消息数据网站关键词优化的步骤和过程
  • 做国外网站衣服码数要怎么写可口可乐搜索引擎营销案例
  • 站长工具seo优化建议网站大全软件下载
  • 成都响应式网站网络营销与网站推广的区别
  • 如何做网站的薪酬调查餐饮培训
  • 北京建设银行公积金提取网站高中同步测控优化设计答案
  • 在线网站地图生成器系统优化大师下载
  • 中国有什么网站做跨境零售武汉seo首页
  • WordPress的黑色框谷歌外贸seo
  • 建站工具 开源为什么打开网址都是站长工具
  • 网站设计 优帮云百度搜索风云榜电脑版