In this code I am scanning for bluetooth devices when button is clicked, but I am not getting any discovered devices.
public class MainActivity extends AppCompatActivity {
private static final int REQUESTCODE =200 ;
BluetoothAdapter bluetoothAdapter;
private boolean isDeviceEnabled=false;
Button button;
BroadcastReceiver broadcastReceiver=new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action=intent.getAction();
Log.d("TAG","DSSSSS");
if (BluetoothDevice.ACTION_FOUND.equals(action)){
BluetoothDevice device=intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
Log.d("TAG ","Unknown Device address "+device.getAddress());
Log.d("TAG ","Unknown Device name "+device.getName());
bluetoothAdapter.cancelDiscovery();
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button=findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
scanBluetoothdevice();
}
});
bluetoothAdapter=BluetoothAdapter.getDefaultAdapter();
if (null==bluetoothAdapter){
Toast.makeText(this, "Bluetooth Missing", Toast.LENGTH_SHORT).show();
}else{
if (!bluetoothAdapter.isEnabled()){
Intent intent=new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(intent,REQUESTCODE);
}else {
noOfPairedDevices();
}
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode==REQUESTCODE && resultCode==RESULT_OK){
noOfPairedDevices();
}else {
Toast.makeText(this,"BlueTooth Off",Toast.LENGTH_SHORT).show();
}
}
private void noOfPairedDevices(){
Set<BluetoothDevice> bluetoothDevices=bluetoothAdapter.getBondedDevices();
if (bluetoothDevices.size() >0){
for (BluetoothDevice device :bluetoothDevices){
Log.d("Tag","Paired Device address="+device.getAddress());
Log.d("Tag","Paired Device name="+device.getName());
}
}
}
private void scanBluetoothdevice(){
IntentFilter filter=new IntentFilter(BluetoothDevice.ACTION_FOUND);
if (!isDeviceEnabled){
this.registerReceiver(broadcastReceiver,filter);
bluetoothAdapter.startDiscovery();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
this.unregisterReceiver(broadcastReceiver);
bluetoothAdapter.cancelDiscovery();
}
}
I have also added the following permissions in the Android manifest:
uses-permission android:name="android.permission.BLUETOOTH"/>
uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
In the Log cat it is showing:
08-10 01:28:50.146 25450-25463/dead.bluetooth D/BluetoothAdapter: onBluetoothStateChange: up=true
onBluetoothStateChange: Bluetooth is on
08-10 01:28:50.266 25450-25450/dead.bluetooth D/Tag: Paired Device address=BC:D1:1F:AB:1E:C1
08-10 01:28:50.276 25450-25450/dead.bluetooth D/Tag: Paired Device name=Galaxy J5 TULSI@avenger
Paired Device address=CC:73:14:85:D5:49
08-10 01:28:50.316 25450-25450/dead.bluetooth D/Tag: Paired Device name=Cloud S9
08-10 01:28:50.356 25450-25450/dead.bluetooth I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@9f162ab time:16915604
08-10 01:28:53.896 25450-25450/dead.bluetooth D/ViewRootImpl: ViewPostImeInputStage processPointer 0
08-10 01:28:53.986 25450-25450/dead.bluetooth D/ViewRootImpl: ViewPostImeInputStage processPointer 1
08-10 01:28:53.986 25450-25450/dead.bluetooth D/BluetoothAdapter: startDiscovery
08-10 01:28:53.986 25450-25450/dead.bluetooth D/BluetoothAdapter: startDiscovery = true
You are missing ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission in your manifest. Take a look at this . Users are likely to deny this permission if you don't explain why you need it, so show them an explanation before requesting permission.
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.