gpt4 book ai didi

Android BluetoothDevice.Action_found 广播从未收到

转载 作者:行者123 更新时间:2023-12-02 03:15:18 42 4
gpt4 key购买 nike

<分区>

我正在尝试在 ListView 中列出附近所有可用的设备。

我的 list 包含 BLUETOOTH、BLUETOOTH_ADMIN 和 ACCESS_COARSE_LOCATION 的权限。

我的问题是收到了 ACTION_DISCOVERY_STARTED 和 ACTION_DISCOVERY_FINISHED 但没有收到 ACTION_FOUND。我有我的电脑蓝牙可发现,我手机上的蓝牙设备可以发现它,但我的应用程序不能。这同样适用于附近的所有设备。

private BluetoothAdapter mBluetoothAdapter;
private ArrayList<String> mDeviceList;
private ListView mListView;
private ProgressDialog progressDialog;

private static final int REQUEST_ENABLE_BT = 1;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bluetooth_list);

//list to hold all devices found
mDeviceList = new ArrayList<>();

//list to display all devices found
mListView = (ListView) findViewById(R.id.availItems);
mListView.setAdapter(new ArrayAdapter<>(this,
android.R.layout.simple_list_item_1, mDeviceList));

//local bluetooth device
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

// If the adapter is null, then Bluetooth is not supported
if (mBluetoothAdapter == null) {
Toast.makeText(this, "Bluetooth is not available", Toast.LENGTH_LONG).show();
}else if(!mBluetoothAdapter.isEnabled()){
Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
}

Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();

if (pairedDevices.size() > 0) {
for (BluetoothDevice device : pairedDevices) {
mDeviceList.add(device.getName() + "\n" + device.getAddress());
}
}

progressDialog = new ProgressDialog(this);
progressDialog.setMessage("Scanning...");
progressDialog.setTitle("Looking for devices...");
progressDialog.setCancelable(false);
progressDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});

Button findButton = (Button) findViewById(R.id.findButton);
findButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
IntentFilter filter = new IntentFilter();

filter.addAction(BluetoothDevice.ACTION_FOUND);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);

registerReceiver(mReceiver, filter);
mBluetoothAdapter.startDiscovery();
}
});
}

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

if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {
progressDialog.show();
}else if (BluetoothDevice.ACTION_FOUND.equals(action) && mBluetoothAdapter.isDiscovering()) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
mDeviceList.add(device.getName() + "\n" + device.getAddress());
}else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
progressDialog.dismiss();
}
}
};

@Override
protected void onResume() {
super.onResume();
registerReceiver(mReceiver, new IntentFilter(BluetoothDevice.ACTION_FOUND));
}

@Override
public void onPause() {
if (mBluetoothAdapter != null) {
if (mBluetoothAdapter.isDiscovering()) {
mBluetoothAdapter.cancelDiscovery();
}
}

super.onPause();
}

@Override
public void onDestroy() {
unregisterReceiver(mReceiver);
mListView = null;
mBluetoothAdapter = null;
mDeviceList = null;

super.onDestroy();
}

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