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

标签 java android bluetooth

根据方法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/

相关文章:

windows - 如何在 Windows 中获取有关串行 (COM) 端口的特定信息?

ios - CoreBluetooth 框架可以与从蓝牙 2.0 串行模块配对并交谈吗?

android - 使用 JUnit5 assertThrows 和 MockWebServer 测试挂起函数的异常

android - 通过 Intent 传递图像导致图像质量下降

java - IMDG (Hazelcast) 如何加强数据一致性

java - Java下载文件及常见错误

android - 导入包含 .so 文件的 aar 库后出现 UnsatisfiedLinkError

java - 使用 bluecove 通过蓝牙实现 PC 到 PC

找不到 java.sql.sqlexception 列

java - 具有多个模块的 Maven 项目无法找到模块的 pom.xml - Jenkins/Eclipse