将BLE服务和特征从Fragment传递到Activity以使用的最佳解决方案是什么?

时间:2017-09-21 15:52:32

标签: java android bluetooth gatt

连接到我的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 个答案:

答案 0 :(得分:0)

你可以制作一个Singleton模式BLEManager,它将自己设置为所有内容的监听器,并调用当前订阅的监听器,因此创建一个接口:

 <label for="Record">Record</label>
                    <select name="record">
                        {%for record in records %}
                            <option value="{{record.recordid}}">{{ 
  record.startDate }} -- {{ record.endDate }}
  </option>
                        {%endfor %}
                    </select>

然后简单地将自己连接到你的单身人士进行回调 MyBLEManager.getInstance(this,this)// context,listener

然后让经理回复您的回复。然后在处理BLE连接的服务类中,只需执行以下操作:

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);


}

另外,您可以注册一个广播接收器并以同样的方式将其传送出去。