1***@qq.com
1***@qq.com
  • 发布:2020-06-10 16:57
  • 更新:2023-02-03 14:14
  • 阅读:2427

uniapp + native.js连接蓝牙设备

分类:Native.js

uniapp + native.js连接蓝牙设备,是从已配对列表获取的蓝牙地址,设备根本没开机,点击连接竟然成功了
let InputStream = plus.android.importClass("java.io.InputStream");
let OutputStream = plus.android.importClass("java.io.OutputStream");
let BluetoothSocket = plus.android.importClass("android.bluetooth.BluetoothSocket");
var Context = plus.android.importClass("android.content.Context");
var BManager = this.mainActivity.getSystemService(Context.BLUETOOTH_SERVICE);
plus.android.importClass(BManager);
var BAdapter = BManager.getAdapter();
var UUID = plus.android.importClass("java.util.UUID");
let MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); //不能改
try {
let device = plus.android.invoke(BAdapter, "getRemoteDevice", address);//这个地址是没开机的蓝牙地址
//this.btSocket = plus.android.invoke(device, "createRfcommSocketToServiceRecord", MY_UUID);
this.btSocket = device.createInsecureRfcommSocketToServiceRecord(MY_UUID);
plus.android.importClass(this.btSocket);
} catch (e) {
return false;
}
try {
//plus.android.invoke(this.btSocket, "connect");
this.btSocket.connect()
//readData(); //读数据
this.readData()
} catch (e) {
this.info = "连接失败" + e;
try {
this.btSocket.close();
this.btSocket = null;
} catch (e1) {

                }  
                return false;  
            }  
            return true;
2020-06-10 16:57 负责人:无 分享
已邀请:
DKUN

DKUN

解决了吗

1***@qq.com

1***@qq.com

解决了吗

  • 1***@qq.com

    btSocket.isConnected() 这个方法可以判断是否连接成功

    2023-02-03 14:34

要回复问题请先登录注册