uni-app 自定义低功耗蓝牙API原生插件 - 老醒 无返回值

发布于 1周前 作者 songsunli 来自 Uni-App

uni-app 自定义低功耗蓝牙API原生插件 - 老醒 无返回值

readBLECharacteristicValue 没有返回值

1 回复

uni-app 中,如果你想自定义一个低功耗蓝牙(BLE)的原生插件,并且该插件在某些情况下没有返回值,你可以按照以下步骤进行开发。这里假设你已经熟悉 uni-app 的基本开发流程,并且了解如何创建原生插件。

1. 创建原生插件项目

首先,你需要在 uni-app 项目中创建一个原生插件。你可以使用 HBuilderX 创建一个新的原生插件项目,或者手动创建一个插件目录。

2. 编写原生代码

在原生插件中,你需要编写与低功耗蓝牙相关的代码。以下是一个简单的示例,展示如何在 Android 平台上实现一个没有返回值的 BLE 操作。

Android 示例

  1. 创建 Java 类:在 src/main/java/com/example/bleplugin 目录下创建一个新的 Java 类,例如 BLEPlugin.java
package com.example.bleplugin;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCallback;
import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothProfile;
import android.content.Context;
import android.util.Log;

import io.dcloud.feature.uniapp.annotation.UniJSMethod;
import io.dcloud.feature.uniapp.bridge.UniJSCallback;
import io.dcloud.feature.uniapp.common.UniModule;

public class BLEPlugin extends UniModule {

    private static final String TAG = "BLEPlugin";
    private BluetoothAdapter bluetoothAdapter;
    private BluetoothGatt bluetoothGatt;

    @UniJSMethod(uiThread = false)
    public void connectToDevice(String deviceAddress) {
        Context context = mUniSDKInstance.getContext();
        bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        BluetoothDevice device = bluetoothAdapter.getRemoteDevice(deviceAddress);

        if (device == null) {
            Log.e(TAG, "Device not found");
            return;
        }

        bluetoothGatt = device.connectGatt(context, false, new BluetoothGattCallback() {
            @Override
            public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
                if (newState == BluetoothProfile.STATE_CONNECTED) {
                    Log.i(TAG, "Connected to GATT server.");
                    gatt.discoverServices();
                } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
                    Log.i(TAG, "Disconnected from GATT server.");
                }
            }

            @Override
            public void onServicesDiscovered(BluetoothGatt gatt, int status) {
                if (status == BluetoothGatt.GATT_SUCCESS) {
                    Log.i(TAG, "Services discovered.");
                } else {
                    Log.w(TAG, "onServicesDiscovered received: " + status);
                }
            }

            @Override
            public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
                if (status == BluetoothGatt.GATT_SUCCESS) {
                    Log.i(TAG, "Characteristic read: " + characteristic.getStringValue(0));
                }
            }
        });
    }

    @UniJSMethod(uiThread = false)
    public void disconnectFromDevice() {
        if (bluetoothGatt != null) {
            bluetoothGatt.disconnect();
            bluetoothGatt.close();
            bluetoothGatt = null;
        }
    }
}
  1. 注册插件:在 src/main/assets/dcloud_uniplugins.json 文件中注册你的插件。
{
  "plugins": [
    {
      "type": "module",
      "name": "BLEPlugin",
      "class": "com.example.bleplugin.BLEPlugin"
    }
  ]
}

3. 在 uni-app 中调用插件

uni-app 项目中,你可以通过 uni.requireNativePlugin 方法来调用你自定义的原生插件。

const blePlugin = uni.requireNativePlugin('BLEPlugin');

// 连接设备
blePlugin.connectToDevice('00:11:22:33:44:55');

// 断开连接
blePlugin.disconnectFromDevice();

4. 处理无返回值的情况

在上述示例中,connectToDevicedisconnectFromDevice 方法都没有返回值。如果你需要在某些情况下返回结果,可以在方法中添加 UniJSCallback 参数,并通过回调返回结果。

例如:

@UniJSMethod(uiThread = false)
public void connectToDevice(String deviceAddress, UniJSCallback callback) {
    // ... 连接设备的代码

    if (connected) {
        callback.invoke("Connected successfully");
    } else {
        callback.invoke("Connection failed");
    }
}

uni-app 中调用时:

blePlugin.connectToDevice('00:11:22:33:44:55', (result) => {
    console.log(result);
});
回到顶部
AI 助手
你好,我是IT营的 AI 助手
您可以尝试点击下方的快捷入口开启体验!