蓝牙广播接收器永远不会调用ACTION_DISCOVERY_STARTED

时间:2015-02-17 10:36:19

标签: android bluetooth broadcastreceiver intentfilter

我正在运行一项服务,我正在接收蓝牙广播接收器。但它永远不会达到ACTION_DISCOVERY_FINISH和ACTION_DISCOVERY_STARTED。我不知道为什么当我在android设置中手动进入蓝牙门户时,然后完成并开始,我可以在我的广播接收器中看到日志。

这是代码

public void run() 
{
    if(service != null && target.equals("User")) 
        {
            bluetooth = BluetoothAdapter.getDefaultAdapter();
            IntentFilter filter = new IntentFilter();
            filter.addAction(BluetoothDevice.ACTION_FOUND);
           filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
            filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
            bReceiver = new  BluetoothReceiver();
            service.registerReceiver(bReceiver, filter);
            bluetooth.startDiscovery();
        }

    if(target.equals("Server")) 
        {
            if (((!bluetooth.isEnabled()))
                {
                    bt = BluetoothAdapter.getDefaultAdapter();
                    Log.d(TAG, "test Bt #1");
                    if(!bt.isEnabled()) {
                        Log.d(TAG, "test Bt #2");
                        bt.enable();
                        firstCall = true;
                        btReceiver = new  BluetoothReceiver();
                        filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
                        service.registerReceiver(btReceiver, filter);
                        filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
                        service.registerReceiver(btReceiver, filter);
                        filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
                        service.registerReceiver(btReceiver, filter);
                        filter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
                        service.registerReceiver(btReceiver, filter);
                        bt.startDiscovery();
                        shouldTurnOff = true;
                    }
                }
        }
}

class BluetoothReceiver extends BroadcastReceiver 
{
    public void onReceive(Context context, Intent intent) 
    {
        Log.d(TAG, "Start Action OnReceive");
        action = intent.getAction();
        if(BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action))
            {
                Log.d(TAG, "Start Discovery");

            }
        if(BluetoothDevice.ACTION_FOUND.equals(action)) 
            {
                Log.d(TAG, "Start Action FOUND");
            }

        if(BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) 
            {
                Log.d(TAG, "Start Action FINISHED");
           }
     }
  }

当代码在后台时,代码永远不会超出onReceive的日志。

0 个答案:

没有答案