【问题标题】:Detect bluetooth media device connected检测连接的蓝牙媒体设备
【发布时间】:2017-12-10 08:19:07
【问题描述】:

我有一个 BrodcastReciever 接收耳机插头事件和蓝牙连接。这很好用,但问题是当我连接时,例如,SmartWatch 类似于我连接蓝牙耳机或条形音箱。我该怎么做才能过滤可以播放其他音乐的媒体设备?

广播接收者

@Override
public void onReceive(final Context context, Intent intent) {
    String action = intent.getAction();
    if (Intent.ACTION_MEDIA_BUTTON.equals(action)) {
        if (Settings.PROCESS_PRESSING){
            KeyEvent event = intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
            if (isAvailable) {
                if (mTimer != null) mTimer.cancel();
                if (myTimer != null) myTimer.cancel();
                mTimer = new Timer();
                myTimer = new MyTimerTask();
                if (KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE == event.getKeyCode()) {
                    listener.playButtonPressed();
                } else if (KeyEvent.KEYCODE_MEDIA_PLAY == event.getKeyCode()) {
                    listener.playButtonPressed();
                } else if (KeyEvent.KEYCODE_MEDIA_PAUSE == event.getKeyCode()) {
                    listener.playButtonPressed();
                } else if (KeyEvent.KEYCODE_MEDIA_STOP == event.getKeyCode()) {
                    listener.playButtonPressed();
                } else if (KeyEvent.KEYCODE_MEDIA_NEXT == event.getKeyCode()) {
                    listener.nextTrackButtonPressed();
                } else if (KeyEvent.KEYCODE_MEDIA_PREVIOUS == event.getKeyCode()) {
                    listener.previousTrackButtonPressed();
                }

                isAvailable = false;
                mTimer.schedule(myTimer, 500);
            }
        }
    } else if (action.equals(Intent.ACTION_HEADSET_PLUG)) {
        int state = intent.getIntExtra("state", -1);
        switch (state) {
            case 0:

                break;
            case 1:
                if (Settings.START_HEADSET) listener.headsetPlugged();
                break;

        }
    }
    else if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) {
        if (Settings.START_BLUETOOTH)
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        Thread.sleep(6000);
                        Handler mainHandler = new Handler(context.getMainLooper());
                        Runnable myRunnable = new Runnable() {
                            @Override
                            public void run() {
                                listener.headsetPlugged();
                            }
                        };
                        mainHandler.post(myRunnable);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }).start();

    }
     else if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)) {}

}

主活动

 headsetFilter = new IntentFilter();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        headsetFilter.addAction(AudioManager.ACTION_HEADSET_PLUG);
    }
    headsetFilter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED);
    headsetFilter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECT_REQUESTED);
    headsetFilter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);

    getApplicationContext().registerReceiver(control_reciever, headsetFilter);

