f***@163.com
f***@163.com
  • 发布:2025-07-26 06:48
  • 更新:2025-07-26 06:48
  • 阅读:287

plus蓝牙指令根本没反应,请高山指教一下!问题出在哪?

分类:HTML5+

<script src="http://www.html5plus.org/plus.js" type="text/javascript"></script>
<script src="http://www.html5plus.org/plus.bluetooth.js" type="text/javascript"></script>
<script>
// 全局变量存储扫描状态
if(window.plus) {
initBluetoothFunctions();
} else {
document.addEventListener('plusready', initBluetoothFunctions, false);
}

function initBluetoothFunctions() {
// 全局变量存储扫描状态
let isScanning = false;
let scanTimer = null;

// 初始化蓝牙适配器  
function initBluetooth() {  
    plus.bluetooth.getSystemBluetoothState({  
        success: function(res) {  
            if(res.available) {  
                openBluetoothAdapter();  
            } else {  
                console.log("系统蓝牙未开启");  
                alert("请先开启系统蓝牙");  
            }  
        },  
        fail: function(error) {  
            console.error("获取系统蓝牙状态失败:", error);  
        }  
    });  
}  
// 其余原有函数保持不变...  
// [原有全部函数代码]  

}

function openBluetoothAdapter() {
plus.bluetooth.openBluetoothAdapter({
success: function() {
console.log("蓝牙适配器初始化成功");
checkAdapterState();
},
fail: function(error) {
console.error("蓝牙适配器初始化失败:", error);
if(error.code === 10000) {
alert("当前设备不支持蓝牙功能");
}
}
});
}

// 检查适配器状态
function checkAdapterState() {
plus.bluetooth.getBluetoothAdapterState({
success: function(state) {
console.log("蓝牙适配器状态:", state);
if(state.discovering) {
plus.bluetooth.stopBluetoothDevicesDiscovery();
}
},
fail: function(error) {
console.error("获取适配器状态失败:", error);
}
});
}

// 开始扫描设备
function startDiscovery() {
if(isScanning) return;

isScanning = true;  
plus.bluetooth.startBluetoothDevicesDiscovery({  
    success: function() {  
        console.log("开始扫描蓝牙设备");  
        // 设置10秒自动停止  
        scanTimer = setTimeout(stopDiscovery, 10000);  
        // 监听新设备发现  
        plus.bluetooth.onBluetoothDeviceFound(handleDeviceFound);  
        getBluetoothDevices();  
    },  
    fail: function(error) {  
        console.error("扫描启动失败:", error);  
        isScanning = false;  
    }  
});  

}

// 停止扫描
function stopDiscovery() {
if(!isScanning) return;

clearTimeout(scanTimer);  
plus.bluetooth.stopBluetoothDevicesDiscovery({  
    success: function() {  
        console.log("扫描已停止");  
        isScanning = false;  
    },  
    fail: function(error) {  
        console.error("停止扫描失败:", error);  
    }  
});  

}

// 处理发现的设备
function handleDeviceFound(res) {
console.log("发现新设备:", res.devices);
updateDeviceList(res.devices);
}

// 获取已发现设备
function getBluetoothDevices() {
plus.bluetooth.getBluetoothDevices({
success: function(res) {
console.log("已发现设备数量:", res.devices.length);
updateDeviceList(res.devices);
},
fail: function(error) {
console.error("获取设备列表失败:", error);
}
});
}

// 更新设备列表
function updateDeviceList(devices) {
const tableBody = document.getElementById('deviceList');
if(!tableBody) {
console.error("设备列表容器不存在");
return;
}

tableBody.innerHTML = '';  

if(!devices || devices.length === 0) {  
    const row = document.createElement('tr');  
    const cell = document.createElement('td');  
    cell.colSpan = 3;  
    cell.textContent = '未发现蓝牙设备';  
    row.appendChild(cell);  
    tableBody.appendChild(row);  
    return;  
}  

devices.forEach(device => {  
    const row = document.createElement('tr');  
    row.onclick = () => connectDevice(device.deviceId);  

    const nameCell = document.createElement('td');  
    nameCell.textContent = device.name || '未知设备';  
    row.appendChild(nameCell);  

    const idCell = document.createElement('td');  
    idCell.textContent = device.deviceId || 'N/A';  
    row.appendChild(idCell);  

    const rssiCell = document.createElement('td');  
    rssiCell.textContent = device.RSSI || 'N/A';  
    row.appendChild(rssiCell);  

    tableBody.appendChild(row);  
});  

}

// 连接设备
function connectDevice(deviceId) {
plus.bluetooth.createBLEConnection({
deviceId: deviceId,
success: function() {
console.log("连接成功:", deviceId);
},
fail: function(error) {
console.error("连接失败:", error);
}
});
}

function jia()
{ var score
score = document.getElementById("score");
score=score+1;
document.getElementById("score").value=score;
}

function jian()
{ var score
score = document.getElementById("score");
score=score-1;
document.getElementById("score").value=score;
}

</script>

2025-07-26 06:48 负责人:无 分享
已邀请:

要回复问题请先登录注册