标签: android-ble

在没有BluetoothGATT的情况下断开BluetoothDevice的连接

我的应用程序与BLE外设通信。有时,应用程序是在已连接外围设备的情况下启动的。我可以通过调用以下方式检索设备:

BluetoothManager manager = (BluetoothManager) getSystemService(BLUETOOTH_SERVICE);
List<BluetoothDevice> connectedDevices = manager.getConnectedDevices(BluetoothProfile.GATT);
Run Code Online (Sandbox Code Playgroud)

然后,我可以根据地址或UUID过滤connectedDevices。但是,BluetoothDevice没有断开连接的方法。要断开连接,我需要一个BluetoothGATT实例。但是我看到的获取BluetoothGATT实例的唯一方法是调用

connectedDevice.connectGatt(Context, boolean, BluetoothGattCallback)
Run Code Online (Sandbox Code Playgroud)

这需要很长时间。最重要的是,我在调用connectGatt之后返回的BluetoothGatt实例似乎在我断开连接时实际上并未断开外围设备的连接。

所以我的问题是:

  • 有没有一种方法可以在不调用connectGatt的情况下断开连接的BluetoothDevice?
  • 为什么connectGatt对于已连接的设备要花费这么长时间?
  • 在连接的BluetoothDevice上调用connectGatt甚至有效吗?

谢谢

android bluetooth-lowenergy android-ble

6
推荐指数
1
解决办法
3800
查看次数

我想从我们开发的移动应用程序的 11 字节数据中识别 BLE 车轮和曲柄传感器数据

我想从11 字节数据中识别车轮和曲柄传感器数据。我试图按照下面链接中的拆分来解析我在我们的移动应用程序中获得的 11 字节十六进制数据。

https://www.bluetooth.com/wp-content/uploads/Sitecore-Media-Library/Gatt/Xml/Characteristics/org.bluetooth.characteristic.csc_measurement.xml

例如,我尝试了以下方法,

十六进制数据:0x03 6D010000 FC7E 2C01 F87E

Flag-03 ->0000 0011 -> 8bits 所以两者都是真的,因此我们可以得到车轮和曲柄各自的值。

累积车轮转数- 6D 01 00 00 -> 32 位,因此将其转换为十进制我们得到 -1828782080

最后一个轮子事件时间 - FC 7E -> 16 位,因此我们将其转换为十进制 - 64638

累积曲柄转数 - 2C 01 -> 16 位,因此将其转换为十进制我们得到 - 11265

上次曲柄事件时间 - F8 7E -> 16 位,因此我们将其转换为十进制 - 63614

我无法从 BLE 获得实际的车轮和曲柄测量值。我从我遵循的参考链接中了解到的上述程序是否正确?还是我在其他地方错了?我已尽最大努力剖析和解析数据,但不幸的是我无法找到解决方案。请指导我完成这个过程。我们必须做什么才能获得正确的价值?就像我应该用某个数字乘以它吗?我尝试过不同的组合但无法获得。我使用的设备是SunDing515 自行车速度和踏频传感器,带有蓝牙低功耗

android bluetooth-lowenergy gatt android-ble

6
推荐指数
1
解决办法
1090
查看次数

Android BLE readCharacteristic失败

当我连接它时,我正试图读取BLE设备的初始状态.这是我必须尝试做的代码:

