【问题标题】:When is physically a BLE connection is establishing? by calling establish connection() ? or calling the first characteristic after it?何时建立 BLE 连接?通过调用建立连接()?还是在它之后调用第一个特征?
【发布时间】:2018-10-08 12:56:47
【问题描述】:

我实际上对发布在与“establishconnection()”和相关讨论相关的其他问题的答案感到困惑。自从我开始与我的应用程序建立低能耗 BLE 设备连接以来,我对应用程序和设备之间的物理通信建立有疑问。在我的程序中,我在读取特征之前调用了establishconnection()。但是在之后的同步过程中,我总是在应用程序和设备之间遇到一些连接问题。所以我需要用我的设备再次深入检查连接过程。那么您能否从上述各点中给出一个确切的代码点实际上是从应用程序建立到 BLE 设备的物理连接

【问题讨论】:

  • 您是否尝试过自己检查行为?也许使用日志?
  • @DariuszSeweryn 是的,我使用 Logs 进行了尝试,但没有从中获取实际的连接点信息。每当我有时阅读这些特征时,它都会先抛出 GATT 错误。所以我无法找到有关物理连接起点的确切信息。
  • 你试过分离连接和读取特性吗?您也可以考虑展示您使用的代码,以便我们考虑示例

标签: android bluetooth bluetooth-lowenergy rx-android rxandroidble


【解决方案1】:

有几种方法可以找出连接发生的确切时间。这可以在几个层面上完成:

Android 默认日志

通常在连接到 BLE 外围设备时,logcat 中会显示特定的行:

D/BluetoothGatt:onClientConnectionState() - status=0 clientIf=6 device=00:00:00:00:00:00

RxAndroidBle日志

可以设置RxBleLog.setLogLevel(RxBleLog.VERBOSE),这将使图书馆内部日志可见。系统通知库建立连接的那一刻用日志标记:

D/RxBle#BluetoothGatt: onConnectionStateChange newState=2 status=0

低级 ACL 广播

大多数 Android 操作系统会在建立低级别蓝牙连接时广播信息。要访问它,必须注册 BroadcastReceiver

Context context = /* your Context */;
IntentFilter intentFilter = new IntentFilter(BluetoothDevice.ACTION_ACL_CONNECTED);
BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        final BluetoothDevice bluetoothDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
        Log.d("Connected", "BluetoothDevice[macAddress=" + bluetoothDevice.getAddress() + ']');
    }
};
context.registerReceiver(broadcastReceiver, intentFilter);

这将触发以下日志:

D/已连接:BluetoothDevice[macAddress=00:00:00:00:00:00]

【讨论】:

    猜你喜欢
    • 2016-02-02
    • 2017-02-02
    • 2020-06-24
    • 1970-01-01
    • 2021-02-28
    • 2017-06-24
    • 2016-09-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多