Java 更改蓝牙查询扫描时间

Java 更改蓝牙查询扫描时间,java,android,bluetooth,Java,Android,Bluetooth,根据该方法,发现过程通常涉及大约12秒的查询扫描。是否有任何措施可以减少查询扫描时间(低于12秒) 这是我的main活动: package com.example.bluetoothsignal; import android.os.Bundle; import android.app.Activity; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import and

根据该方法,发现过程通常涉及大约12秒的查询扫描。是否有任何措施可以减少查询扫描时间(低于12秒)

这是我的
main活动

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(地址)直接连接到此设备

因此,直接使用
.getRemoteDevice(地址)您可以为扫描设备保存。而是直接连接

例如:

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

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

然后
sock.connect()等。。希望您能拿到它

当它停止时,您可以呼叫startDiscovery。在boardcast接收器中:

if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action))
{
    btAdapter.startDiscovery();
}

我认为您无法控制这一点,因为蓝牙发现协议需要时间来扫描附近的设备,这是蓝牙协议栈的一部分。可能有任何选项,当蓝牙发现第一个设备时,它可以停止扫描吗?或者在蓝牙继续扫描时开始操作第一个发现的设备?谢谢如果你想连接到一个特定的设备,你可以存储它的mac地址并直接连接,而无需扫描谢谢,如果我有这个设备的mac地址,我如何直接连接并找到他的蓝牙信号强度?谢谢