蓝牙在Android:StartDiscovery不工作。无法扫描设备蓝牙、设备、工作、StartDiscovery

2023-09-07 03:49:11 作者:______丶疼你

我是新来的Andr​​oid和我正在制作带有蓝牙功能的应用程序。我能够设置蓝牙适配器,一个取我自己的设备的信息,但我无法用startdiscovery发现的蓝牙设备。当我开始扫描它什么都不做。

I am new to android and i am making an application with bluetooth functionalities. I am able to set the bluetooth adaptor, an fetch my own device information, but i could not use startdiscovery to discover bluetooth devices. When i start a scan it does nothing.

我使用的是onclicklistner开始扫描:

i am using an onclicklistner to start a scan:

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

           }

下面是我刚才公共无效的onCreate功能后,把功能的onActivityResult:

Here is the onActivityResult function which i have put just after the "public void onCreate" function:

 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功能,对此我callng startdiscovery:

This is my scand function, in which i am callng 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());
            }
          }
        };

在上面的code为广播接收器,我试图把发现的设备名称在一个字符串ArrayList的项目。

In the Above code for broadcastreceiver, i am trying to put the found device name in a string ArrayList "items".

我的OnCreate中functon像这里面注册的BroadcastReceiver

I am registering broadcastreceiver inside the oncreate functon like this:

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

我已经设置了androidmanifest文件蓝牙权限。在上面的SCAND功能,它是假设,以显示发现的设备列表中,但它显示只有标题的空对话框。请告诉我怎么用startdiscovery和广播接收器正确地显示结果在alertdialog。

i have set bluetooth permissions in the androidmanifest file. In the scand function above, it is suppose to show the list of discovered devices, but it is displaying an empty dialog with just the title. Please tell me how to use startdiscovery and broadcastreceiver properly to display the result in the alertdialog.

推荐答案

startDiscovery()是异步调用之后您将不会收到结果。移动code,以显示该对话框功能让说公共无效showScanResult(),并呼吁在你的onReceive。

The startDiscovery() is asynchronous you will not receive the result right after the call. Move the code to show the dialog to a function let say public void showScanResult() and call that in your onReceive.

 
精彩推荐
图片推荐