gpt4 book ai didi

java - 我如何在一秒内实时获得蓝牙 RSSI 10 次,持续 10 秒

转载 作者:行者123 更新时间:2023-11-29 23:28:59 27 4
gpt4 key购买 nike

这是我的代码:主要 Activity :

public class MainActivity extends AppCompatActivity {

private final static int REQUEST_ENABLE_BT=1;
private BluetoothAdapter bluetooth;
private BluetoothLeScanner bluetoothLeScanner;
private LinearLayout devicesList;
private HashSet<BluetoothDevice> devices = new HashSet();
private Pattern p = Pattern.compile("Beacon_\\d*");
private boolean discovering = false;

private final BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if(BluetoothDevice.ACTION_FOUND.equals(intent.getAction())){
// Получаем объект BluetoothDevice из интента
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
Matcher m = p.matcher(device.getName());
if(!devices.contains(device) && m.matches()) {
TextView textView = new TextView(getApplicationContext());
textView.setText(device.getName());
devicesList.addView(textView);
devices.add(device);
}
} else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(intent.getAction())) {
Toast.makeText(getApplicationContext(), "Сканирование завершено", Toast.LENGTH_LONG).show();
discovering = false;
startDiscoveringButton.setText("Начать новое сканирование");
}
}
};

private final ScanCallback scanCallback = new ScanCallback() {
@Override
public void onScanResult(int callbackType, ScanResult result) {
super.onScanResult(callbackType, result);
System.out.println(result.getRssi());
}

@Override
public void onBatchScanResults(List<ScanResult> results) {
super.onBatchScanResults(results);
}

@Override
public void onScanFailed(int errorCode) {
super.onScanFailed(errorCode);
}
};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

devicesList = findViewById(R.id.devicesList);
startDiscoveringButton = findViewById(R.id.startDiscoveringButton);
checkAndSaveSignalsButton = findViewById(R.id.checkAndSaveSignalsButton);

bluetooth = BluetoothAdapter.getDefaultAdapter();
if(bluetooth != null) {
if (!bluetooth.isEnabled()) {
// Bluetooth выключен. Предложим пользователю включить его.
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
} else {
startScanning();
}
} else {
Toast.makeText(this, "Bluetooth не поддерживается", Toast.LENGTH_LONG).show();
}
}

@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(broadcastReceiver);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == REQUEST_ENABLE_BT) {
startScanning();
}
}

private void startScanning() {
Toast.makeText(this, "Начинаю сканирование", Toast.LENGTH_SHORT).show();
IntentFilter filter = new IntentFilter();
filter.addAction(BluetoothDevice.ACTION_FOUND);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
registerReceiver(broadcastReceiver, filter);
bluetooth.startDiscovery();
discovering = true;
startDiscoveringButton.setText("Идёт сканирование ...");
bluetoothLeScanner = bluetooth.getBluetoothLeScanner();
bluetoothLeScanner.startScan(scanCallback);
}

private void clear() {
devices.clear();
devicesList.removeAllViews();
}
}

信号存储:

 public void checkSignals(final HashSet<BluetoothDevice> devices) {
new Timer().schedule(new TimerTask() {
@Override
public void run() {
measurings.add(new HashMap<String, Double>());
for(int i = 0; i < 10; ++i) {

}
}
}, 0, 1000);
}

我有一些蓝牙信标。我可以得到他们的名字、地址等,我也可以得到 ech 的 rssi,但只有一次。我需要每秒接收 10 个我之前发现的每台设备的 10 次测量 rssi,持续 10 秒。但是广播接收器对每个设备只工作一次,而 LeScanner 根本不工作。我该怎么做?

最佳答案

我前一段时间遇到了同样的问题,这就是我所做的。

 private final BroadcastReceiver brBluetoothScanningLooper=new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action=intent.getAction();
if(BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)){
if(deviceFound==false) {
bluetoothAdapter.cancelDiscovery();
bluetoothAdapter.startDiscovery();
}
}
}

};


private final BroadcastReceiver brNearbyBluetoothDevices=new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action=intent.getAction();
if(BluetoothDevice.ACTION_FOUND.equals(action)){
BluetoothDevice device=intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

String deviceName=device.getName();
String deviceHardwareAddress=device.getAddress();
int rssi=intent.getShortExtra(BluetoothDevice.EXTRA_RSSI,Short.MIN_VALUE);

if(bondedList.contains(deviceHardwareAddress)){

if(!trackerList.contains(deviceHardwareAddress)){
if(trackerList.isEmpty()){
MAC=deviceHardwareAddress;
}
trackerList.add(deviceHardwareAddress);
rssiList.add(String.valueOf(rssi));
displayList.add(deviceName+" "+rssi);
arrayAdapter.notifyDataSetChanged();
lv.setAdapter(arrayAdapter);
}
if(trackerList.contains(deviceHardwareAddress)){
rssiList.set(trackerList.indexOf(deviceHardwareAddress), String.valueOf(rssi));
displayList.set(trackerList.indexOf(deviceHardwareAddress),deviceName+" "+(rssiList.get(trackerList.indexOf(deviceHardwareAddress))));
arrayAdapter.notifyDataSetChanged();
lv.setAdapter(arrayAdapter);
}
}
}
};

我做的事情有些不同。我想要一个我已经知道其 MAC ID 的设备的扫描 RSSI。一个广播接收器不断循环发现过程,另一个进行实际发现

关于java - 我如何在一秒内实时获得蓝牙 RSSI 10 次,持续 10 秒,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53029696/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com