17379620@qq.com
17379620@qq.com
  • 发布:2021-02-01 14:01
  • 更新:2021-02-01 14:01
  • 阅读:50

连接蓝牙(已有安卓源码转Native.js)设备不支持低功耗

分类:Native.js

device.connectGatt 一直报错
Unexpected identifier
main参数是main = plus.android.runtimeMainActivity()

function connect(address) {  
                console.log('准备连接:' + address);  
                if (mBluetoothAdapter == null || address == null) {  
                    console.log("BluetoothAdapter 未初始化或未指定的地址");  
                    return false;  
                }  

                // Previously connected device.  Try to reconnect.  
                if (mBluetoothDeviceAddress != null && address.equals(mBluetoothDeviceAddress) &&  
                    mBluetoothGatt != null) {  
                    console.log("正在尝试使用现有的蓝牙设备进行连接");  
                    if (mBluetoothGatt.connect()) {  
                        mConnectionState = STATE_CONNECTING;  
                        return true;  
                    } else {  
                        return false;  
                    }  
                }  

                var device = mBluetoothAdapter.getRemoteDevice(address);  
                plus.android.importClass(device);  
                // if (device == null) {  
                //  console.log("找不到设备。无法连接。");  
                //  return false;  
                // }  

                var mGattCallback = plus.android.implements('io.dcloud.android.bluetooth.BluetoothGattCallback', {  
                    onConnectionStateChange: function(gatt, status, newState) {  
                        plus.android.importClass(gatt);  
                    },  
                    onServicesDiscovered: function(gatt, status) {  
                        plus.android.importClass(gatt);  
                    },  

                    onCharacteristicRead: function(gatt, characteristic, status) {  
                        plus.android.importClass(gatt);  
                    },  
                    onCharacteristicChanged: function(gatt, characteristic) {  
                        plus.android.importClass(gatt);  
                    },  
                    onCharacteristicWrite: function(gatt, characteristic, status) {  
                        plus.android.importClass(gatt);  
                    }  
                });  
                var nwv = plus.android.currentWebview();  
                console.log(device.connectGatt(main, false, mGattCallback));  
                // plus.android.importClass(nwv);  

                // 我们想直接连接到设备,所以我们正在设置自动连接  
                // 参数设置为 false.  
                mBluetoothGatt = device.connectGatt(main, false, mGattCallback);  
                plus.android.importClass(mBluetoothGatt);  
                console.log("正在尝试创建新连接");  
                mBluetoothDeviceAddress = address;  
                mConnectionState = STATE_CONNECTING;  
                return true;  
            }

附件为蓝牙继电器厂商提供Google代码,测试可连接

2021-02-01 14:01 负责人:无 分享
已邀请:

要回复问题请先登录注册