3

方法によると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();
            }
        }
    };
}
4

2 に答える 2

0

完全なアドレスがわからないデバイスに接続する場合は、完全な検出を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();など..あなたがそれを手に入れたことを願っています

于 2013-03-13T04:04:30.147 に答える