Android蓝牙SDK设计,GitHub - RelinRan/BluetoothSDK: Android蓝牙SDK | 蓝牙广播 | BLE蓝牙 | SPP 蓝牙...

本文介绍了如何在Android中使用蓝牙SDK进行SPP和BLE通信。详细阐述了依赖引入、蓝牙搜索、设备连接、数据读写及监听接口的实现,包括权限申请、客户端和服务端的连接流程,适用于Android应用中的蓝牙交互场景。
摘要由CSDN通过智能技术生成

BluetoothSDK

Android蓝牙SDK | 蓝牙广播 | BLE蓝牙 | SPP 蓝牙

方法一 ARR依赖

android {

....

repositories {

flatDir {

dirs 'libs'

}

}

}

dependencies {

implementation(name: 'BluetoothSDK', ext: 'aar')

}

方法二 JitPack依赖

A.项目/build.grade

allprojects {

repositories {

...

maven { url 'https://jitpack.io' }

}

}

B.项目/app/build.grade

dependencies {

implementation 'com.github.RelinRan:BluetoothSDK:1.0.1'

}

蓝牙搜索

//权限申请

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

requestPermissions(new String[]{Manifest.permission.BLUETOOTH, Manifest.permission.BLUETOOTH_ADMIN,Manifest.permission.ACCESS_COARSE_LOCATION}, 100);

}

//权限申请成功进行蓝牙搜索,Bluetooth.MODE_SPP为经典搜索方式;Bluetooth.MODE_BLE为广播搜索方式

Bluetooth.with(this).enable(false).startScan(Bluetooth.MODE_SPP, onBluetoothScanListener);

蓝牙搜索监听

public interface OnBluetoothScanListener {

/**

* 发现新设备(SPP)

*

* @param device

*/

void onBluetoothFound(BluetoothDevice device);

/**

* 设备扫描完毕(SPP)

*/

void onBluetoothDiscoveryFinished();

/**

* 设备绑定状态(SPP)

*

* @param state {@link Bluetooth#BOND_BONDED}

*/

void onBluetoothBondStateChanged(int state);

/**

* 设备连接成功(SPP)

*

* @param device

*/

void onBluetoothConnected(BluetoothDevice device);

/**

* 设备断开连接(SPP)

*

* @param device

*/

void onBluetoothDisconnected(BluetoothDevice device);

/**

* 低功耗设备扫描结果(BLE)

*

* @param callbackType {@link Bluetooth#CALLBACK_TYPE_ALL_MATCHES}

* @param result

*/

void onBluetoothLeScanResult(int callbackType, ScanResult result);

/**

* 低功耗批量处理扫描结果(BLE)

*

* @param results

*/

void onBluetoothLeBatchScanResults(List results);

/**

* 低功耗扫描失败(BLE)

*

* @param error

*/

void onBluetoothScanFailed(int error);

}

蓝牙客户端连接

BUUID buuid = new BUUID();

//以下UUID如果不设置会有默认UUID

buuid.put(BUUID.TYPE_SERVICE, "服务UUID");//默认:BUUID.DEFAULT_UUID_SERVICE

buuid.put(BUUID.TYPE_CHARACTERISTICS_READ, "特征UUID-读");//默认:BUUID.DEFAULT_UUID_CHARACTERISTIC_READ

buuid.put(BUUID.TYPE_CHARACTERISTICS_WRITE, "特征UUID-写");//默认:BUUID.DEFAULT_UUID_CHARACTERISTIC_WRITE

buuid.put(BUUID.TYPE_CHARACTERISTICS_NOTIFY, "特征UUID-通知");//默认:DEFAULT_UUID_CHARACTERISTIC_NOTIFY

buuid.put(BUUID.TYPE_DESCRIPTOR, "描述UUID");//默认:BUUID.DEFAULT_UUID_DESCRIPTOR

client = new BluetoothClient(context, BluetoothClient.MODE_SPP, "扫描出的蓝牙地址", buuid);

client.setOnBluetoothClientListener(onBluetoothClientListener);

client.connect();

蓝牙客户端连接监听

