Android BLE如何通过GATT发送消息?

Android BLE how to send message with GATT?

我正在尝试使用 Android BLE (Java) 与 JDY-18 蓝牙模块通信。目前,我可以连接到 JDY-18,但无法向它发送消息。我在使用时遇到问题:mBluetoothGatt.getService 这是我程序的一些片段:
首先,我得到 JDY-18 的 UUIDList:

public void startScanning() {
    AsyncTask.execute(new Runnable() {
        @Override
        public void run() {
            if (ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.BLUETOOTH_SCAN) != PackageManager.PERMISSION_GRANTED) {
                ArrayList<String> permissions = new ArrayList<>();
                permissions.add(Manifest.permission.BLUETOOTH_SCAN);
                requestPermissionLauncher.launch(permissions.toArray(new String[0]));
                return;
            }
            btScanner.startScan(leScanCallback);
        }
    });
}


// Device scan callback.


private ScanCallback leScanCallback = new ScanCallback() {
        @Override
        public void onScanResult(int callbackType, ScanResult result) {
            if (ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
                ArrayList<String> permissions = new ArrayList<>();
                permissions.add(Manifest.permission.BLUETOOTH_CONNECT);
                requestPermissionLauncher.launch(permissions.toArray(new String[0]));
                return;
            }
    

            if (result.getDevice().getName() != null) {

                if (result.getDevice().getName().equals("JDY-18")) {
                    targetDevAddr = result.getDevice().getAddress();
                    tvAppend(tv_read, "\r\nSet targetDevAddr to " + targetDevAddr);

//                    Add this to get UUIDList
                    serviceUUIDsList = getServiceUUIDsList(result);
                    stopScanning();
                }
            }
        }
    };

private List<UUID> getServiceUUIDsList(ScanResult scanResult)
    {
        List<ParcelUuid> parcelUuids = scanResult.getScanRecord().getServiceUuids();

        List<UUID> serviceList = new ArrayList<>();

        for (int i = 0; i < parcelUuids.size(); i++)
        {
            UUID serviceUUID = parcelUuids.get(i).getUuid();

            if (!serviceList.contains(serviceUUID))
                serviceList.add(serviceUUID);
        }

        return serviceList;
    }

我得到 2 个 UUID:

0000ffe1-0000-1000-8000-00805f9b34fb

0000fee7-0000-1000-8000-00805f9b34fb

然后,我得到服务:

BluetoothGattService service = mBluetoothGatt.getService(UUID.fromString("0000ffe1-0000-1000-8000-00805f9b34fb"));
if (service == null) {
        tvAppend(tv_read,"\r\nService not found!");
    }
service = mBluetoothGatt.getService(UUID.fromString("0000fee7-0000-1000-8000-00805f9b34fb"));
if (service == null) {
        tvAppend(tv_read,"\r\nService not found!");
    }

输出为:

Service not found!

Service not found!

我也在清单文件中设置了权限:

<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE" />
<uses-permission android:name="android.permission.BLUETOOTH_SCAN" android:usesPermissionFlags="neverForLocation" />

<uses-feature android:name="android.hardware.bluetooth" android:required="false"/>
<uses-feature android:name="android.hardware.bluetooth_le" android:required="false"/>

我是不是遗漏了什么?请帮我找出问题所在。

非常感谢!

在访问 BLE 设备之前,您需要执行以下几个步骤:

  1. 通过调用 bluetoothLeScanner.startScan(leScanCallback); 查找设备。找到的设备通过回调leScanCallback.
  2. 上报
  3. 使用 bluetoothService.connect(deviceAddress);
  4. 连接到设备
  5. 连接成功后调用bluetoothGatt.discoverServices();查找设备提供的所有可用服务。

在读取或写入特征之前,这些步骤始终是必需的。您目前只是在做第一步。更多信息和示例代码片段请参考官方Bluetooth Low Energy Guide