Android:如何在给定的时间后完成蓝牙扫描?

问题描述 投票:0回答:1

因此,我创建了一个可扫描附近蓝牙设备的应用。现在,这部分工作正常。问题是,我希望它在30秒的时间段后停止发现过程。我该如何实现?

这是我的代码:

广播接收器:

  private BroadcastReceiver mBroadcastReceiver3 = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            final String action = intent.getAction();
            Log.d(TAG,"onReceive: Action Found");

            if(action.equals(BluetoothDevice.ACTION_FOUND))
            {
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);




                Log.d(TAG, "onReceive: " + device.getName() + ": " + device.getAddress());

                if(device.getName() != null)
                    {
                        //System.out.println("JUNGE ER FINDET DOCH SOGAR DAS DEVICE WIESO ZEIGT ER DIE SCHEIßE NICHT AN");
                        //Jetzt wo keine nullpointer Exception mehr ent
                        if(device.getName().contains("HC")) {
                            mBTDevices.add(device);
                            mDeviceListAdapter = new DeviceListAdapter(context, R.layout.device_adapter_view, mBTDevices);
                            lvNewDevices.setAdapter((mDeviceListAdapter));

                        }
                    }

            }

            /*else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
                Log.v(TAG, "Entered the Finished ");
                System.out.println("Entered the Finished");
            }

             */

        }




    } ;

发现方法(当我按下发现按钮时单击)

    public void btnDiscover(View view)
    {
        Log.d(TAG,"btnDiscover: Looking for unpaired Devices");

        if(mBluetoothAdapter.isDiscovering())
        {
            mBluetoothAdapter.cancelDiscovery();
            Log.d(TAG,"btnDiscover:Cancelling discovery.");
            //Checkt ob die Berechtigungen im Manifest für BT vorliegen
            checkBTPermissions();


            //start discovery again
            mBluetoothAdapter.startDiscovery();
            IntentFilter discoverDevicesIntent = new IntentFilter(BluetoothDevice.ACTION_FOUND);
            registerReceiver(mBroadcastReceiver3,discoverDevicesIntent);


        }

        if(!(mBluetoothAdapter.isDiscovering()))
        {
            //another check
            checkBTPermissions();

            mBluetoothAdapter.startDiscovery();
            IntentFilter discoverDevicesIntent = new IntentFilter(BluetoothDevice.ACTION_FOUND);
            registerReceiver(mBroadcastReceiver3,discoverDevicesIntent);
        }







    }

java android android-studio bluetooth
1个回答
1
投票

mBluetoothAdapter.startDiscovery();替换scann(mBluetoothAdapter,30);

void scann(final BluetoothAdapter mBluetoothAdapter, int seconds)
 {
  final long desired_miliseconds=seconds*1000;
  final long start_mils=System.currentTimeMillis();
final Timer tm=new Timer();
tm.schedule(new TimerTask() {
    @Override
    public void run() {
        if((System.currentTimeMillis()-start_mils)>=desired_miliseconds&&mBtAdapter.isDiscovering())
        {
            mBluetoothAdapter.cancelDiscovery();
            tm.cancel();
        }else if((System.currentTimeMillis()-start_mils)<desired_miliseconds&&!mBtAdapter.isDiscovering())
        {
            mBluetoothAdapter.startDiscovery();

        }

    }
},1000,1000);   

}
© www.soinside.com 2019 - 2024. All rights reserved.