天天看點

android 藍牙rssi的值,實時讀取已連接配接的藍牙裝置的RSSI

我在使用MUI開發BLE智能硬體app的時候,需要在裝置已經連接配接後周期性去讀取rssi值。我首先嘗試了調用getBluetoothDevices()方法,但是讀取的RSSI沒有變化,我推測這個RSSI是掃描發現裝置時記錄的,并沒有再更新。

plus.bluetooth.getBluetoothDevices({

success:function(e){

var devices = e.devices;

for(var i in devices){

if (devices[i].name == deviceName){

rssi_value = devices[i].RSSI;

console.log("rssi_value = " + rssi_value);

}

}

},

fail:function(e){

console.log('connected devices failed: '+JSON.stringify(e));

}

});

接下來我又嘗試使用getConnectedBluetoothDevices()方法,遺憾的是,它傳回的devices中隻包含name和deviceId兩個屬性(官方介紹裡面确實也是這樣說的),是以依然沒法讀到RSSI。

在此求助大家,希望大牛和官方不吝賜教如何解決這個問題,先行拜謝了。

PS: 我目前隻能斷開藍牙連接配接,在startBluetoothDevicesDiscovery辦法中定義allowDuplicatesKey為true,重新開始掃描藍牙裝置,但是不能連接配接裝置,在onBluetoothDeviceFound()方法中去讀取重新整理的RSSI值。

plus.bluetooth.startBluetoothDevicesDiscovery({

allowDuplicatesKey: true,

success: function(e){

outSet('開始搜尋成功!');

},

failed: function(e){

outSet('開始搜尋失敗! '+JSON.stringify(e));

}

});

plus.bluetooth.onBluetoothDeviceFound(function(e){

var devices = e.devices;

for(var i in devices){

if (devices[i].name == deviceName){

var device = devices[i];

rssi_value = device.RSSI;

}

}

});