android 蓝牙扫描时间,更改蓝牙查询扫描时间

根据方法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();

}

}

};

}

+0

我不认为你可以控制这个,因为蓝牙发现协议需要时间扫描附近的设备,这是蓝牙堆栈的一部分。 –

2013-03-11 22:12:22

+0

也许有什么选择,当蓝牙发现第一个设备时,它可以停止扫描?或者在蓝牙继续扫描时开始操作第一个发现的设备?谢谢 –

2013-03-11 22:16:29

+0

如果你想连接到一个特定的设备,你可以存储它的mac地址,并直接连接,而无需扫描 –

2013-03-11 23:37:54

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值