I'm pretty sure there's at least two devices disoverable with bluetooth but my application just show me paired one. I already gived bluetooth permission and i don't understand where is the problem. Can you find it?
//Scan Function
void startScan(){
adapter.clear(); //List View cleared
deviceList = btAdapter.getBondedDevices(); //Paired Device
for (BluetoothDevice bd : deviceList) //Adding Paired Device
adapter.add(bd.getName() + "\n" + bd.getAddress()+"\t (Paired)");
//Init of Broadcast Receiver
receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(BluetoothDevice.ACTION_FOUND.equals(action)){ //Device Found
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); //Extractind Device Info
adapter.add(device.getName()+"\t"+device.getAddress()); //Adding Device to List View
}
}
};
//Broadcast Receiver Registration
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(receiver,filter);
btAdapter.startDiscovery(); //Scan start
}
If I am not wrong you are getting only bonded device. Your code should be something like below.
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
void startScan(){
adapter.clear(); //List View cleared
deviceList = btAdapter.getBondedDevices(); //Paired Device
for (BluetoothDevice bd : deviceList) //Adding Paired Device
adapter.add(bd.getName() + "\n" + bd.getAddress()+"\t (Paired)");
//Broadcast Receiver Registration
Check for bluetooth enable
(!btAdapter.isEnabled()) {
Intent BtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(BtIntent, 0);
Toast.makeText(BluetoothScannerActivity.this, "Turning on Bluetooth", Toast.LENGTH_LONG).show();
}
else{
registerReceiver(receiver,filter);
btAdapter.startDiscovery(); //Scan start
}
}
Your broadcast should be out of method, its better parctice
receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(BluetoothDevice.ACTION_FOUND.equals(action)){ //Device Found
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); //Extractind Device Info
adapter.add(device.getName()+"\t"+device.getAddress()); //Adding Device to List View
}
}
};
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == BtIntent && resultCode == RESULT_OK) {
registerReceiver(receiver,filter);
btAdapter.startDiscovery();
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.