连续跟踪移动的使用Android的附近可用的蓝牙连接 [英] Continuously tracing the available bluetooth connections in the mobile's vicinity using android

查看:202
本文介绍了连续跟踪移动的使用Android的附近可用的蓝牙连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要不断监测这是present每5秒的蓝牙设备。我写了下面的一段code这是不是为我工作。

I need to continuously monitor the bluetooth devices which are present every 5 seconds. I have written the following piece of code which is not working for me.

private static final int DISCOVERY_REQUEST = 1;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_bluetooth_connection);

    final TextView tv = (TextView) findViewById(R.id.textView1);
    tv.setText("");
    final BluetoothAdapter mBluetoothAdapter = BluetoothAdapter
            .getDefaultAdapter();
    mBluetoothAdapter.startDiscovery();
    final BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            // When discovery finds a device

            if (BluetoothDevice.ACTION_FOUND.equals(action)) {
                // Get the BluetoothDevice object from the Intent
                BluetoothDevice device = intent
                        .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                tv.append(device.getName() + "-"
                        + /* device.getAddress()+ */"\n");
                tv.append("here");
                /*
                 * if (device.getName().equals("ONCEWASCUT-L7")) {
                 * tv.append("this is in the vicinity");
                 * 
                 * }
                 */
            } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED
                    .equals(action)) {
                tv.append("\nEntered the Finished\n ");
                mBluetoothAdapter.startDiscovery();
            }
        }

    };
    String aDiscoverable = BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE;
    startActivityForResult(new Intent(aDiscoverable), DISCOVERY_REQUEST);
    IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
    registerReceiver(mReceiver, filter);
    mBluetoothAdapter.startDiscovery();
}

Ç$ C需要被添加到这个什么$使得aplication作品fine.Note我需要监视的蓝牙设备,每5秒

What code needs to be added to this so that the aplication works fine.Note that I need to monitor the bluetooth devices every 5 seconds.

推荐答案

您可以使用Timer和TimerTask连续检查特定设备是否可用。

You can use the Timer and TimerTask for continuously checking that particular device is available or not.

例如

定时器定时器=新的Timer();

Timer timer = new Timer();

timer.scheduleAtFixedRate(新的TimerTask {

timer.scheduleAtFixedRate(new TimerTask{

@覆盖

公共无效的run(){

public void run() {

在这里,您可以使用处理程序或任何你想要使用。

Here you can use handler or whatever you want to use.

}

},延迟期);

延迟 - 首次执行之前,以毫秒为单位的时间量

delay - amount of time in milliseconds before first execution.

期 - 在后续执行之间的毫秒的时间量

period - amount of time in milliseconds between subsequent executions.

http://developer.android.com/reference/java/util/Timer.html#scheduleAtFixedRate

请参阅有关详细信息,此链接。

Refer this link for more details.

这篇关于连续跟踪移动的使用Android的附近可用的蓝牙连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