Android 蓝牙连续搜索不起作用?

发布于 2024-11-27 05:42:34 字数 2143 浏览 0 评论 0原文

我编写了以下代码,并希望我的蓝牙持续工作,是否有任何正确的方法可以让我的 startdiscovery 一直被扫描,直到我的程序运行?

          mBluetoothAdapter.startDiscovery();

      System.out.println("flow is here");
      // Create a BroadcastReceiver for ACTION_FOUND
      final BroadcastReceiver mReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
        // When discovery finds a device
           if (BluetoothDevice.ACTION_FOUND.equals(action)) {

                System.out.println("Print in IF");
                // 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 ListView
                mNewDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress());               
                //System.out.println(mNewDevicesArrayAdapter);

                //info = mNewDevicesArrayAdapter.getItem(x).toString();
                System.out.println("Found :\t"+device.getAddress()+"\tName :"+device.getName());

            }
            else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
                    System.out.prinlnt("Else BOdy ");
                  mBluetoothAdapter.startDiscovery();
               }    
            }

甚至 System.out.println("Else BOdy") 根本没有显示...

  int delay = 10000; // delay for 1 sec. 
  int period = 15000; // repeat every 10 sec. 
  Timer timer = new Timer(); 
  timer.scheduleAtFixedRate(new TimerTask() { 
          public void run() 
          { 
              displayData();  // display the data
          }
        private void displayData() {
            System.out.println("Veer Suthar is calling method after 10 seconds");
            // TODO Auto-generated method stub
            mBluetoothAdapter.startDiscovery();
        } 
      }, delay, period);

这就是我所做的,所以它被调用,并且

if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {

这个方法对我不起作用。

I have written the following code and want my bluetooth continously working, and is there any proper way so that my startdiscovery should be scan until my program in running?

          mBluetoothAdapter.startDiscovery();

      System.out.println("flow is here");
      // Create a BroadcastReceiver for ACTION_FOUND
      final BroadcastReceiver mReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
        // When discovery finds a device
           if (BluetoothDevice.ACTION_FOUND.equals(action)) {

                System.out.println("Print in IF");
                // 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 ListView
                mNewDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress());               
                //System.out.println(mNewDevicesArrayAdapter);

                //info = mNewDevicesArrayAdapter.getItem(x).toString();
                System.out.println("Found :\t"+device.getAddress()+"\tName :"+device.getName());

            }
            else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
                    System.out.prinlnt("Else BOdy ");
                  mBluetoothAdapter.startDiscovery();
               }    
            }

Even System.out.println("Else BOdy") is not showing at all...

  int delay = 10000; // delay for 1 sec. 
  int period = 15000; // repeat every 10 sec. 
  Timer timer = new Timer(); 
  timer.scheduleAtFixedRate(new TimerTask() { 
          public void run() 
          { 
              displayData();  // display the data
          }
        private void displayData() {
            System.out.println("Veer Suthar is calling method after 10 seconds");
            // TODO Auto-generated method stub
            mBluetoothAdapter.startDiscovery();
        } 
      }, delay, period);

This is what simply I have done, so it is called, and

if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {

This method didn't work with me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文