【问题标题】:Bluetooth device discovery in Android -- startDiscovery()Android中的蓝牙设备发现——startDiscovery()
【发布时间】:2012-05-20 13:53:55
【问题描述】:

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

我做了什么:

  1. 仔细阅读文档。
  2. 实现了主机设备的 BT 适配器的本地实例。
  3. 实现了通知以启用 BT(如果未启用)。
  4. 注册的广播接收器和意图解析来自 startDiscovery()ACTION_FOUNDs
  5. 在清单中注册了 BLUETOOTHBLUETOOTH_ADMIN 权限。

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


沮丧:

  • startDiscovery() -- 我怀疑我在错误的上下文中传递了这个。此方法需要放置在什么上下文中才能正常运行?

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

更新 - 这是一个让我伤心的代码的精简版;这种简化概括了我的错误。这段代码运行,它没有抛出 cat.log 错误或其他错误,它根本没有给出任何输出。

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);
        }
    }
}

}

【问题讨论】:

  • 了解您遇到的错误会有所帮助...或者至少是什么导致您相信startDiscovery() 运行不正确。

标签: android android-intent bluetooth android-adapter android-context


【解决方案1】:

此方法需要放置在什么上下文中才能正常运行。

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

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

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

    BluetoothAdapter mBtAdapter = BluetoothAdapter.getDefaultAdapter();
    
  2. 检查您的设备是否已经在“发现”。如果是,则取消发现。

    if (mBtAdapter.isDiscovering()) {
        mBtAdapter.cancelDiscovery();
    }
    
  3. 检查(并可能取消)发现模式后,立即通过调用开始发现,

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

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

【讨论】:

  • 这有帮助。此外,将结果传递给 ArrayAdapter 而不是 Toast 肯定也有帮助。
  • 有没有办法在 BG 服务中保持连续扫描,而无需放置 Timer...任何系统级 API。
  • @Shubh 我不这么认为。之所以设置此限制,是为了确保应用程序不会通过在后台持续扫描来滥用Bluetooth
  • 非常老...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-04-17
  • 1970-01-01
  • 1970-01-01
  • 2012-04-19
  • 2020-08-30
  • 1970-01-01
  • 2022-01-04
相关资源
最近更新 更多