Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/229.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android中的蓝牙设备发现——startDiscovery()_Android_Android Intent_Bluetooth_Android Adapter_Android Context - Fatal编程技术网

Android中的蓝牙设备发现——startDiscovery()

Android中的蓝牙设备发现——startDiscovery(),android,android-intent,bluetooth,android-adapter,android-context,Android,Android Intent,Bluetooth,Android Adapter,Android Context,目标:构建一个Android应用程序,发现范围内BT设备的名称和地址,并将其值提交给Web服务。BT设备之前没有绑定到主机设备,我只想边走边轮询所有内容 我所做的: 仔细研究文档 实现了主机设备的BT适配器的本地实例 实现了一个通知,以便在未启用BT时启用它 已注册的广播接收器,并打算解析操作来自startDiscovery() 在清单中注册了蓝牙和蓝牙管理权限 在startDiscovery()之前,一切都正常(通过增量控制台日志记录测试) 挫折感: startDiscovery()--我

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

我所做的:

  • 仔细研究文档
  • 实现了主机设备的BT适配器的本地实例
  • 实现了一个通知,以便在未启用BT时启用它
  • 已注册的广播接收器,并打算解析
    操作
    来自startDiscovery()
  • 在清单中注册了蓝牙蓝牙管理权限
  • 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()
    。。。例如,如果您想要实现一个
    ListActivity
    ,它可以扫描附近的蓝牙设备并将其动态添加到
    ListView
    (请参阅)

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

  • 定义表示本地蓝牙适配器的类变量

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

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

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


  • 让我知道这是否有帮助。。。如果您仍然有问题,请使用logcat输出和/或您收到的任何错误消息更新您的答案,也许我可以帮您解决更多问题。

    了解您收到的错误会有所帮助。。。或者至少是什么让你相信
    startDiscovery()
    运行不正常。这很有帮助。此外,将结果传递给ArrayAdapter而不是Toast也确实有帮助。有没有办法在BG服务中保持连续扫描而不使用put Timer…任何系统级API。@Shubh我不这么认为。设置此限制的原因是为了确保应用程序不会通过在后台连续扫描而滥用蓝牙。。。我也在试着这么做,但会有什么错误呢-