Android中的蓝牙设备发现——startDiscovery() [英] Bluetooth device discovery in Android -- startDiscovery()

查看:73
本文介绍了Android中的蓝牙设备发现——startDiscovery()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目标:构建一个 Android 应用,该应用可以发现范围内 BT 设备的名称和地址,并将它们的值提交给网络服务.BT 设备之前没有绑定到主机设备,我只是想边走边轮询.

Goal: Build an Android app that discovers the names and addresses of BT devices within range and submits their values to a webservice. BT devices have not been previously bonded to the host device, I just want to poll everything as I walk about.

我做了什么:

  1. 仔细阅读文档.
  2. 实现了主机设备的 BT 适配器的本地实例.
  3. 实施通知以启用 BT(如果未启用).
  4. 注册广播接收器和 Intents 以解析来自 startDiscovery()ACTION_FOUNDs.
  5. 在清单中注册了 BLUETOOTHBLUETOOTH_ADMIN 权限.
  1. Pored over documentation.
  2. Implemented a local instance of the host device's BT adapter.
  3. Implemented a notification to enable BT if it isn't enabled.
  4. Registered Broadcast Receivers and Intents to parse the ACTION_FOUNDs coming off of startDiscovery().
  5. Registered BLUETOOTH and BLUETOOTH_ADMIN permissions in the manifest.

startDiscovery() 之前,一切正常(通过增量控制台日志记录测试).

Things work (as tested with incremental console logging) up until startDiscovery().


沮丧:

  • startDiscovery() -- 我怀疑我在错误的上下文中传递了这个.此方法需要置于什么上下文中才能正常运行?
  • startDiscovery() -- I suspect I am passing this in the wrong context. What context does this method need to be placed within to function properly?

如果您能够使这种方法发挥作用,我将非常感谢您的智慧.

If you have been able to get this method working, I would very much appreciate your wisdom.

更新 - 这是代码的精简版,让我很伤心;这种简化概括了我的错误.这段代码运行,它不会抛出 cat.log 错误或其他错误,它只是不给出任何输出.

UPDATE - here's a stripped down simplified version of the code that is causing me grief; this simplification recapitulates my error. This code runs, it throws no cat.log errors or other errors, it simply doesn't give any output.

package aqu.bttest;

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.os.Bundle;
import android.widget.Toast;

public class BT2Activity extends Activity {

private BluetoothAdapter mBTA;
private SingBroadcastReceiver mReceiver;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

  //register local BT adapter
    mBTA = BluetoothAdapter.getDefaultAdapter();
    //check to see if there is BT on the Android device at all
    if (mBTA == null){
        int duration = Toast.LENGTH_SHORT;
        Toast.makeText(this, "No Bluetooth on this handset", duration).show();
    }
    //let's make the user enable BT if it isn't already
    if (!mBTA.isEnabled()){
        Intent enableBT = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(enableBT, 0xDEADBEEF);
    }
    //cancel any prior BT device discovery
    if (mBTA.isDiscovering()){
        mBTA.cancelDiscovery();
    }
    //re-start discovery
    mBTA.startDiscovery();

    //let's make a broadcast receiver to register our things
    mReceiver = new SingBroadcastReceiver();
    IntentFilter ifilter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
    this.registerReceiver(mReceiver, ifilter);
}

private class SingBroadcastReceiver extends BroadcastReceiver {

    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction(); //may need to chain this to a recognizing function
        if (BluetoothDevice.ACTION_FOUND.equals(action)){
            // Get the BluetoothDevice object from the Intent
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            // Add the name and address to an array adapter to show in a Toast
            String derp = device.getName() + " - " + device.getAddress();
            Toast.makeText(context, derp, Toast.LENGTH_LONG);
        }
    }
}

}

推荐答案

这个方法需要放在什么上下文中才能正常运行.

What context does this method need to be placed within to function properly.

简单地说,当您希望应用程序发现本地蓝牙设备时,您应该使用 startDiscovery()...例如,如果您想实现一个 ListActivity> 扫描附近的蓝牙设备并将其动态添加到 ListView(参见 DeviceListActivity).

To put it simply, you should use startDiscovery() when you want your application to discover local Bluetooth devices... for instance, if you wanted to implement a ListActivity that scans and dynamically adds nearby Bluetooth devices to a ListView (see DeviceListActivity).

您对 startDiscovery() 方法的使用应该如下所示:

Your usage of the startDiscovery() method should look something like this:

  1. 定义一个表示本地蓝牙适配器的类变量.

  1. Define a class variable representing the local Bluetooth adapter.

BluetoothAdapter mBtAdapter = BluetoothAdapter.getDefaultAdapter();

  • 检查您的设备是否已经在发现".如果是,则取消发现.

  • Check to see if your device is already "discovering". If it is, then cancel discovery.

    if (mBtAdapter.isDiscovering()) {
        mBtAdapter.cancelDiscovery();
    }
    

  • 在检查(并可能取消)发现模式后立即通过调用开始发现,

  • Immediately after checking (and possibly canceling) discovery-mode, start discovery by calling,

    mBtAdapter.startDiscovery();
    

  • 通常要非常小心,以免意外将设备置于发现模式.执行设备发现对于蓝牙适配器来说是一个繁重的过程,会消耗大量资源.例如,您要确保在尝试建立连接之前检查/取消发现.您很可能也想在 onDestroy 方法中取消发现.

    告诉我这是否有帮助...如果您仍然遇到问题,请使用您的 logcat 输出和/或您收到的任何错误消息更新您的答案,也许我可以为您提供更多帮助.

    Let me know if this helped... and if you are still having trouble, update your answer with your logcat output and/or any error messages you are getting, and maybe I can help you out a bit more.

    这篇关于Android中的蓝牙设备发现——startDiscovery()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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