gpt4 book ai didi

Android Bluetooth Discovery 采用顺序方法吗?

转载 作者:行者123 更新时间:2023-11-29 14:02:32 28 4
gpt4 key购买 nike


我是 Android 编程的新手。我想制作一个包含蓝牙发现功能的库(一个 JAR 文件)。
既然是库,里面的方法应该是顺序方法(启动蓝牙发现,等待一段时间,返回结果)。
我在下面做了一个代码,但它不起作用。在 LogCat 中,我可以看到 Intent ,但 BroadcastReceiver 无法捕捉到 Intent 。
代码有问题吗?

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

// Get local Bluetooth adapter
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

mSendButton = (Button) findViewById(R.id.button1);
mSendButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
doDiscovery();
}
});
}

private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();

if (BluetoothDevice.ACTION_FOUND.equals(action)) {
// Get the BluetoothDevice object from the Intent
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
Log.e("device_name", device.getName());
Log.e("device_add", device.getAddress());
numberOfDevice++;
Found_Device = true;
} else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
if(numberOfDevice == 0){
Log.e("DISCOVERY", "No Device found");
}
Log.e("DISCOVERY", "Number of device :" + numberOfDevice);
Discovery_Finish = true;
}
}
};

public void doDiscovery(){
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
registerReceiver(mReceiver, filter);

Discovery_Finish = false;
Found_Device = false;
long counter = 0;

if (mBluetoothAdapter.isDiscovering()) {
mBluetoothAdapter.cancelDiscovery();
}
mBluetoothAdapter.startDiscovery();
while((Found_Device == false) || (Discovery_Finish == false)){
counter++;
if (counter >= 1000000){
break;
}
}
// Add a code to check the number of device

unregisterReceiver(mReceiver);
mBluetoothAdapter.cancelDiscovery();
}

有什么方法可以不使用 BroadcastReceiver 来检查 Intent 吗?
感谢您的帮助。

最佳答案

我认为您无法开始发现其他设备。

关于Android Bluetooth Discovery 采用顺序方法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8790909/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com