【发布时间】: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