gpt4 book ai didi

java - 更改蓝牙查询扫描时间

转载 作者:行者123 更新时间:2023-11-30 11:35:01 26 4
gpt4 key购买 nike

根据方法BluetoothAdapter.startDiscovery() ,发现过程通常涉及大约 12 秒的查询扫描。有什么可以减少扫描查询时间(低于 12 秒)?

这是我的 MainActivity:

package com.example.bluetoothsignal;

import android.os.Bundle;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.Menu;
import android.widget.EditText;

public class MainActivity extends Activity {

private EditText statusText;
private BluetoothAdapter mBtAdapter;

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

statusText = (EditText) findViewById(R.id.statusText);

statusText.setText("");
Intent discoverableIntent = new Intent(
BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
discoverableIntent.putExtra(
BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
startActivity(discoverableIntent);

IntentFilter localIntentFilter1 = new IntentFilter(
"android.bluetooth.device.action.FOUND");
registerReceiver(this.mReceiver, localIntentFilter1);
IntentFilter localIntentFilter2 = new IntentFilter(
"android.bluetooth.adapter.action.DISCOVERY_FINISHED");
registerReceiver(this.mReceiver, localIntentFilter2);
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mReceiver, filter);
this.mBtAdapter = BluetoothAdapter.getDefaultAdapter();
this.mBtAdapter.startDiscovery();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

private final BroadcastReceiver mReceiver = new BroadcastReceiver() {

public void onReceive(Context paramContext, Intent paramIntent) {
String action = paramIntent.getAction();
if ("android.bluetooth.device.action.FOUND".equals(action)) {
BluetoothDevice localBluetoothDevice = (BluetoothDevice) paramIntent
.getParcelableExtra("android.bluetooth.device.extra.DEVICE");
int s = paramIntent.getIntExtra(
"android.bluetooth.device.extra.RSSI", -32768);
short s2 = paramIntent.getShortExtra(
"android.bluetooth.device.extra.RSSI", Short.MIN_VALUE);
int s1 = paramIntent.getIntExtra(BluetoothDevice.EXTRA_RSSI,
Integer.MIN_VALUE);

String state = localBluetoothDevice.getAddress() + "\n"
+ localBluetoothDevice.getName() + "\n" + s + "\n" + s1
+ "\n" + s2;
statusText.setText(statusText.getText().toString() + state);
} else if (BluetoothDevice.ACTION_FOUND.equals(action)) {
// Get the BluetoothDevice object from the Intent
BluetoothDevice device = paramIntent
.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
// Add the name and address to an array adapter to show in a
// ListView
statusText.setText(statusText.getText().toString()
+ device.getName() + "\n" + device.getAddress());
} else if (("android.bluetooth.adapter.action.DISCOVERY_FINISHED"
.equals(action))) {
if (MainActivity.this.mBtAdapter.isDiscovering()) {
MainActivity.this.mBtAdapter.cancelDiscovery();
}
statusText.setText("");
MainActivity.this.mBtAdapter.startDiscovery();
}
}
};
}

最佳答案

如果您想连接到不知道完整地址的设备,则必须使用 BluetoothAdapter.startDiscovery() 进行完整发现,并在收到的地址中搜索那些你想要的。

如果您知道要连接的设备的完整地址,您可以使用 BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);

直接连接到该设备

因此,通过直接使用 .getRemoteDevice(address);,您可以节省用于扫描设备的时间。而是直接连接...

例子:

BluetoothDevice device = BluetoothAdapter.getDefaultAdapter().getRemoteDevice("00:1C:4D:02:A6:55");

sock = device.createRfcommSocketToServiceRecord(UUID.fromString("8e1f0cf7-508f-4875-b62c-fbb67fd34812"));

然后 sock.connect(); 等..希望你明白了

关于java - 更改蓝牙查询扫描时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15349673/

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