蓝牙断线报警

时间:2018-03-25 16:05:42

标签: android android-bluetooth tone-generator

我想创建蓝牙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厘米,它会发出哔哔声,我希望它至少在任何设备超过蓝牙范围时发出哔哔声?????

提前致谢

1 个答案:

答案 0 :(得分: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)
            }           
        }
        };