检测连接的蓝牙媒体设备

时间:2017-12-10 08:19:08

标签: java android bluetooth device

我有一个BrodcastReciever带耳机插头事件和蓝牙连接。这很有效,但问题是当我连接时,例如,SmartWatch类似于我连接蓝牙耳机或条形音箱。我有什么办法来过滤可以播放其他音乐的媒体设备?

BrodcastReciever

sources := $(wildcard src/*.c)
objects := $(sources:src/%.c=obj/%.o)
deps    := $(objects:.o=.d)

CPPFLAGS := -MMD -MP
CFLAGS   := -g -Wall -pedantic
LDLIBS   := -lm

jogo.exe: $(objects)
    $(LINK.o) $^ $(LDLIBS) -o $@

obj/%.o: src/%.c
    $(COMPILE.c) $(OUTPUT_OPTION) $<

clean:
    -del $(subst /,\,$(objects) $(deps)) jogo.exe

-include $(deps)

MainActivity

@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)) {}

}

2 个答案:

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

    };

答案 1 :(得分:0)

使用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有足够的信息来查找它是什么类型的设备。