【问题讨论】:

    标签: java android bluetooth device


    【解决方案1】:

    接收 BluetoothA2dp.STATE_CONNECTED 意图然后处理

     BroadcastReceiver mReceiver = new BroadcastReceiver() {
    
            @Override
            public void onReceive(Context ctx, Intent intent) {
                String action = intent.getAction();
                Log.d(TAG, "receive intent for action : " + action);
                if (action.equals(BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED)) {
                    int state = intent.getIntExtra(BluetoothA2dp.EXTRA_STATE, BluetoothA2dp.STATE_DISCONNECTED);
                    if (state == BluetoothA2dp.STATE_CONNECTED) {
                        setIsA2dpReady(true);
                        playMusic();
                    } else if (state == BluetoothA2dp.STATE_DISCONNECTED) {
                        setIsA2dpReady(false);
                    }
                } else if (action.equals(BluetoothA2dp.ACTION_PLAYING_STATE_CHANGED)) {
                    int state = intent.getIntExtra(BluetoothA2dp.EXTRA_STATE, BluetoothA2dp.STATE_NOT_PLAYING);
                    if (state == BluetoothA2dp.STATE_PLAYING) {
                        Log.d(TAG, "A2DP start playing");
                        Toast.makeText(A2DPActivity.this, "A2dp is playing", Toast.LENGTH_SHORT).show();
                    } else {
                        Log.d(TAG, "A2DP stop playing");
                        Toast.makeText(A2DPActivity.this, "A2dp is stopped", Toast.LENGTH_SHORT).show();
                    }
                }
            }
    
        };
    

    【讨论】:

    • 欢迎亲爱的!!伊万萨布林 :)
    【解决方案2】:

    使用MediaRouter库查找设备连接和设备类型

         MediaRouter  mediaRouter = MediaRouter.getInstance(WEYVCoreUtils.getApplicationContext());
            MediaRouteSelector mediaRouteSelector = new MediaRouteSelector.Builder().addControlCategory(MediaControlIntent.CATEGORY_LIVE_AUDIO).build()
    
    mediaRouter.addCallback(mediaRouteSelector, new MediaRouterCallback(), MediaRouter.CALLBACK_FLAG_REQUEST_DISCOVERY);
    
        public static class MediaRouterCallback extends MediaRouter.Callback {
            /**
             * Called when the supplied media route becomes selected as the active route.
             *
             * @param router The media router reporting the event.
             * @param route The route that has been selected.
             */
            public void onRouteSelected(MediaRouter router, RouteInfo route) {
            }
    
            /**
             * Called when the supplied media route becomes unselected as the active route.
             * For detailed reason, override {@link #onRouteUnselected(MediaRouter, RouteInfo, int)}
             * instead.
             *
             * @param router The media router reporting the event.
             * @param route The route that has been unselected.
             */
            public void onRouteUnselected(MediaRouter router, RouteInfo route) {
            }
    
            /**
             * Called when the supplied media route becomes unselected as the active route.
             * The default implementation calls {@link #onRouteUnselected}.
             * <p>
             * The reason provided will be one of the following:
             * <ul>
             * <li>{@link MediaRouter#UNSELECT_REASON_UNKNOWN}</li>
             * <li>{@link MediaRouter#UNSELECT_REASON_DISCONNECTED}</li>
             * <li>{@link MediaRouter#UNSELECT_REASON_STOPPED}</li>
             * <li>{@link MediaRouter#UNSELECT_REASON_ROUTE_CHANGED}</li>
             * </ul>
             *
             * @param router The media router reporting the event.
             * @param route The route that has been unselected.
             * @param reason The reason for unselecting the route.
             */
            public void onRouteUnselected(MediaRouter router, RouteInfo route, int reason) {
                onRouteUnselected(router, route);
            }
    
            /**
             * Called when a media route has been added.
             *
             * @param router The media router reporting the event.
             * @param route The route that has become available for use.
             */
            public void onRouteAdded(MediaRouter router, RouteInfo route) {
            }
    
            /**
             * Called when a media route has been removed.
             *
             * @param router The media router reporting the event.
             * @param route The route that has been removed from availability.
             */
            public void onRouteRemoved(MediaRouter router, RouteInfo route) {
            }
    
            /**
             * Called when a property of the indicated media route has changed.
             *
             * @param router The media router reporting the event.
             * @param route The route that was changed.
             */
            public void onRouteChanged(MediaRouter router, RouteInfo route) {
            }
    
            /**
             * Called when a media route's volume changes.
             *
             * @param router The media router reporting the event.
             * @param route The route whose volume changed.
             */
            public void onRouteVolumeChanged(MediaRouter router, RouteInfo route) {
            }
    
            /**
             * Called when a media route's presentation display changes.
             * <p>
             * This method is called whenever the route's presentation display becomes
             * available, is removed or has changes to some of its properties (such as its size).
             * </p>
             *
             * @param router The media router reporting the event.
             * @param route The route whose presentation display changed.
             *
             * @see RouteInfo#getPresentationDisplay()
             */
            public void onRoutePresentationDisplayChanged(MediaRouter router, RouteInfo route) {
            }
    
            /**
             * Called when a media route provider has been added.
             *
             * @param router The media router reporting the event.
             * @param provider The provider that has become available for use.
             */
            public void onProviderAdded(MediaRouter router, ProviderInfo provider) {
            }
    
            /**
             * Called when a media route provider has been removed.
             *
             * @param router The media router reporting the event.
             * @param provider The provider that has been removed from availability.
             */
            public void onProviderRemoved(MediaRouter router, ProviderInfo provider) {
            }
    
            /**
             * Called when a property of the indicated media route provider has changed.
             *
             * @param router The media router reporting the event.
             * @param provider The provider that was changed.
             */
            public void onProviderChanged(MediaRouter router, ProviderInfo provider) {
            }
        }
    

    当附近有新设备可用时,将调用上述回调 MediaRouter.RouteInfo 有足够的信息来查找它是什么类型的设备。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-28
      • 2018-01-20
      • 2013-02-19
      • 2013-05-16
      • 1970-01-01
      相关资源
      最近更新 更多