下面是我的代码: MainActivity:
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();
}
}SignalsStorage:
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);
}我有一些蓝牙信标。我可以得到他们的名字,入口等,我也可以得到rssi的技术,但只有一次。我需要接收每10秒的测量,rssi,我之前发现的每一个设备10秒。但是每个设备的广播接收只工作一次,而LeScanner根本不起作用。我怎么能做到呢?
发布于 2018-10-30 08:09:39
不久前我也遇到了同样的问题,这就是我所做的。
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);
}
}
}
};我做的事情有点不同。我想要一个扫描RSSI的设备,它的MAC id我已经知道了。一个braodcast接收器一直循环在发现过程中,另一个进行实际的发现。
https://stackoverflow.com/questions/53029696
复制相似问题