我正在尝试测量执行BLE扫描的Galaxy Tab 4(蓝牙4.0)上的iBeacon距离。我收到的rssi数据似乎是错误的。它总是在-2.147483648E9。
这是我的代码:
public class MainActivity extends AppCompatActivity {
private ScanFilter mScanFilter;
private ScanSettings mScanSettings;
private BluetoothLeScanner mBluetoothLeScanner;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findBeaconClickListener();
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
mBluetoothLeScanner = mBluetoothAdapter.getBluetoothLeScanner();
}
public void findBeaconClickListener() {
setScanFilter();
setScanSettings();
Button findBeacon = (Button) findViewById(R.id.findBeacon);
findBeacon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mBluetoothLeScanner.startScan(Arrays.asList(mScanFilter), mScanSettings, mScanCallback);
}
});
}
private void setScanFilter() {
ScanFilter.Builder mBuilder = new ScanFilter.Builder();
ParcelUuid pUUID = ParcelUuid.fromString("0000fff0-0000-1000-8000-00805f9b34fb");
mBuilder.setServiceUuid(pUUID);
mScanFilter = mBuilder.build();
}
private void setScanSettings() {
ScanSettings.Builder mBuilder = new ScanSettings.Builder();
mBuilder.setReportDelay(0);
mBuilder.setScanMode(ScanSettings.SCAN_MODE_LOW_POWER);
mScanSettings = mBuilder.build();
}
protected ScanCallback mScanCallback = new ScanCallback() {
@Override
public void onScanResult(int callbackType, ScanResult result) {
ScanRecord mScanRecord = result.getScanRecord();
int txPower = mScanRecord.getTxPowerLevel();
String deviceName = mScanRecord.getDeviceName();
int mRssi = result.getRssi();
}
};
所以当我从结果中读取这个rssi值时,它显然是错误的。 devicename和txPower似乎没问题。