【问题标题】:Bluetooth Devices Not Detected with Bluetooth Scan蓝牙扫描未检测到蓝牙设备
【发布时间】:2021-12-26 19:00:16
【问题描述】:

我制作了一个小安卓应用程序来扫描蓝牙设备并向我的服务器发送 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();
        }
    }
};

【问题讨论】:

  • 扬声器和耳机很可能在任何给定时间只允许一个蓝牙设备作为音乐源。这就是为什么如果你连接到他们,他们会隐藏
  • 是的,这就是我所坚持的,因此该应用程序将无法在他们身上使用。谢谢。

标签: android bluetooth device scanning


【解决方案1】:

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

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

【讨论】:

  • 我已经添加了代码,但据我所知,它使用的是经典的 BT 而不是 BLE。可能是相反的吗?使用BLE的投影仪?即使我认为这不太可能。
  • 我同意,根据您的代码,您正在执行蓝牙经典发现程序。我已经将自己定位在措辞上,“扫描”代表蓝牙 LE,而在蓝牙经典的上下文中,“发现”更为常见。关于未找到接收器 - 正如 Android 文档中所述,设备需要“可发现”才能在发现过程中找到。也许这需要以某种方式启用。 developer.android.com/reference/android/bluetooth/…
  • 但是如果我打开我的扬声器,它会自动连接到投影仪,所以它是可以发现的,但我无法用手机找到它。是否只能在某些设备上发现?如果我做相反的事情,在投影仪BT设置中扫描手机,它会扫描扬声器而不是手机。
  • 您认为可以由另一台设备连接的蓝牙设备是“可发现的”的假设是错误的。如果两个连接伙伴“认识”对方(因为他们已经配对和绑定),则设备不需要是“可发现的”。
  • 投影机没有列出手机很可能是不兼容的结果。作为“音频分配源”的投影仪会搜索扬声器和耳机等“音频分配接收器”,而不是手机等其他“源”设备...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多