首页 > 解决方案 > 蓝牙扫描未检测到蓝牙设备

问题描述

我制作了一个小 android 应用程序来扫描蓝牙设备并向我的服务器发送 HTTP 请求,这样我就可以检测它们是打开还是关闭。我已经用带有蓝牙适配器的台式电脑对其进行了测试,它工作得很好。当检测到电脑时它显示电脑打开,当我关闭电脑上的蓝牙时它显示电脑关闭。现在,我需要使用这个应用程序的设备是:Yaber 投影仪、Bose SoundLink 和 JBL 耳机,但我遇到了一些问题。

首先,投影仪似乎无法与手机通信,我只能连接耳机或扬声器进入投影仪 BT 设置并扫描设备,但是当我扫描找到我的手机时,什么都没有出现,就像投影仪不可见一样手机,导致应用程序检测到它总是关闭。如果我用手机扫描投影仪也是一样。这怎么可能?

最后是扬声器和耳机,似乎一旦它们连接到设备(例如投影仪),它们就不再对手机可见,我认为这与电池节省/安全有关。但是有没有一种解决方法可以让它们即使在连接时也能继续检测到它们?

谢谢。

编辑

这是服务中运行扫描的代码,据我了解,它使用的是蓝牙分类技术而不是 BLE。

    private BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
private ArrayList<BluetoothDevice> arrayList = new ArrayList<>();

    @Override
public int onStartCommand(Intent intent, int flags, int startId) {

    createNotificationChannel();

    Intent intent1 = new Intent(this, MainActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent1,0);

    Notification notification = new NotificationCompat.Builder(this,"BTAPP")
            .setContentTitle("Bluetooth Scan")
            .setContentText("App is scanning")
            .setContentIntent(pendingIntent).build();

    startForeground(1,notification);

    IntentFilter intentFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
    IntentFilter intentFilter2 = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
    IntentFilter intentFilter3 = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
    registerReceiver(broadcastReceiver, intentFilter);
    registerReceiver(broadcastReceiver, intentFilter2);
    registerReceiver(broadcastReceiver, intentFilter3);
    bluetoothAdapter.startDiscovery();

    return START_STICKY;
}

final BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {

    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();

        // When discovery starts
        if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {
            //clearing any existing list data
            flagJBL = false;
            flagBose = false;
            flagProjector = false;
            arrayList.clear();
        }

        // When discovery finds a device
        if (BluetoothDevice.ACTION_FOUND.equals(action)) {
            // Get the BluetoothDevice object from the Intent
            BluetoothDevice device =
                    intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

            // Add the name and address to an array adapter
            if (!arrayList.contains(device)) {
                if (device.getAddress().equals(JBL_HEADSET_ADDRESS))
                    flagJBL = true;
                if (device.getAddress().equals(BOSE_SOUNDLINK_ADDRESS))
                    flagBose = true;
                if (device.getAddress().equals(PROJECTOR_ADDRESS))
                    flagProjector = true;
                arrayList.add(device);
            }
        }

        // When discovery starts
        if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
            //clearing any existing list data
            //Toast.makeText(getApplicationContext(), "Scan has stopped",Toast.LENGTH_SHORT).show();
            if (flagJBL) {
                Intent jbloni = new Intent(getApplicationContext(), RequestHandler.class);
                jbloni.putExtra("URL",JBL_ON_URL);
                startService(jbloni);
            }
            //showNotification("JBL Result", "JBL is On");
            else {
                Intent jbloffi = new Intent(getApplicationContext(), RequestHandler.class);
                jbloffi.putExtra("URL",JBL_OFF_URL);
                startService(jbloffi);
            }
            //showNotification("JBL Result", "JBL is Off");
            if (flagBose) {
                Intent boseoni = new Intent(getApplicationContext(), RequestHandler.class);
                boseoni.putExtra("URL",BOSE_ON_URL);
                startService(boseoni);
                // showNotification("Bose Result", "Bose is On");
            }
            else {
                Intent boseoffi = new Intent(getApplicationContext(), RequestHandler.class);
                boseoffi.putExtra("URL",BOSE_OFF_URL);
                startService(boseoffi);
                //showNotification("Bose Result", "Bose is Off");
            }
            if (flagProjector) {
                Intent projectoroni = new Intent(getApplicationContext(), RequestHandler.class);
                projectoroni.putExtra("URL",PROJECTOR_ON_URL);
                startService(projectoroni);
                //showNotification("Projector Result", "Projector is On");
            }
            else {
                Intent projectoroffi = new Intent(getApplicationContext(), RequestHandler.class);
                projectoroffi.putExtra("URL",PROJECTOR_OFF_URL);
                startService(projectoroffi);
                //showNotification("Projector Result", "Projector is Off");
            }

            bluetoothAdapter.startDiscovery();
        }
    }
};

标签: androidbluetoothdevicescanning

解决方案


根据您的描述,听起来您的 Android 应用程序仅在扫描蓝牙 LE 设备。由于投影机最有可能使用蓝牙经典并且这两种技术,除了它们的名称之外,不兼容您无法扫描它。如果您也想检测这些设备,则需要在第二步中扫描蓝牙经典设备。

就您的扬声器和耳机而言,根据我的观察,我可以说这种行为实际上是行业标准。即使在技术上可以在连接期间继续宣传自己的存在,但从应用程序的角度来看,它仅在极少数情况下才有意义,因此很少实施。


推荐阅读