如何在 Nativescript 中实现 Android native abstract class 的覆盖方法?
How to implement override method for Android native abstract class in Nativescript?
作为 NativeScript 菜鸟,我一般都在玩 NativeScript,特别是蓝牙 Classic/Bluetooth LE,目前使用原生 Android 蓝牙 API。
我正在使用 Android 5.0.2 和 Cyanogenmod 13.1(即 Android 5.1)在支持 BT LE 的设备上进行测试。 NativeScript 版本是 1.5.2.
在我的 playground 应用程序中,我可以使用基本的蓝牙经典功能,但是当涉及到蓝牙 LE 扫描的回调时我被卡住了(使用 Android 5.0+ BTLE-API) .我的代码如下所示:
btAdapter = android.bluetooth.BluetoothAdapter.getDefaultAdapter();
if(null !== btAdapter && btAdapter.isEnabled()) {
btLeScanner = btAdapter.getBluetoothLeScanner();
btLeScanner.startScan(new android.bluetooth.le.ScanCallback({
onScanResult: function(callbackType, result) {
console.log("BT LE scan result");
}
}));
}
根据我现在从 NativeScript documentation and the specification of Androids abstract ScanCallback class 中了解到的情况,这应该是实现回调的正确方法。
摘自platforms/android/src/main/AndroidManifest。xml:
<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-feature android:name="android.hardware.bluetooth_le" android:required="true" />
执行代码时,出现以下错误:"Cannot convert JavaScript object with id xxxxxxxx at index 0",引用"new android.bluetooth.le.ScanCallback ..."
那么,简短的问题:我在这里错过了什么?
我认为问题在于您没有在扩展 classes 时扩展摘要 ScanCallback class. Instead, you are using the syntax for implementing an interface (see {N} docs on implementing an interface). To extend a class you should call extend() on the ScanCallback class (see the {N} documentation。
在您的情况下,您应该首先扩展 android.bluetooth.le.ScanCallback,然后创建派生的 class 的新实例,它将继承自 android.bluetooth.le.ScanCallback。
// Extend the android.bluetooth.le.ScanCallback class.
var MyScanCallback = android.bluetooth.le.ScanCallback.extend({
onScanResult: function(callbackType, result) {
console.log('BT LE scan result');
}
});
btAdapter = android.bluetooth.BluetoothAdapter.getDefaultAdapter();
if (null !== btAdapter && btAdapter.isEnabled()) {
btLeScanner = btAdapter.getBluetoothLeScanner();
// Now call btLeScanner.startScan and create an instance of our derived
// callback class named MyScanCallback.
btLeScanner.startScan(new MyScanCallback());
}
我还没有测试过代码,但在我的脑海中,这是你应该扩展原生的方式 android classes.
作为 NativeScript 菜鸟,我一般都在玩 NativeScript,特别是蓝牙 Classic/Bluetooth LE,目前使用原生 Android 蓝牙 API。 我正在使用 Android 5.0.2 和 Cyanogenmod 13.1(即 Android 5.1)在支持 BT LE 的设备上进行测试。 NativeScript 版本是 1.5.2.
在我的 playground 应用程序中,我可以使用基本的蓝牙经典功能,但是当涉及到蓝牙 LE 扫描的回调时我被卡住了(使用 Android 5.0+ BTLE-API) .我的代码如下所示:
btAdapter = android.bluetooth.BluetoothAdapter.getDefaultAdapter();
if(null !== btAdapter && btAdapter.isEnabled()) {
btLeScanner = btAdapter.getBluetoothLeScanner();
btLeScanner.startScan(new android.bluetooth.le.ScanCallback({
onScanResult: function(callbackType, result) {
console.log("BT LE scan result");
}
}));
}
根据我现在从 NativeScript documentation and the specification of Androids abstract ScanCallback class 中了解到的情况,这应该是实现回调的正确方法。
摘自platforms/android/src/main/AndroidManifest。xml:
<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-feature android:name="android.hardware.bluetooth_le" android:required="true" />
执行代码时,出现以下错误:"Cannot convert JavaScript object with id xxxxxxxx at index 0",引用"new android.bluetooth.le.ScanCallback ..."
那么,简短的问题:我在这里错过了什么?
我认为问题在于您没有在扩展 classes 时扩展摘要 ScanCallback class. Instead, you are using the syntax for implementing an interface (see {N} docs on implementing an interface). To extend a class you should call extend() on the ScanCallback class (see the {N} documentation。
在您的情况下,您应该首先扩展 android.bluetooth.le.ScanCallback,然后创建派生的 class 的新实例,它将继承自 android.bluetooth.le.ScanCallback。
// Extend the android.bluetooth.le.ScanCallback class.
var MyScanCallback = android.bluetooth.le.ScanCallback.extend({
onScanResult: function(callbackType, result) {
console.log('BT LE scan result');
}
});
btAdapter = android.bluetooth.BluetoothAdapter.getDefaultAdapter();
if (null !== btAdapter && btAdapter.isEnabled()) {
btLeScanner = btAdapter.getBluetoothLeScanner();
// Now call btLeScanner.startScan and create an instance of our derived
// callback class named MyScanCallback.
btLeScanner.startScan(new MyScanCallback());
}
我还没有测试过代码,但在我的脑海中,这是你应该扩展原生的方式 android classes.