@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status)
{
    if(status == BluetoothGatt.GATT_SUCCESS)
    {
        Log.i(TAG, gatt.getDevice().toString() + "Discovered Service Status: " + gattStatusToString(status));
        for(BluetoothGattService service : gatt.getServices())
        {
            Log.i(TAG, "Discovered Service: " + service.getUuid().toString() + " with " + "characteristics:");
            for(BluetoothGattCharacteristic characteristic : service.getCharacteristics())
            {
                // Set notifiable
                if(!gatt.setCharacteristicNotification(characteristic, true))
                {
                    Log.e(TAG, "Failed to set notification for: " + characteristic.toString());
                }

                // Enable notification descriptor
                BluetoothGattDescriptor descriptor = characteristic.getDescriptor(CCC_UUID);
                if(descriptor != null)
                {
                    descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
                    gatt.writeDescriptor(descriptor);
                }

                // Read characteristic
                if(!gatt.readCharacteristic(characteristic))
                { …
Run Code Online (Sandbox Code Playgroud)

android bluetooth-lowenergy android-ble

5
推荐指数
1
解决办法
6181
查看次数

bluetoothGatt writeCharacteristic 返回 false

我创建了一个具有 WRITE_TYPE_NO_RESPONSE 特性的外围设备。使用另一个应用程序,我写了特性,但我偶尔会遇到带有棒棒糖 5.1.1 的 samsung Galaxy nexus i9250 问题:有时函数 BluetoothGatt.writeCharacteristic 返回 false 并且写入未完成。

会是什么呢 ?会不会是 cyanogenmod 的错?我可以用什么方式修补这个问题?

在外围:

new BluetoothGattCharacteristic(
                UUID.fromString(characteristic),
                BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE ,
                BluetoothGattCharacteristic.PERMISSION_WRITE);
Run Code Online (Sandbox Code Playgroud)

编辑:这个问题只发生在 android >= 5.0.2

在kitkat和android 5.0.1上从来没有发生过,是偶然的吗?

android bluetooth-lowenergy android-ble

5
推荐指数
1
解决办法
3062
查看次数

如何确保应用程序与蓝牙 LE 设备断开连接?

我有两个正在连接的设备。当我离开应用程序时,我会断开与设备的连接。两者都经历相同的过程,但有时其中一个设备会保持连接,直到我强制关闭应用程序。

我的设备上有指示灯,确认它仍然认为它已连接,并且应用程序的其他实例无法连接到它,直到我强行关闭第一个实例。在下面的日志中,第一个列出的设备保持连接。

//call gatt.disconnect();
BluetoothGatt: cancelOpen() - device: F0:3D:A0:04:CA:E7
BluetoothGatt: onClientConnectionState() - status=0 clientIf=7 device=F0:3D:A0:04:CA:E7

//wait for connection state change callback then call gatt.close();
BluetoothGatt: close()
BluetoothGatt: unregisterApp() - mClientIf=7

//call gatt.disconnect();
BluetoothGatt: cancelOpen() - device: FF:A9:CA:EF:08:A4
BluetoothGatt: onClientConnectionState() - status=0 clientIf=5 device=FF:A9:CA:EF:08:A4

//wait for connection state change callback then call gatt.close();
BluetoothGatt: close()
BluetoothGatt: unregisterApp() - mClientIf=5
Run Code Online (Sandbox Code Playgroud)

调用 gatt.close() 后,我获取蓝牙管理器并在已连接设备列表中查找我的设备。它在列表中。

BluetoothGattCallback

public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
    super.onConnectionStateChange(gatt, status, newState);
    final String address = gatt.getDevice().getAddress(); …
Run Code Online (Sandbox Code Playgroud)

android bluetooth bluetooth-lowenergy android-ble

5
推荐指数
1
解决办法
5499
查看次数

Android Ble 断开连接时间延迟

我编写了一些代码来将我的 Android 手机连接到 BLE 设备。当我关闭 ble 设备电源时,我的手机需要几秒钟(2-20 秒)才能通知我它已失去与 ble 设备的连接。有什么办法可以立即通知我吗?我可以更改连接监控超时吗?

android android-intent android-activity bluetooth-lowenergy android-ble

5
推荐指数
1
解决办法
2094
查看次数

android - 在 API 级别 22 中不推荐使用 stopLeScan() 和 startLeScan() - 我该如何用 stopScan() 和 startScan() 替换它?

我看到在 Android 5.1.1 中不推荐使用 stopLeScan() 和 startLeScan()。我在替换 stopLeScan() 和 startLeScan() 方法时遇到问题。这是我的以下代码:

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    final BluetoothDevice device = mLeDeviceListAdapter.getDevice(position);
    if (device == null) return;
    final Intent intent = new Intent(this, MainActivity.class);
    //intent.putExtra(DeviceControlActivity.EXTRAS_DEVICE_NAME, device.getName());
    intent.putExtra(MainActivity.EXTRAS_DEVICE_ADDRESS, device.getAddress());
    if (mScanning) {
        mBluetoothAdapter.stopScan(mLeScanCallback);
        mBluetoothAdapter.getBluetoothLeScanner();
        mScanning = false;
    }
    startActivity(intent);
}

private void scanLeDevice(final boolean enable) {
    if (enable) {
        // Stops scanning after a pre-defined scan period.
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() { …
Run Code Online (Sandbox Code Playgroud)

android bluetooth bluetooth-lowenergy android-bluetooth android-ble

5
推荐指数
1
解决办法
8254
查看次数

Android 蓝牙 LE SCAN_FAILED_OUT_OF_HARDWARE_RESOURCES

在我的 Android(API 版本 21 及更高版本)蓝牙 LE 应用程序中,应用程序首先使用BluetoothLeScannerScanCallback对象扫描外围设备。

这在应用程序启动的前几次工作正常,但是在从 Android Studio 启动和停止应用程序几次后,回调函数onScanFailed()将被调用为errorCode5。此代码甚至没有被 ScanCallback.java 公开,但来源表明它被声明为public static final int SCAN_FAILED_OUT_OF_HARDWARE_RESOURCES = 5.

当出现此错误时,恢复应用程序正常行为的唯一方法是关闭蓝牙然后再次打开。

这个错误代码似乎完全没有记录,而且是罕见的。更有趣的是,在设备发现后,应用程序开始使用另一个侦听设备广播的回调进行扫描。即使在初始发现扫描收到错误代码 5 之后,此扫描会话也始终会可靠地启动。

两个不同 s 的设置ScanCallback在几个方面有所不同。

发现扫描仪的设置如下:

ScanSettings.Builder()
            .setScanMode(ScanSettings.SCAN_MODE_LOW_POWER)
            .also {
                if(Build.VERSION.SDK_INT >= 23) 
                    it.setCallbackType(ScanSettings.CALLBACK_TYPE_FIRST_MATCH)
            }
            .build()
Run Code Online (Sandbox Code Playgroud)

还有一个过滤器: ScanFilter.Builder().setServiceUuid(BluetoothUUID.service.parcelUuid).build()

同时,广播监听扫描仪的设置如下:

ScanSettings.Builder()
            .setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY)
            .build()
Run Code Online (Sandbox Code Playgroud)

广播扫描仪的过滤器为空。

如何避免 BLE 子系统处于这种状态,为什么会发生这种情况?

android bluetooth-lowenergy android-ble

5
推荐指数
1
解决办法
920
查看次数

缺少 BLUETOOTH_CONNECT 权限,但编译/目标 SDK 配置为 30

我正在实现一个支持一些 BLE 实用程序的 Android 库。在我的build.gradle中,编译和目标SDK都配置为30,但我在编译时遇到了缺少权限错误:

错误:缺少 BluetoothDevice.getName 所需的权限:android.permission.BLUETOOTH_CONNECT [MissingPermission]

我的理解是,BLUETOOTH_CONNECT 在 SDK 31 之后可用。知道为什么即使我将 SDK 强制设置为 30,我也会看到此错误吗?

android android-permissions android-ble

5
推荐指数
0
解决办法
552
查看次数

Android 蓝牙 (BLE) onCharacteristicChanged 上的数据损坏

我的应用程序执行以下操作:

  1. onDescriptorWrite它向 BT 设备发送命令。

  2. 一旦 BT 设备收到此命令,它就会开始向 Android 手机传输数据。

  3. AndroidonCharacteristicChanged正在捕获从 BT 设备发送的所有数据。

  4. 所有数据传输完毕后,Android 应用程序会将其写入文件。

我已经对其进行了测试,一切在三星(Android 11)、OnePlus(Android 11)和小米(Android 9)上运行得非常完美,但来自诺基亚(Android 11)的数据onCharacteristicChanged被损坏。

此测试示例显示将传输的数据写入带有校验和的文件中。正如你所看到的,我在小米上得到了完全相同的字节,但在诺基亚上它有时会被损坏。

Tested on Xiaomi MiA1 (Android 9)

File_MD5: The right file MD5 -> AE36F08213B25B5E0EE19425257D0D85

File_MD5: Measurement file MD5: AE36F08213B25B5E0EE19425257D0D85 (ok)
File_MD5: Measurement file MD5: AE36F08213B25B5E0EE19425257D0D85 (ok)
File_MD5: Measurement file MD5: AE36F08213B25B5E0EE19425257D0D85 (ok)
File_MD5: Measurement file MD5: AE36F08213B25B5E0EE19425257D0D85 (ok)
File_MD5: Measurement file MD5: AE36F08213B25B5E0EE19425257D0D85 (ok)
File_MD5: Measurement file MD5: AE36F08213B25B5E0EE19425257D0D85 (ok) …
Run Code Online (Sandbox Code Playgroud)

android android-bluetooth android-ble

5
推荐指数
1
解决办法
876
查看次数