有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java更改蓝牙查询扫描时间

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

这是我的MainActivity

package com.example.bluetoothsignal;

import 安卓.os.Bundle;
import 安卓.app.Activity;
import 安卓.bluetooth.BluetoothAdapter;
import 安卓.bluetooth.BluetoothDevice;
import 安卓.content.BroadcastReceiver;
import 安卓.content.Context;
import 安卓.content.Intent;
import 安卓.content.IntentFilter;
import 安卓.view.Menu;
import 安卓.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(
                "安卓.bluetooth.device.action.FOUND");
        registerReceiver(this.mReceiver, localIntentFilter1);
        IntentFilter localIntentFilter2 = new IntentFilter(
                "安卓.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 ("安卓.bluetooth.device.action.FOUND".equals(action)) {
                BluetoothDevice localBluetoothDevice = (BluetoothDevice) paramIntent
                        .getParcelableExtra("安卓.bluetooth.device.extra.DEVICE");
                int s = paramIntent.getIntExtra(
                        "安卓.bluetooth.device.extra.RSSI", -32768);
                short s2 = paramIntent.getShortExtra(
                        "安卓.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 (("安卓.bluetooth.adapter.action.DISCOVERY_FINISHED"
                    .equals(action))) {
                if (MainActivity.this.mBtAdapter.isDiscovering()) {
                    MainActivity.this.mBtAdapter.cancelDiscovery();
                }
                statusText.setText("");
                MainActivity.this.mBtAdapter.startDiscovery();
            }
        }
    };
}

共 (1) 个答案

  1. # 1 楼答案

    如果你想连接到一个不知道完整地址的设备,你必须使用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();等等。。希望你收到