mui.plusReady(function() {
switch (plus.os.name) {
case "Android":
main = plus.android.runtimeMainActivity();
BluetoothAdapter = plus.android.importClass("android.bluetooth.BluetoothAdapter"); //本机蓝牙适配器
BluetoothDevice = plus.android.importClass("android.bluetooth.BluetoothDevice");
BluetoothSocket = plus.android.importClass("android.bluetooth.BluetoothSocket");
BluetoothReceiver = plus.android.importClass("android.bluetooth.BluetoothReceiver");
Intent = plus.android.importClass("android.content.Intent");
IntentFilter = plus.android.importClass("android.content.IntentFilter");
BAdapter = BluetoothAdapter.getDefaultAdapter(); //获得本机蓝牙适配器
/*检查是否支持蓝牙*/
if (BAdapter == null) {
mui.toast("本地蓝牙设备驱动异常");
return false;
} else {
/*检查蓝牙打开状态*/
if (!BAdapter.isEnabled()) {
var intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); //打开蓝牙
BAdapter.enable();
console.log(BAdapter.getAddress());
console.log(BAdapter.getName());
}
/**
* 检查蓝牙扫描模式
* BAdapter.getScanMode() 获取蓝牙扫描模式
* BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE 值为23, 查询扫描和页面扫描都有效
*/
if (BAdapter.getScanMode() != BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) {
var discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE); //打开蓝牙搜索
discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300); //设置扫描时间(最多300秒)
main.startActivity(discoverableIntent);
}
/*获取已配对设备*/
var devices = BAdapter.getBondedDevices();
plus.android.importClass(devices);
console.log(devices);
var devices_len = devices.size();
for (var i = 0; i < devices_len; i++) {
console.log(devices.get(i));
}
}
break;
case "iOS":
break;
default:
break;
}
});
devices.get(i)报错,里面没有get方法。对JAVA不熟悉,官方的麻烦帮助看下。
另一个问题是Native.js是否能开发蓝牙打印功能?