main = plus.android.runtimeMainActivity();
BluetoothAdapter = plus.android.importClass("android.bluetooth.BluetoothAdapter");
UUID = plus.android.importClass("java.util.UUID");
uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
BAdapter = BluetoothAdapter.getDefaultAdapter();
device = BAdapter.getRemoteDevice(mac_address);
plus.android.importClass(device);
bluetoothSocket = device.createInsecureRfcommSocketToServiceRecord(uuid);
plus.android.importClass(bluetoothSocket);
if(!bluetoothSocket.isConnected()) {
console.log('检测到设备未连接,尝试连接....');
bluetoothSocket.connect();
}
if(bluetoothSocket.isConnected()) {
console.log('设备已连接');
var outputStream = bluetoothSocket.getOutputStream();
plus.android.importClass(outputStream);
var string = "传输测试\r\n";
var bytes = plus.android.invoke(string, 'getBytes', 'gbk');
outputStream.write(bytes);
outputStream.flush();
device = null //这里关键
bluetoothSocket.close(); //必须关闭蓝牙连接否则意外断开的话打印错误
} else {
console.log('设备连接失败');
}
在执行bluetoothSocket.connect();的时候报错,但是我已经打开了对面的蓝牙, 也做好了接收文件的准备
Uncaught java.io.IOException: read failed, socket might closed or timeout, read ret: -1;at android.bluetooth.BluetoothSocket.connect
3 个回复
小猪熊
LZ解决了吗 我也遇到了
喜洋洋liugp
连接必须要保证uuid一致 如果连接的是手机设备的话可能的写一个服务端 去接受连接
8***@qq.com
最近研究蓝牙打印,也遇到这个问题,不知道有没有解决办法。哪位大神解决了,寻求帮助!