将 BLE 服务和特征从 Fragment 传递到 Activity 以使用的最佳解决方案是啥?
Posted
技术标签:
【中文标题】将 BLE 服务和特征从 Fragment 传递到 Activity 以使用的最佳解决方案是啥?【英文标题】:What is the best solution to pass BLE Services and Characteristics from Fragment to Activity to work with?将 BLE 服务和特征从 Fragment 传递到 Activity 以使用的最佳解决方案是什么? 【发布时间】:2017-09-21 15:52:31 【问题描述】:在我连接到我的 BLE 设备后,我一起获得了服务和特性。使用此代码:
if(gattServices == null)return;
//loops through available GATT SERVICES
for(BluetoothGattService gattService : gattServices)
uuid = gattService.getUuid().toString();
System.out.println("Service discovered: " + uuid);
new ArrayList<HashMap<String, String>>();
List<BluetoothGattCharacteristic> gattCharacteristics = gattService.getCharacteristics();
//loops through available characteristics
for (BluetoothGattCharacteristic gattCharacteristic : gattCharacteristics)
final String charUuid = gattCharacteristic.getUuid().toString();
System.out.println("Characteristic discovered: " + charUuid);
现在我想在我的应用程序的另一个活动中显示这些服务和特征,但问题是我不知道这样做的最佳方式是什么。有人可以给我建议吗?
【问题讨论】:
【参考方案1】:您可以创建一个单例模式 BLEManager,将自己设置为所有内容的侦听器并调用当前订阅的侦听器,因此请创建一个接口:
public interface IBLEComListener
/*/////////////////////////////////////////////////////////////////////////////////////////
// PUBLIC METHODS.
*//////////////////////////////////////////////////////////////////////////////////////////
/**
* To notify BLE connected.
*
* @param bluetoothDevice the instance of connected BluetoothDevice.
*/
void onBLEDeviceConnected(BluetoothDevice bluetoothDevice);
/**
* To notify BLE disconnected.
*
* @param bluetoothDevice the instance of connected BluetoothDevice.
*/
void onBLEDeviceDisconnected(BluetoothDevice bluetoothDevice);
/**
* To notify when unable to initialize Bluetooth.
*/
void onBLEUnableToInitializeBluetooth();
/**
* To notify Services ready for Connected BLE Device.
*
* @param bluetoothDevice the instance of connected BluetoothDevice.
*/
void onBLEDeviceServicesReady(BluetoothDevice bluetoothDevice);
/**
* To notify when service not found into BLE device.
*
* @param bluetoothDevice the instance of connected BluetoothDevice.
*/
void onServiceNotFound(BluetoothDevice bluetoothDevice);
/**
* To notify when characteristic notification.
*
* @param bluetoothDevice the instance of connected BluetoothDevice.
* @param bleMessageModelList the instance list of BLEMessageModel.
*/
void onBLECharacteristicNotificationReceived(BluetoothDevice bluetoothDevice, List<BLEMessageModel> bleMessageModelList);
/**
* To notify when message arrived.
*
* @param bluetoothDevice the instance of connected BluetoothDevice.
* @param characteristicDescriptorIdentifier the ENUM to identify the Characteristic or Descriptor.
* @param bleMessageModelList the instance list of BLEMessageModel.
*/
void onBLEMessageReceived(BluetoothDevice bluetoothDevice, CharacteristicDescriptorIdentifier characteristicDescriptorIdentifier, List<BLEMessageModel> bleMessageModelList);
/**
* To notify when message Sent.
*
* @param bluetoothDevice the instance of connected BluetoothDevice.
* @param characteristicDescriptorIdentifier the ENUM to identify the Characteristic or Descriptor.
*/
void onBLEMessageSent(BluetoothDevice bluetoothDevice, CharacteristicDescriptorIdentifier characteristicDescriptorIdentifier);
/**
* To notify when bluetooth off/disabled.
*/
void onBluetoothDisabled();
/**
* To notify when bluetooth on/enabled.
*/
void onBluetoothEnabled();
/**
* To notify BLE devices discovered/updated.
*
* @param deviceModel the BLE device.
*/
void onBLEDeviceDiscovered(DeviceModel deviceModel);
然后简单地将自己连接到您的单例以进行回调,例如 MyBLEManager.getInstance(this, this) //上下文,监听器
然后让经理回击您的回复。然后在处理 BLE 连接的服务类中执行以下操作:
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status)
if (status == BluetoothGatt.GATT_SUCCESS)
mServicesReadyBluetoothDeviceMap.clear();
for(BluetoothDevice bluetoothDevice : gatt.getConnectedDevices())
mServicesReadyBluetoothDeviceMap.put(bluetoothDevice.getAddress(), bluetoothDevice);
IBLEComListener comListener = A35BLEManager.getBLEComListener();
if(comListener != null)
comListener.onBLEDeviceServicesReady(gatt.getDevice());
else
A35Log.w(TAG, "onServicesDiscovered received: " + status);
或者,您可以注册一个广播接收器并以这种方式轻松分发。
【讨论】:
以上是关于将 BLE 服务和特征从 Fragment 传递到 Activity 以使用的最佳解决方案是啥?的主要内容,如果未能解决你的问题,请参考以下文章
如何在 android 中将特征写入 BLE GATT 服务器?
如何使用 TabLayout 将数据从 Activity 传递到 Fragment
如何将变量从 Activity 传递到 Fragment,并将其传回?