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

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

问题描述

目标:构建一个Android应用程序,发现BT设备的名称和地址范围内,并提交自己的价值观,以一个web服务。英国电信设备尚未previously结合到主机设备,我只想轮询一切,我走的。

我做了什么:

  1. 在点滴记录文档。
  2. 实施了主机设备的BT适配器的本地实例。
  3. 在实施了通知,让BT,如果它未启用。
  4. 在注册的广播接收器和意图解析 ACTION_FOUNDs 脱落的 startDiscovery()
  5. 注册的蓝牙 BLUETOOTH_ADMIN 在清单中的权限。

东西的工作(如用增量控制台记录测试),直到 startDiscovery()


无奈:

  • startDiscovery() - 我怀疑我在错误的情况下通过这一点。确实这个方法需要被妥善安置内的功能是什么背景?

如果你已经能够得到这个方法的工作,我非常AP preciate你的智慧。

更新 - 这里的的code进行精简简化版本,是造成我的悲伤;这种简化概括我的错误。这code运行时,它抛出任何 cat.log 错误或其他错误,它根本不给任何输出。

 包aqu.bttest;

进口android.app.Activity;
进口android.bluetooth.BluetoothAdapter;
进口android.bluetooth.BluetoothDevice;
进口android.content.BroadcastReceiver;
进口android.content.Context;
进口android.content.Intent;
进口android.content.IntentFilter;
进口android.os.Bundle;
进口android.widget.Toast;

公共类BT2Activity延伸活动{

私人BluetoothAdapter MBTA;
私人SingBroadcastReceiver mReceiver;

/ **第一次创建活动时调用。 * /
@覆盖
公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);

  //注册本地BT适配器
    MBTA = BluetoothAdapter.getDefaultAdapter();
    //检查,看看是否有BT在Android设备上的所有
    如果(MBTA == NULL){
        INT持续时间= Toast.LENGTH_SHORT;
        Toast.makeText(这一点,没有蓝牙的这款手机,持续时间).show();
    }
    //让我们的用户能够BT,如果它是不是已经
    如果(!mBTA.isEnabled()){
        意图enableBT =新的意图(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(enableBT,0xDEADBEEF);
    }
    //取消任何现有的BT设备发现
    如果(mBTA.isDiscovering()){
        mBTA.cancelDiscovery();
    }
    //重新启动的发现
    mBTA.startDiscovery();

    //让我们的广播接收器来注册我们的东西
    mReceiver =新SingBroadcastReceiver();
    IntentFilter的IFilter的=新的IntentFilter(BluetoothDevice.ACTION_FOUND);
    this.registerReceiver(mReceiver,IFilter的);
}

私有类SingBroadcastReceiver扩展的BroadcastReceiver {

    公共无效的onReceive(上下文的背景下,意图意图){
        串动= intent.getAction(); //可能需要链这一个识别功能
        如果(BluetoothDevice.ACTION_FOUND.equals(动作)){
            //从意图获取BluetoothDevice类对象
            BluetoothDevice类设备= intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            //添加姓名和地址到一个数组适配器举杯展示
            串DERP = device.getName()+ - + device.getAddress();
            Toast.makeText(背景下,DERP,Toast.LENGTH_LONG);
        }
    }
}
 

}

解决方案
  

确实这个方法需要被妥善安置内的功能是什么背景。

要简单地说,你应该使用 startDiscovery()当你想你的应用程序发现本地的蓝牙设备......举例来说,如果你想实现一个 ListActivity 的扫描和动态地将附近的蓝牙设备连接到的ListView (见<一href="http://developer.android.com/resources/samples/BluetoothChat/src/com/example/android/BluetoothChat/DeviceListActivity.html"><$c$c>DeviceListActivity).

您使用了 startDiscovery()的方法应该是这个样子:

  1. 定义一个类变量重新presenting本地蓝牙适配器。

      BluetoothAdapter mBtAdapter = BluetoothAdapter.getDefaultAdapter();
     

  2. 请检查你的设备已经是发现。如果是,则取消发现

     如果(mBtAdapter.isDiscovering()){
        mBtAdapter.cancelDiscovery();
    }
     

  3. 立即检查(也可能取消)发现模式后,通过调用开始发现,

      mBtAdapter.startDiscovery();
     

  4. 要非常小心,一般不小心留下您的设备在发现模式。执行设备发现是一个沉重的程序为蓝牙适配器,并会消耗大量的资源。例如,你要确保你检查/尝试建立连接之前取消发现。您很可能希望取消的发现你的的onDestroy 方法了。

让我知道,如果这有助于...如果你仍然有问题,你的logcat的输出和/或您收到任何错误消息更新你的答案,也许我可以帮你多一点。

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.

What I've done:

  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.

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


Frustration:

  • 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.

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.

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).

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

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

    BluetoothAdapter mBtAdapter = BluetoothAdapter.getDefaultAdapter();
    

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

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

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

    mBtAdapter.startDiscovery();
    

  4. Be very careful in general about accidentally leaving your device in discovery-mode. Performing device discovery is a heavy procedure for the Bluetooth adapter and will consume a lot of its resources. For instance, you want to make sure you check/cancel discovery prior to attempting to make a connection. You most likely want to cancel discovery in your onDestroy method too.

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天全站免登陆