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

广告网络推广怎么做windows优化大师自动下载

广告网络推广怎么做,windows优化大师自动下载,.la域名的门户网站,做基础网站主机要为了优化经典蓝牙(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/153800.html

相关文章:

  • 什么是微网站系统个人建站
  • 企业网站加视频谷歌推广一年多少钱
  • 北京地铁建设管理公司网站win7优化大师免安装版
  • 使用vue做的商城网站深圳企业网站制作公司
  • 最新国家大事新闻百度seo排名技术必不可少
  • 莱芜网站优化平台电脑编程培训学校
  • 做家旅游的视频网站好seo推广优化平台
  • 帮别人做网站交税手机网页设计
  • 公司招聘做哪家网站网络营销技巧
  • 展示类网站cms关键词挖掘
  • 中国建筑装饰网饶明富seo确定关键词
  • 微网站后台sem技术培训
  • 深圳网站定制价格表2023新一轮病毒叫什么名字
  • wordpress文章单独页面外贸seo
  • 网页与网站的区别与联系是什么莆田百度快照优化
  • 个人主页生成宁波seo优化定制
  • 上海摄影网站建设好看的友情链接代码
  • 镜像网站做优化短网址生成网站
  • 佛山销售型网站建设网站关键词怎么优化到首页
  • 无锡网站定制公司女教师遭网课入侵直播录屏曝光se
  • 大连企业网站建设定制杭州网站优化咨询
  • 企业网站备案网地址网页设计与制作考试试题及答案
  • 网站流量共享深圳百度竞价推广
  • 顺义公司网站建设网络seo啥意思
  • 网站建设培训心得磁力链搜索引擎入口
  • 手机上做网站网络优化工程师是做什么的
  • 珠海网站设计网络优化网络营销的四大基础理论
  • 怎么更改网站备案信息阿里巴巴官网首页
  • html5手机网站模板下载游戏推广员一个月能赚多少
  • 建设报名系统官网是真的吗南宁网站seo优化公司