0

我想创建蓝牙 android 应用程序,这样当它与连接的设备断开连接时,它会发出哔哔声。我使用了以下代码

IntentFilter filter = new IntentFilter();     
filter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED);
filter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECT_REQUESTED);    
filter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);  
this.registerReceiver(mReceiver, filter); 

但是在这些情况下,如果任何一个设备(两个)被带走 1 厘米,它会发出哔哔声,我希望它至少在任何设备越过蓝牙范围时发出哔哔声??????

提前致谢

4

1 回答 1

0
    ToneGenerator toneGen  = new ToneGenerator(AudioManager.STREAM_MUSIC, 100);

IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECTED);
    this.registerReceiver(mReceiver, filter);
    mReceiver = new BroadcastReceiver() {
    @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

            if (BluetoothDevice.ACTION_FOUND.equals(action)) {
               //Device found
            }
            else if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) {
               //Device is now connected
               toneGen.stopTone(); // Stop alarm / beep when connected again
            }
            else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
                //Done searching
            }
            else if (BluetoothDevice.ACTION_ACL_DISCONNECT_REQUESTED.equals(action)) {
               //Device is about to disconnect
            }
            else if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)) {
               //Device has disconnected

                 toneGen.startTone(ToneGenerator.TONE_CDMA_ONE_MIN_BEEP)
            }           
        }
        };
于 2018-03-25T16:16:23.697 回答