[英]Android search for bluetooth devices
I am trying to find the available Bluetooth devices. 我正在尝试找到可用的蓝牙设备。
This is my OnClickListener which is called when the user tries to search for the available devices. 这是我的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);
}
};
I have also registered a BroadcastReceiver. 我还注册了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");
}
}
}
};
But nothing happens. 但没有任何反应。 It does not show anything.
它没有显示任何东西。 Please help.
请帮忙。
Looks like you are missing the call to mBluetoothAdapter.startDiscovery()
. 看起来你错过了对
mBluetoothAdapter.startDiscovery()
的调用。
That would explain why you are not getting any results, because the adapter doesn't even start searching for the devices. 这可以解释为什么你没有得到任何结果,因为适配器甚至没有开始搜索设备。
Your code looks fine to me otherwise. 否则你的代码对我来说很好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.