this code for search and discovering bluetooth devices
i make Toast on BroadcastReceiver they don't even show it (android 7 a guess the issue in permission i just put it on mainfist)
Button button;
ListView listView;
BluetoothAdapter mBluetoothAdapter;
Integer requestbluetooth=1;
ArrayList<String> arrayList;
ArrayAdapter<String> Adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
arrayList =new ArrayList<>();
button=findViewById(R.id.printButton);
listView=findViewById(R.id.listView);
Adapter = new ArrayAdapter<String>(getBaseContext(),android.R.layout.simple_list_item_1,arrayList);
listView.setAdapter(Adapter);
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(receiver, filter);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
mBluetoothAdapter.startDiscovery();
}
});
}
BroadcastReceiver receiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
String deviceName = device.getName();
String deviceHardwareAddress = device.getAddress();
arrayList.add(deviceName);
Adapter.notifyDataSetChanged();
}
}
};
Since Android 6.0 (API level 23) you have to request the permissions at runtime. Most application do that when the user is starting the app for the first time.
To check if you have the permission already, use
int permissionCheck = ContextCompat.checkSelfPermission(thisActivity, Manifest.permission.READ_SMS);
To request permission, use
ActivityCompat.requestPermissions(thisActivity, new String[]{Manifest.permission.READ_SMS}, MY_PERMISSIONS_REQUEST_READ_CONTACTS);
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.