uni-app 继承BluetoothGattCallback基类无法重写内置方法,报 overrides nothing
uni-app 继承BluetoothGattCallback基类无法重写内置方法,报 overrides nothing
示例代码:
export class LkmBluetoothGattCallback extends BluetoothGattCallback{
constructor(){
super()
}
public override onCharacteristicChanged(param0?: BluetoothGatt, param1?: BluetoothGattCharacteristic): void{
console.log(param0,param1,"看看有啥阿凡达")
};
public override onCharacteristicRead(param0?: BluetoothGatt, param1?: BluetoothGattCharacteristic, param2?: number): void{
console.log("onCharacteristicRead",param0,param1,param2)
};
public override onCharacteristicWrite(param0?: BluetoothGatt, param1?: BluetoothGattCharacteristic, param2?: number): void{
console.log(param0,param1,param2)
};
public override onConnectionStateChange(param0?: BluetoothGatt, param1?: number, param2?: number): void{
console.log(param0,param1)
};
override onDescriptorRead(param0: BluetoothGatt, param1: BluetoothGattDescriptor, param2: number): void{
console.log(param0,param1,param2)
};
public override onDescriptorWrite(param0: BluetoothGatt, param1: BluetoothGattDescriptor, param2: number): void{
console.log(param0,param1)
};
public override onMtuChanged(param0: BluetoothGatt, param1: number, param2: number): void{
console.log(param0,param1)
};
public override onPhyRead(param0: BluetoothGatt, param1: number, param2: number, param3: number): void{
console.log(param0,param1)
};
public override onPhyUpdate(param0: BluetoothGatt, param1: number, param2: number, param3: number): void{
console.log(param0,param1)
};
public override onReadRemoteRssi(param0: BluetoothGatt, param1: number, param2: number): void{
console.log(param0,param1)
};
public override onReliableWriteCompleted(param0: BluetoothGatt, param1: number): void{
console.log(param0,param1)
};
public override onServiceChanged(param0: BluetoothGatt): void{
console.log(param0)
};
public onServicesDiscovered(param0?: BluetoothGatt, param1?: number): void{
console.log(param0,param1,"毒瘤跟")
};
}
操作步骤:
直接调用就会出现这个问题,基于这个BluetoothGattCallback基类
预期结果:
直接调用就会出现这个问题,基于这个BluetoothGattCallback基类
实际结果:
error: 'onCharacteristicRead' overrides nothing
上面的父类方法基本都会报不能重写
bug描述:
使用uni-appX 写安卓蓝牙的时候有自己的回调方法去继承原生的回调类,报不能重写
更多关于uni-app 继承BluetoothGattCallback基类无法重写内置方法,报 overrides nothing的实战教程也可以访问 https://www.itying.com/category-93-b0.html
更多关于uni-app 继承BluetoothGattCallback基类无法重写内置方法,报 overrides nothing的实战教程也可以访问 https://www.itying.com/category-93-b0.html
在uni-app中处理蓝牙功能时,确实可能会遇到一些与原生Android开发相关的问题,尤其是当你试图继承并重写BluetoothGattCallback
类的方法时。BluetoothGattCallback
是Android SDK中的一个抽象类,用于处理蓝牙GATT客户端回调。在uni-app中,我们通常通过插件或者条件编译来使用原生代码,尤其是涉及到Android或iOS原生API的时候。
如果你在uni-app中遇到“overrides nothing”的错误,这通常意味着你的重写方法签名与父类中的方法签名不匹配。这里我将提供一个简化的示例,展示如何在uni-app的Android原生插件中正确重写BluetoothGattCallback
的方法。
步骤 1: 创建Android原生插件
首先,你需要创建一个Android原生插件。在native-plugins
目录下创建一个新的插件项目。
步骤 2: 继承BluetoothGattCallback
在你的Android插件项目中,创建一个类继承自BluetoothGattCallback
,并确保正确重写所有需要的方法。
package com.example.uniappbluetooth;
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCallback;
import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattDescriptor;
import android.bluetooth.BluetoothProfile;
public class MyBluetoothGattCallback extends BluetoothGattCallback {
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
// Handle connection state change
}
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
// Handle services discovered
}
@Override
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
// Handle characteristic change
}
// Implement other callback methods as needed
}
步骤 3: 在uni-app中调用插件
确保你的uni-app项目已经配置好调用这个原生插件。在JavaScript代码中,你可以通过插件接口与原生代码交互。
注意事项
- 确保所有回调方法的签名与
BluetoothGattCallback
中的定义完全一致。 - 如果你使用的是uni-app的条件编译,确保在编译到Android平台时包含相应的原生代码。
- 调试时,使用Android Studio来查看日志输出,这有助于诊断问题。
通过上述步骤,你应该能够在uni-app中成功重写BluetoothGattCallback
的方法。如果问题仍然存在,检查方法签名的准确性,以及确保你的插件正确集成到了uni-app项目中。