gpt4 book ai didi

Android搜索蓝牙设备

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:00:33 24 4
gpt4 key购买 nike

我正在尝试查找可用的蓝牙设备。

这是我的 OnClickListener,它会在用户尝试搜索可用设备时调用。

View.OnClickListener OnSearchDevices = new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//Toast.makeText(context, "Search Devices", Toast.LENGTH_LONG).show();
Log.d("Discovery", "Started");
listOfDevices.clear();
label.setText("Searching Available Devices...");
label.setEnabled(false);
}
};

我还注册了一个 BroadcastReceiver。

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

// 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);
Log.d("DeviceList" , device.getName() + "\n" + device.getAddress());
MyBluetoothDevice tempDevice = new MyBluetoothDevice();
tempDevice.setDeviceAddress(device.getAddress());
tempDevice.setDeviceName(device.getName());
listOfDevices.add(tempDevice);
mListAdapter.notifyDataSetChanged();
// discovery is finished
}
else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {

Log.d("Discovery","Finished");
label.setEnabled(true);
if(listOfDevices.size() == 0)
{
label.setText("No Devices Available!");
label.setTextColor(Color.parseColor("#FF0000"));

}
else
{
label.setText("Available Devices");
}

}
}
};

但是没有任何反应。它没有显示任何内容。请帮忙。

最佳答案

看起来您错过了对 mBluetoothAdapter.startDiscovery() 的调用。

这可以解释为什么您没有得到任何结果,因为适配器甚至没有开始搜索设备。

除此之外,您的代码在我看来还不错。

关于Android搜索蓝牙设备,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17059057/

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