2

我是 android 新手,我正在制作一个具有蓝牙功能的应用程序。我可以设置蓝牙适配器,获取我自己的设备信息,但我无法使用 startdiscovery 来发现蓝牙设备。当我开始扫描时,它什么也不做。

我正在使用 onclicklistner 开始扫描:

  bt.setOnClickListener(new OnClickListener() {
           @Override
           public void onClick(View v) {
               if (!(bluetooth.isEnabled())) {
                   status = "Bluetooth is not Enabled.";
                   Toast.makeText(AddUser.this, status, Toast.LENGTH_LONG).show();
                   Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                startActivityForResult(enableBtIntent, 1);

                }
                else
                {

                    scand();
                }

           }

这是我在“public void onCreate”函数之后放置的 onActivityResult 函数:

 protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
    System.out.println(resultCode);
    if (resultCode ==  RESULT_CANCELED) {
        status="Error Enabling bluetooth";
        Toast.makeText(AddUser.this, status, Toast.LENGTH_LONG).show();
    } else {

            scand();
    }



}

这是我的 scand 函数,我在其中调用了 startdiscovery:

    private void scand()
{



   bluetooth.startDiscovery(); 
   Log.d("itmes", ""+items.size());
   item1 = new String[items.size()];
   item1 = items.toArray(item1);
   AlertDialog.Builder builder = new AlertDialog.Builder(this);
   builder.setTitle("Choose a device");
   builder.setItems(item1, new DialogInterface.OnClickListener() {

       public void onClick(DialogInterface dialog, int item) {
            Toast.makeText(getApplicationContext(), item1[item], Toast.LENGTH_SHORT).show();

       }

    });

    AlertDialog alert = builder.create();

    alert.show();

}

这是广播接收器:

 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();

            if (BluetoothDevice.ACTION_FOUND.equals(action)) 
            {
                   Log.e("br", "--- device found ---");
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

               items.add(device.getName());
            }
          }
        };

在广播接收器的上述代码中,我试图将找到的设备名称放在字符串 ArrayList“items”中。

我在 oncreate 函数中注册广播接收器,如下所示:

filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
   registerReceiver(mReceiver, filter);

我在 androidmanifest 文件中设置了蓝牙权限。在上面的 scand 函数中,它应该显示已发现设备的列表,但它显示的是一个只有标题的空对话框。请告诉我如何正确使用 startdiscovery 和 broadcastreceiver 在 alertdialog 中显示结果。

4

5 回答 5

7

检查你有

 <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

在你的清单中。

请参阅 https://developer.android.com/reference/android/bluetooth/BluetoothDevice#ACTION_FOUND

于 2018-06-04T17:57:19.057 回答
1

是异步的startDiscovery(),您不会在调用后立即收到结果。移动代码以显示对话框到一个函数,比如 public void showScanResult() 并在你的 onReceive 中调用它。

于 2013-04-24T07:02:10.930 回答
1
  discovery() {
     IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
     this.registerReceiver(mReceiver, filter);
     // Register for broadcasts when discovery has finished
     filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
     this.registerReceiver(mReceiver, filter);
        
     // If we're already discovering, stop it
     if (mBluetoothAdapter.isDiscovering()) {
         mBluetoothAdapter.cancelDiscovery();
     }
     // Request discover from BluetoothAdapter
     mBluetoothAdapter.startDiscovery();
  }

广播接收器:

  private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
  @Override
  public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        // When discovery finds a device
        try {
            Broadcast=true;
            if (BluetoothDevice.ACTION_FOUND.equals(action)) {
                // Get the BluetoothDevice object from the Intent
                BluetoothDevice device = intent
                        .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

                // If it's already paired, skip it, because it's been listed
                // already
                if (device.getBondState() !=BluetoothDevice.BOND_BONDED)    {
                    
                    near_device.add(device.getAddress());
                
                // When discovery is finished, change the Activity title
            } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED
                    .equals(action)) {
                scanResult();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

};

 scanresult()
    {

            mBluetoothAdapter.cancelDiscovery();
            receiverCheck = false;
            // Unregister broadcast listeners
            Home.this.unregisterReceiver(mReceiver);
          // near device arraylist contain all device mac address
    }
于 2015-03-12T16:09:04.870 回答
0

好吧,在我看来,当您将数组 item1 设置为要在 AlertDialog 中显示的内容时,您是在告诉 Dialog 显示当时数组中的项目(一个空数组),这样您就不会看到任何元素。

我不确定您是否可以在这样做之后更新 item1 数组的内容并期望刷新 AlertDialog。我不认为这是这样工作的(但我从来没有尝试过,无论如何)。

我使用 ListView 和 Adapter 在我的应用程序中做了类似的事情,所以当您通过适配器添加项目时,ListView 会刷新(无论如何您都必须使用 adapter.notifyDataSetChanged()):

protected ArrayList<BluetoothDevice> foundDevices 
                      = new ArrayList<BluetoothDevice>();
private ListView foundDevicesListView;
private ArrayAdapter<BluetoothDevice> adapter;

foundDevicesListView = (ListView) findViewById(R.id.foundDevicesListView);

adapter = new ArrayAdapter<BluetoothDevice>(this,
            android.R.layout.simple_list_item_1, foundDevices);
foundDevicesListView.setAdapter(adapter);
//
//  Initialization, etc ....


    BroadcastReceiver mReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();

            // When discovery finds a new device
            if (BluetoothDevice.ACTION_FOUND.equals(action)) {
                // Get the BluetoothDevice object from the Intent
                BluetoothDevice device= intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE));
                if (!foundDevices.contains(device)) {
                    foundDevices.add(device);
                    adapter.notifyDataSetChanged();
                }
            }

            // When discovery cycle finished
            if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
                if(foundDevices==null || foundDevices.isEmpty()){
                    // Show not devices found message
                }
            }
        } 
于 2013-04-29T16:26:48.567 回答
0

如果您的应用针对 Android 11(API 级别 30)或更低版本,请使用 ACCESS_FINE_LOCATION,这是必要的,在 Android 的开发人员中

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

在此处输入图像描述

如果您的应用针对 API 级别 23 或更低,则必须在运行时检查权限和请求权限

if(checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
        discovery()
    } else {
        requestLocationPermissions()
    }

private fun discovery() {
    if (bluetoothAdapter == null) {
        //bluetooth can't work

    } else {
        if (bluetoothAdapter.isEnabled) {
            bluetoothAdapter.startDiscovery()
        } else {
            startBluetooth()
        }
    }
}

private fun startBluetooth() {
    val bluetoothIntent = Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE)
    startActivityForResult(bluetoothIntent, REQUET_ENABLE_BT)
}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    when(requestCode) {
        REQUET_ENABLE_BT -> {
            if (resultCode == RESULT_OK) {
                discovery()
            } else {
                Toast.makeText(baseContext, "bluetooth open failed", Toast.LENGTH_SHORT).show()
            }
        }
        else -> {
            super.onActivityResult(requestCode, resultCode, data)
        }
    }
}
于 2021-11-18T08:12:12.087 回答