Android 上的蓝牙:StartDiscovery 不起作用。无法扫描设备

DoY*_*ift 2 android bluetooth

我是 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();
                }

           }
Run Code Online (Sandbox Code Playgroud)

这是我在“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();
    }



}
Run Code Online (Sandbox Code Playgroud)

这是我的扫描功能,我在其中调用 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();

}
Run Code Online (Sandbox Code Playgroud)

这是广播接收器:

 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());
            }
          }
        };
Run Code Online (Sandbox Code Playgroud)

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

我正在像这样在 oncreate 函数中注册广播接收器:

filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
   registerReceiver(mReceiver, filter);
Run Code Online (Sandbox Code Playgroud)

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

obd*_*key 6

检查你有

 <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
Run Code Online (Sandbox Code Playgroud)

在您的清单中。

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