Android 连接多蓝牙设备, 蓝牙与多设备连接, 蓝牙 ble 多设备并发操作, Android 连接不了, Android ble 开发框架, Android 连接蓝牙总结
前言
小白请绕道百度, 本文适合有一定 Android,ble 蓝牙, 面向对象基础的同学进阶探讨, 只讲关键技术点, 细节自行脑补
看过很多蓝牙 demo, 开源库, 没发现真正以面向对象的思维写的, 把自己的一套框架开源出来, 希望对看到的有缘人有用, 特别是面向对象思维方面. 不是说定义了类, 就叫面向对象, 希望你能领悟
此框架支持多种不同类型的 ble 设备, 同时连接, 收发数据, 互不干扰. 比如 App 同时连两个 LED 蓝牙灯, 两个手环, 一个蓝牙加热器.(不可超过 7 个, 极少数手机不可超过 5 个)
当然连接单个 ble 设备, 或者只连接一种 ble 设备同样适用本框架.
GitHub 源码: https://github.com/ruigeyun/Android-DualBle
- public class LedDevice extends BLEAppDevice {
- private final String TAG = "BLELedDevice";
- public static final Integer DEVICE_TYPE_ID = 1002;
- public static final UUID SERVICE_UUID = UUID.fromString("0000ff**-0000-1000-8000-00805f9b34fb");
- private final UUID RX_CHAR_UUID = UUID.fromString("0000ff**-0000-1000-8000-00805f9b34fb");
- private final UUID TX_CHAR_UUID = UUID.fromString("0000ff**-0000-1000-8000-00805f9b34fb");
- @Override
- public UUID getServiceUUID() {
- return SERVICE_UUID;
- }
- @Override
- public UUID getRxUUID() {
- return RX_CHAR_UUID;
- }
- @Override
- public UUID getTxUUID() {
- return TX_CHAR_UUID;
- }
- public String dualColor = "";
- public String hardwareVersion = "";
- public int powerState = 0;
- public String nickname = "";
- public LedDevice(BluetoothDevice device, DataParserAdapter adapter) {
- super(device, adapter);
- }
- }
- public class BLEManager extends BLEBaseManager {
- private final String TAG = "BLEManager";
- private static BLEManager instance = new BLEManager();
- public static BLEManager getInstance() {
- return instance;
- }
- @Override
- public HashMap<Integer, UUID> onGetDevicesServiceUUID() {
- HashMap<Integer, UUID> map = new HashMap();
- map.put(HeaterDevice.DEVICE_TYPE_ID, HeaterDevice.SERVICE_UUID);
- map.put(LedDevice.DEVICE_TYPE_ID, LedDevice.SERVICE_UUID);
- return map;
- }
- @Override
- public void onScanOver() {
- Log.w(TAG, "onScanOve..");
- }
- @Override
- public BLEAppDevice onCreateDevice(BluetoothDevice bluetoothDevice, int deviceType) {
- if (deviceType == HeaterDevice.DEVICE_TYPE_ID) {
- // 数据包解析适配器为 null, 蓝牙设备回应的数据在 onDevicesRespOriginalData(BLEPacket message)
- return new HeaterDevice(bluetoothDevice, null);
- }
- else if (deviceType == LedDevice.DEVICE_TYPE_ID) {
- // 设置了数据包解析适配器, 数据回调在 onDeviceRespSpliceData(BLEPacket message)
- return new LedDevice(bluetoothDevice, new LedDataAdapter());
- }
- else {
- return null;
- }
- }
- @Override
- public void onAddScanDevice(BluetoothDevice bluetoothDevice){
- EventBus.getDefault().post(new AddScanDeviceEvent(bluetoothDevice));
- }
- @Override
- public void onConnectUnTypeDevice(BluetoothDevice bluetoothDevice, int type) {
- EventBus.getDefault().post(new ConnectUnTypeDeviceEvent(bluetoothDevice, type));
- }
- @Override
- public void onConnectDevice(BLEAppDevice device, int type){
- EventBus.getDefault().post(new ConnectDeviceEvent(device, type));
- }
- @Override
- public void onAddNewDevice(BLEAppDevice device){
- EventBus.getDefault().post(new AddNewDeviceEvent(device));
- }
- @Override
- public void onUpdateDeviceInfo(BLEAppDevice device) {
- EventBus.getDefault().post(new updateDeviceInfoEvent(device));
- }
- @Override
- public void onDeviceSendResult(String result){
- EventBus.getDefault().post(new BleSendResultEvent(result));
- }
- @Override
- public void onDeviceRespSpliceData(BLEPacket message) {
- LogUtil.i(TAG, "onDeviceRespSpliceDat: [" + BytesUtil.BytesToHexStringPrintf(message.bleData) + "] bleId:" + message.bleId);
- // DataManager.getInstance().DecodeRespData(message.bleData, message.bleId);
- }
- @Override
- public void onDevicesRespOriginalData(BLEPacket message) {
- LogUtil.v(TAG, "onDevicesRespOriginalDat: [" + BytesUtil.BytesToHexStringPrintf(message.bleData) + "] bleId:" + message.bleId);
- }
- }
来源: https://www.cnblogs.com/littlecarry/p/11889982.html