public interface OnBluetoothClientListener {

/**

* 连接成功

*

* @param mode 模式

* @param device 设备

* @param gatt Ble对象

*/

void onBluetoothClientConnected(int mode, BluetoothDevice device, BluetoothGatt gatt);

/**

* 断开连接

*

* @param mode 模式

* @param device 设备

* @param gatt Ble对象

*/

void onBluetoothClientDisconnect(int mode, BluetoothDevice device, BluetoothGatt gatt);

/**

* 错误

*

* @param mode 模式

* @param device 设备

* @param gatt Ble对象

* @param code 错误代码

* @param e 异常信息

*/

void onBluetoothClientError(int mode, BluetoothDevice device, BluetoothGatt gatt, int code, IOException e);

/**

* 读取数据

*

* @param mode 模式

* @param device 设备

* @param gatt Ble对象

* @param dataType 数据类型

* @param data 数据

*/

void onBluetoothClientRead(int mode, BluetoothDevice device, BluetoothGatt gatt, int dataType, byte[] data);

/**

* 写入数据

*

* @param mode 模式

* @param device 设备

* @param gatt Ble对象

* @param dataType 数据类型

* @param data 数据

*/

void onBluetoothClientWrite(int mode, BluetoothDevice device, BluetoothGatt gatt, int dataType, byte[] data);

/**

* 新服务发现

*

* @param mode 模式

* @param device 设备

* @param gatt Ble对象

* @param status 状态

*/

void onBluetoothClientServicesDiscovered(int mode, BluetoothDevice device, BluetoothGatt gatt, int status);

}

蓝牙服务端连接

BUUID buuid = new BUUID();

//以下UUID如果不设置会有默认UUID

buuid.put(BUUID.TYPE_SERVICE, "服务UUID");//默认:BUUID.DEFAULT_UUID_SERVICE

buuid.put(BUUID.TYPE_CHARACTERISTICS_READ, "特征UUID-读");//默认:BUUID.DEFAULT_UUID_CHARACTERISTIC_READ

buuid.put(BUUID.TYPE_CHARACTERISTICS_WRITE, "特征UUID-写");//默认:BUUID.DEFAULT_UUID_CHARACTERISTIC_WRITE

buuid.put(BUUID.TYPE_CHARACTERISTICS_NOTIFY, "特征UUID-通知");//默认:DEFAULT_UUID_CHARACTERISTIC_NOTIFY

buuid.put(BUUID.TYPE_DESCRIPTOR, "描述UUID");//默认:BUUID.DEFAULT_UUID_DESCRIPTOR

server = new BluetoothServer(context, BluetoothClient.MODE_SPP, "服务名称", buuid);

//广播数据配置,注意:mode = BluetoothClient.MODE_BLE 才能使用广播模式;BluetoothClient.MODE_SPP 模式下删除以下配置

server.setBleManufacturerId(520);

server.setBleManufacturerData("11".getBytes());

//server.setBleServiceData("22".getBytes());//manufacturer 和 Service 数据只能选一种,不然就报错数据传输过大错误。

//设置监听开启服务

server.setOnBluetoothServerListener(this);

server.start();

蓝牙服务端连接监听

public interface OnBluetoothServerListener {

/**

* 开启广播成功

*

* @param mode 模式

* @param settingsInEffect 设置

*/

void onBluetoothServerStartAdvertiseSuccess(int mode, AdvertiseSettings settingsInEffect);

/**

* 开启广播失败

*

* @param mode 模式

* @param error 错误代码

*/

void onBluetoothServerStartAdvertiseFailure(int mode, int error);

/**

* 服务添加

*

* @param mode 模式

* @param service 服务

* @param status 状态

*/

void onBluetoothServerServiceAdded(int mode, BluetoothGattService service, int status);

/**

* 连接成功

*

* @param mode 模式

* @param device 设备

*/

void onBluetoothServerConnected(int mode, BluetoothDevice device);

/**

* 断开连接

*

* @param mode 模式

* @param device 设备

*/

void onBluetoothServerDisconnect(int mode, BluetoothDevice device);

/**

* 读取数据

*

* @param mode 模式

* @param device 设备

* @param dataType 数据类型

* @param data 数据

*/

void onBluetoothServerRead(int mode, BluetoothDevice device, int dataType, byte[] data);

/**

* 写入数据

*

* @param mode 模式

* @param device 设备

* @param dataType 数据类型

* @param data 数据

*/

void onBluetoothServerWrite(int mode, BluetoothDevice device, int dataType, byte[] data);

/**

* 错误

*

* @param mode 模式

* @param device 设备

* @param code 错误代码

* @param e 异常信息

*/

void onBluetoothServerError(int mode, BluetoothDevice device, int code, IOException e);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值